Web Hosting Talk







View Full Version : "Previous" & "Next" links in php


MGCJerry
11-14-2002, 04:19 AM
I'm working on a PHP-Nuke module and I have run accross a little problem with the "previous" and "next" links in my script.

The addon displays a list of races or civilizations and they are queried by ID's (mysql auto_increment). The problem is, when a race is deleted from the database, the buttons do not avoid any non-existant IDs. How would I make the "Previous" and "Next" links where people can view the next race in the database without the buttons trying to get the script to display non-existant records?

Another problem is if there is only one race with the ID of 1, the previous button links to a non-existant ID which is 0 (zero). You can see what I mean by this here (http://www.2thextreme.org/modules.php?name=Races&file=viewrace&id=1).

I know I'm missing something stupidly simple, but I'm stuck. Here is my code snippets...



/* The $count variable is the result from the mysql_num_rows() function */

$prev = $id-1;
$next = $id+1;

if ($count < $next){
echo "<table align=\"center\" width=\"415\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td align=center>
<a href=\"modules.php?name=$module_name&amp;file=viewrace&amp;id=$prev\">&lt;&lt; Previous</a>&nbsp;|&nbsp;<a href=\"modules.php?name=$module_name\">Home</a>&nbsp;|&nbsp;Next &gt;&gt;
</td>
</tr>
</table> ";
} elseif ($prev < 1) {
echo "<table align=\"center\" width=\"415\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td align=center>
&lt;&lt; Previous&nbsp;|&nbsp;<a href=\"modules.php?name=$module_name\">Home</a>&nbsp;|&nbsp;<a href=\"modules.php?name=$module_name&amp;file=viewrace&amp;id=$next\">Next &gt;&gt;</a>
</td>
</tr>
</table>";
} else {
echo "<table align=\"center\" width=\"415\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td align=center>
<a href=\"modules.php?name=$module_name&amp;file=viewrace&amp;id=$prev\">&lt;&lt; Previous</a>&nbsp;|&nbsp;<a href=\"modules.php?name=$module_name\">Home</a>&nbsp;|&nbsp;<a href=\"modules.php?name=$module_name&amp;file=viewrace&amp;id=$next\">Next &gt;&gt;</a>
</td>
</tr>
</table><br>";
}



Thanks for any pointers. :)

LocalHoster
11-15-2002, 09:21 AM
ok, im assuming your using a mysql database.

The first thing i would do would be to a mysql query to get all the available race numbers and add them into an array. and then that will enable you to output the data easier :)


$idlist = array();
$query = mysql_query("SELECT id FROM races");
while($row = mysql_fetch_array($query)){
array_push($idlist, $row["id"]);
};

MGCJerry
11-15-2002, 10:33 AM
Yes, I'm using a MySQL database. Thanks for the snippet...

All I gotta figure out now is how to assign the correct links based on which ID is being viewed.

Novicane
11-16-2002, 03:51 AM
This article here helped me to complete this and gave me a very good understanding for using Classes in PHP. I recomend everyone give this a read just for the heck of it. Although it seems his support materials aren't up anymore, you can easily go through (please no copy and paste at least rewrite it @_@) and rewrite the script that he has explained. This thing works like a charm and is one heck of a time saver when working on multiple sites that require this sort of function. Not only is it nice to call this from any site, but its also template oriented and can easily be formed to fit any site.

http://www.devarticles.com/art/1/110/2

Cant rave enough about this article, hope it helps, I'm sure it will !

whatever
11-17-2002, 08:48 AM
This is from daydreamgraphics.com:

Ever wanted the next, previous link to be generated dynamically? This is the tutorial to read. I'll teach you how to achieve this goal using some quick and 'dirty' code.

PHP Syntax

There are 2 parts for this code :

Code Part A :

// dynamic navigation variables
$rows_per_page=10;
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);
mysql_free_result($result);

if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
$q .= "LIMIT $start, $rows_per_page";
$result= mysql_db_query($dbase, $q, $connection) or die
("Could not execute query : $q." . mysql_error());

Code Part B :

// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}

// page numbering links now

