simple previous / next code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

simple previous / next code

Post 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>";
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: simple previous / next code

Post 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>";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: simple previous / next code

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: simple previous / next code

Post 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
   }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply