Jump to content

Dynamic Link Creation


MinusMyThoughts
 Share

Recommended Posts

if i want to add a full-view option to my photo albums with a "previous" and "next" button, i need to first figure out the photoID of the previous and next photos......here's where i'm at right now: using $_GET variables, i'm displaying the photo as the user clicks on it (i.e. /photo.php?id=65). that's working fine, but what i need to be able to do is pull the ID out of the database for the previous and next entries, which may not be sequential......i'll be sorting by the albumID, which isn't a problem, and then ordering the table by the photoID ASC. does anyone know a trick to get the ID of the entries to add to a link?...i thought about putting this in SQL, but i figured that it belonged here since all the code will be in PHP. hope that wasn't faux pas...love,jason

Link to comment
Share on other sites

i came up with something that's working. as usual, i'm up for suggestions to improve on this:

			$prevQ = "SELECT * FROM aKa_photo WHERE photoID < '$photoID' 				AND albumID = '$albumID' ORDER BY photoID ASC LIMIT 1";			$prevR = mysql_query($prevQ);			$prev = mysql_fetch_array($prevR);			if($prev['photoID'] != '')			  {			  $prevPic = 'pictures.php?id=' . $prev['photoID'];			  }			else			  {			  $prevPic = 'pictures.php?album=' . $albumID;			  }			$nextQ = "SELECT * FROM aKa_photo WHERE photoID > '$photoID' 				AND albumID = '$albumID' ORDER BY photoID ASC LIMIT 1";			$nextR = mysql_query($nextQ);			$next = mysql_fetch_array($nextR);			if($next['photoID'] != '')			  {			  $nextPic = 'pictures.php?id=' . $next['photoID'];			  }			else			  {			  $nextPic = 'pictures.php?album=' . $albumID;			  }

love,jason

Link to comment
Share on other sites

  • 2 weeks later...

Yeah, that will work. But the first one needs to be ordered DESC instead of ASC. I was also going to say you could loop through the result set and do it that way. Something like this:

$result = mysql_query("SELECT photoID FROM aKa_photo ORDER BY photoID ASC");$prev = $cur = $next = 0;$done = false;while (!$done){  if (!($row = mysql_fetch_assoc($result))	$done = true;  else  {	if ($row['photoID'] == $photoID)	{	  $prev = $cur;	  if ($row = mysql_fetch_assoc($result))		$next = $row['photoID'];	  $done = true;	}	else	  $cur = $row['photoID'];  }}

Then if $prev or $next is not equal to zero, it found them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...