Page 1 of 1

simple previous / next code

Posted: Fri Jun 18, 2010 1:31 pm
by rhecker
I have image galleries divided into categories and have while statements to iterate through the result set. But when the visitor hits the last image in the set, I want the "NEXT" option to change to a "RETURN" which will return to the list of categories. If I could use ELSE with WHILE, I could do this, but ELSE doesn't work with WHILE. So I need to tell the code what to do when the loop ends.

Here is the code, such as it is. The bottom IF statement doesn't work, but it tells you what I am trying to do.

Code: Select all

while ($next1 = mysql_fetch_assoc($next)){
$n = $next1[portfolio_id];
$cat_id=$next1[category_id];
echo "<a href='index.php?id=$n&c_id=$cat_id'>NEXT</a>";
}
if (!isset($next1)){
echo "<a href='index.php?cat_id=$c_id'>RETURN</a>";
}

Re: simple previous / next code

Posted: Fri Jun 18, 2010 1:37 pm
by AbraCadaver
You know when the loop ends, after the }, or I totally misunderstand, so why not this:

Code: Select all

while ($next1 = mysql_fetch_assoc($next)){
   $n = $next1[portfolio_id];
   $cat_id=$next1[category_id];
   echo "<a href='index.php?id=$n&c_id=$cat_id'>NEXT</a>";
}
echo "<a href='index.php?cat_id=$c_id'>RETURN</a>";

Re: simple previous / next code

Posted: Fri Jun 18, 2010 3:15 pm
by rhecker
Well, you misunderstood, but only because I wasn't clear. I want the RETURN link to show only when the last item in the recordset is reached, which is where NEXT doesn't apply.

Re: simple previous / next code

Posted: Fri Jun 18, 2010 3:26 pm
by AbraCadaver
FYI, you need to quote your array keys:

Code: Select all

$i = 0;
$count = mysql_num_rows($next);

while ($next1 = mysql_fetch_assoc($next)) {
   $i++;
   $n = $next1['portfolio_id'];
   $cat_id = $next1['category_id'];

   if ($i == $count) {
      // RETURN
   } else {
      // NEXT
   }
}