Jump to content

Change Image Description On Click


BorysSokolov

Recommended Posts

I built a photo gallery which changes images on click (using jQuery), and now I need to implement photo descriptions. Obviously, I want the descriptions to change according to the image currently being displayed. I did that by passing post data using JS to a PHP file which fetched the description for the given photo from a database, but unfortunately, there was a delay, and I want the description to load instantaneously  I though about fetching all the data at the start, and then just retrieving them in the form of an array whenever the image changes, but I'm not sure how to pass the array into JS; of course, I could just $.ajax for the data from a PHP file, which would spit out the array, but it would still have to query on every click, which would defeat the purpose, since I want to query only once.

 

Any ideas? Not sure if I'm being descriptive enough. :confused:

Link to comment
https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/
Share on other sites

If you don't want to load your data via ajax (which of course will incur a delay) you will need to load it all up front. I would suggest storing it in an array or json.

 

Now, please stop posting javascript questions in the PHP Help forum. I'm starting to get tired of moving your threads.

Alright, so I ran a query and fetched all the data at the start, like you said, and then ran JS which loaded all the PHP array information into a javascript array:

<script type = "text/javascript">
	var jsonarray = <?php echo json_encode($holdImageInfo); ?>;
</script>

Then, whenever the user skipped to the next photo, I just set the appropriate div content to the description of the image being displayed:


$('#encaseComments').html(jsonarray[imageBasename]['description']); 

It works fine, but is this the standard approach?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.