wrathican Posted July 18, 2008 Share Posted July 18, 2008 hey guys, im trying to create next and previous functions for an image viewing script. the links for the next and previous images will be only viewable when a single image is being viewd. im trying to get the next id of an image from a db. heres what im trying to use so far. <?php //$currentAl = the album being viewed //$currentImg = current image being viewed function getNextImg($currentAl, $currentImg) { $query = "SELECT pic_id FROM won_picture WHERE pic_albumid='$currentAl'"; $result = query($query); $imgId = mysql_fetch_array($result) while ($imgId != $currentImg) { } return $nextImg; } ?> im a bit stuck on what i should write next. i gather all i need to do is get the numerical value of the position of current image id in the array, then add 1 to it and voila i have my next id in the sequence. i imagine the id's are stored in the array like so; $imgId[0] is the image id in the db of the first image $imgId[1] is the second image and so on.. so, my question is: how do i find out what posistion in the array the current image id is? and how do i get the next array value? Link to comment https://forums.phpfreaks.com/topic/115404-solved-getting-next-value-of-an-array/ Share on other sites More sharing options...
Kairu Posted July 18, 2008 Share Posted July 18, 2008 If you call your page like http://blah.com/page.php?id=1&al=1 You can do this: <?php $currentAl = $_GET['al'] $currentImg = $_GET['id']; function getNextImg($currentAl, $currentImg) { $query = "SELECT pic_id FROM won_picture WHERE pic_albumid='$currentAl'"; $result = query($query); $imgId = mysql_fetch_array($result) while ($imgId != $currentImg) { } return $nextImg; } ?> Link to comment https://forums.phpfreaks.com/topic/115404-solved-getting-next-value-of-an-array/#findComment-593279 Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 try this $query = "SELECT pic_id FROM won_picture WHERE pic_albumid='$currentAl' and pic_id > '".$currentImg."' order by pic_id limit 1"; $result = query($query); $arrImg = mysql_fetch_array($result); $imgNextId = $arrImg['pic_id']; Query for image with the specific album name and pic id greater than existing pic id, sort by pic id and limit 1 , you will get next pic_id, simple enough ? Link to comment https://forums.phpfreaks.com/topic/115404-solved-getting-next-value-of-an-array/#findComment-593281 Share on other sites More sharing options...
wrathican Posted July 18, 2008 Author Share Posted July 18, 2008 thats a very good idea. Thanks Link to comment https://forums.phpfreaks.com/topic/115404-solved-getting-next-value-of-an-array/#findComment-593282 Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 if you got what you want, please mark topic as solved ! Link to comment https://forums.phpfreaks.com/topic/115404-solved-getting-next-value-of-an-array/#findComment-593283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.