Jump to content

Change Image Description On Click


BorysSokolov
Go to solution Solved by trq,

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
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.