Jump to content

[SOLVED] getting next value of an array


wrathican

Recommended Posts

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

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;

}

?>

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 ? ;)

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.