for ($i = 0; $i < $pages; $i++) {
$url = "$PHP_SELF?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}

if ($screen < $pages) {
$j = $screen + 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Next</a>";
}

Explaination of Syntax

$rows_per_page=10;
Here we declare that we want 10 records in a page. You can set any number you wish here.

$total_records=mysql_num_rows($result);
This one is straight forward, we are declaring a variable $total_records and that have to be the number of rows returned by the query.

$pages = ceil($total_records / $rows_per_page);
Here we set a mathematic rule in which $pages is $total_records divided by $rows_per_page. Then we round up the remainder. This is pretty much common sense since you want to calculate how many pages you want php to generate. After the division, the remainder should made up the last page so you round it up with ceil function.

mysql_free_result($result);
This one is a standard practice that we free the result variable. Optional but good programming practice to free the memory.

if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
Here we tell php that if $screen variable is not set, then $screen will be 0. $start is our first page and is declared as $screen multiply by $rows_per_page. So, you can manually calculate that the first page will be $start=0 since $screen has to be 0.

$q .= "LIMIT $start, $rows_per_page";
$result= mysql_db_query($dbase, $q, $connection) or die
("Could not execute query : $q." . mysql_error());
The dot (.) after $q actually means that the string will be added to the previous query. If you named your sql query, $query, then you have to change this line to $query.. Here, we tell MySQL that we are adding a 2nd portion to the previous query we set. The previous query of course will be what your script does. The 2nd line of course tells MySQL to execute the query or output an error message if failed.

if ($screen > 0) {
$j = $screen - 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}
This tells php, that if $screen is more than 0 (ie, visitor is not reading the first page), a previous link is to be generated.

for ($i = 0; $i < $pages; $i++) {
$url = "$PHP_SELF?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}
This tells php that the visitor is reading the $i page, and the code will loop until the maximum pages, $pages is reached. This will output the number of pages and their links.

if ($screen < $pages-1) {
$j = $screen + 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Next</a>";
}
This will output the next link as long as the $screen is not on the last page as declared by $pages.

Usage

Basically, you put in Code Part A after your initial query, and Code Part B where you want the navigation to appear.

Conclusion

This tutorial is a little complex in code, but this is one of the easiest method to output the dynamic navigation. There are more efficiency ways to do the same thing but they are much more complex to code.

MGCJerry
11-17-2002, 03:05 PM
Ohh... I forgot about this post :o.

I managed to get this working by taking some existing code that I found and hacking the crap out of it.

Thanks for the code snippets and the link, I may need them in the future. :)

heres my code:

// Naviagtion code
function getPos($res,$id) {
$i=0;
while ($row=mysql_fetch_array($res)) {
if ($row[ID] == $id)
return $i;
$i++;
}
}

$sql = "SELECT ID FROM ".$prefix."_races order by ID";
$res = mysql_query($sql);
$max = mysql_num_rows($res);
$ind = getPos($res, $id);
if ($ind>0) {
$prev = mysql_result($res, $ind-1, 0);
// $pname = mysql_result($res, $ind-1, 1); ** Future Use for sort by name instead ID
}
if ($ind<$max-1) {
$next = mysql_result($res, $ind+1, 0);
// $nname = mysql_result($res, $ind+1, 1); ** Future Use for sort by name instead ID
}


// Links for navigation
$navout = '<p align="center">';
if ($prev) {
$navout .= '<a class="content" href="modules.php?name='.$module_name.'&amp;file=viewrace&amp;id='.$prev.'">&lt;&lt; Previous</a><font class="content"> | <a href="modules.php?name='.$module_name.'">Home</a> | </font>';
} else {
$navout .= '<font class="content">&lt;&lt; Previous</font> <font class="content"> | <a href="modules.php?name='.$module_name.'">Home</a> | </font>';
}

if ($next) {
$navout .= '<a class="content" href="modules.php?name='.$module_name.'&amp;file=viewrace&amp;id='.$next.'">Next &gt;&gt;</a>';
} else {
$navout .= '<font class="content">Next &gt;&gt;</font>';
}

$navout .= '</p>';

echo $navout;


You can see the code in action here (http://www.2thextreme.org/modules.php?name=Races). It is my first PHP-Nuke module that I wrote code for.