BorysSokolov Posted April 14, 2013 Share Posted April 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/ Share on other sites More sharing options...
Solution trq Posted April 14, 2013 Solution Share Posted April 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/#findComment-1424648 Share on other sites More sharing options...
BorysSokolov Posted April 14, 2013 Author Share Posted April 14, 2013 I understand that bit. My issue is that I don't know how to pass the array that holds the descriptions over to javascript. Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/#findComment-1424654 Share on other sites More sharing options...
trq Posted April 14, 2013 Share Posted April 14, 2013 You need a javascript array, not a php array. Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/#findComment-1424659 Share on other sites More sharing options...
BorysSokolov Posted April 14, 2013 Author Share Posted April 14, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/#findComment-1424691 Share on other sites More sharing options...
trq Posted April 15, 2013 Share Posted April 15, 2013 It is a fairly common approach yeah. Just so you know though, your not dealing with JavaScript arrays here, but simple objects. Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/#findComment-1424772 Share on other sites More sharing options...
BorysSokolov Posted April 16, 2013 Author Share Posted April 16, 2013 I just wanted to make sure, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/276923-change-image-description-on-click/#findComment-1424992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.