I'll try to be as thorough as possible here.
The application that I am working on enables administrators to create folders, and then upload photos into that folder. The display is similar to that of Pinterest, where it loads all of the photos in a collage on one screen. It works perfect when you only have 5 - 6 photos in there, but that is rarely going to be the case. Once you start getting a lot more photos in there, it starts becoming more slow. The other thing is, whenever you delete a picture, or re-enter that folder, the entire display has to reload. I need some way of caching these images.
Here is the logic of my script:
On dashboard.php, there are two javascript includes: homepageActions.js and fileManager.js. homepageActions.js has a document.ready() script, that calls a javascript function, showFiles().
So basically, to be a bit more concise, as soon as the document loads, the function showFiles() gets called. That function looks like this:
function showFiles(directoryName) {
$.ajax({
url: 'displayRecords.php',
type: 'post',
data: ({directoryName:directoryName}),
dataType: 'json',
success: function(data) {
constructTable(data);
$(document).find("a[rel*='lightbox']").lightBox();
}
});
}
This function has a folder path passed into it. From there, the AJAX script calls a PHP script which looks in that folder, and gets a list of all of the directories and images in that folder. It then constructs a JSON string which contains the details of each subfolder and image inside the folder that was originally passed in. Once that script successfully returns, another function, constructTable(data) is called, which passes in the JSON string to the function.
Here is where things start to get a little hairy.
The constructTable() function has about 240 lines of code in it, which I won't include here, because it gets hard to follow. To summarize, this function parses the JSON string, figures out which entries are directories and which entries are files. It creates two arrays from that: an array of folders, and an array of files.
First I process all of the folders, and display them accordingly. This part is fine, and I have no concerns about.
After that however, I start going through the images. For each image, I am basically writing a lot of code around it. There are div's, links, and other HTML markup surrounding each image. But long story short, I have a for...next loop that looks at each file path (gathered from the JSON), and I construct a long string of HTML which I then use jQuery to append into a DIV. So it ultimate ends up looking something like this:
for (var a = 0; a < numPhotos; a++) {
photoCode += "<div id='outside-container-"+a+"'>";
photoCode += "<a rel='lightbox' class='photo-link' href='"++"'>";
photoCode += "<img src='http://" + rootURL + "/uploads/"+fileNameArray[a] + "' width='200' class='photo-thumb'/></a>";
photoCode += "</div>";
$("#column1").append(photoCode);
}
Now - as I previously said, when there are only a couple of photos in the folder, this is no problem. However, when we start getting into higher numbers, it becomes a problem. So - with that being said, is there any way that I can store these photos into the browser cache, so that they load a lot more quickly after the initial load?