Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON Object getting a list of images I have on the page. The images are generated at random using a database. I want to keep adding to the images using AJAX whn the user for example clicks "show more". The images src are found so that no duplicates are pulled from the database on the AJAX call.

I want to know how to parse this array to the pHp file to perform an SQL statement to get more images to append to the images list. In the php file how would I select the elements from there. So SELECT * FROM images WHERE image_src NOT IN (JSON array) ORDER BY rand() LIMIT 12

CODE

var imageArray = {};
imageArray.itemList = [];

function imagesLoaded(){
    var gal_img_thumbs = document.getElementsByClassName("gal-thumb-image");
    for (var i = 0; i < gal_img_thumbs.length ; i++){
        var img = gal_img_thumbs[i].firstChild.getAttribute("src");         
        var new_obj = {'src':img};
        imageArray.itemList.push( JSON.stringify(new_obj));

        //imageArray.itemList.[i]['src']=img;
    }
}

function loadImages(){
    console.log("load images");
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
  xmlhttp.open("GET","getimages.php?imgArray="+imageArray.itemList,true);
  xmlhttp.send();
}
share|improve this question
    
I strongly recommend against this, if you have 500 items in the array your last page is going to have an SQL query containing about 492 exceptions(based on the idea of 12 per page). That's going to take a significant load on your DB. Sorting by rand() is also incredibly inefficient. Might I suggest pre-generating a random number instead? You can then use the normal limit clause to prevent causing issues and the results won't appear to be sorted by any field. If everyone should get different results, or new results on reload let me know, and I'll suggest something else. –  scragar 17 mins ago
    
If the php is called again and the random numbers generated again, assume we had: image 1, 2, 3, 4, 5, 6, 7, 8 in the database image 2, 4, 5 called in the HTML page calling the php again may result in 2, 4, 5 changing too. I want 2, 4, 5 to remain then generate another 3 random images but 2, 4, 5 –  user3012749 9 mins ago
    
Explain what you want to see, what is your end goal? Should every visit get a unique view of the images? Is it OK if the number of possibilities is reduced to improve performance and make the code manageable, or does it have to be completely unique every time(IE can you just have 10,000 possible random variants and just pick one of the set)? –  scragar 6 mins ago
    
each time a user lands on the page 12 images are generated randomly. Performing AJAX calls to the database via php, more elements should be loaded at random for example another 12 when the click a button. These newly generated 12 images should be unique to the already loaded images. The user shouldn't need to request the page again hence using AJAX. –  user3012749 4 mins ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.