Navagation Script??
Posted: Sat Jul 13, 2002 8:16 pm
I need to make a search script with a navigation <PREV 123 NEXT> at the bottom and display 20 resultss per page, what is the best way to do this using the least amont of variables?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$array = array("this", "array", "contains", "the", "data", "you", "want", "to", "display");Code: Select all
$start = 0;
if (isset($_GET{'start'})) $start = $_GET{'start'};
$MAX_ITEMS = 3; // We'll use 3 for this example
$array_count = count($array); // The number of items in the arrayCode: Select all
$num_items = ($array_count - $start);
if ($num_items > $MAX_ITEMS) $num_items = $MAX_ITEMS;
for ($x = $start; $x < ($num_items + $start); $x++)
{
print "$arrayї$x]<br>\n";
}Code: Select all
if ($start > ($MAX_ITEMS - 1))
{
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($start - $MAX_ITEMS)."">Prev</a> \n";
}Code: Select all
if ($start < ($array_count - $MAX_ITEMS)) // Fixed (see below)
{
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($start + $MAX_ITEMS)."">Next</a> \n";
}Code: Select all
if ($array_count > $MAX_ITEMS)
{
for ($x = 0; $x < $array_count; $x += $MAX_ITEMS)
{
$digit = (($x / $MAX_ITEMS) + 1);
if ($x != $start)
{
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=$x">$digit</a> ";
}
else print "$digit ";
}
}Code: Select all
if ($array_count > $MAX_ITEMS)
{
$MAX_DIGITS = 10;
$total_digits = (int)((int)($array_count - 1) / $MAX_ITEMS) + 1;
// Uses 0-based index (ie: the first index is 0):
$current_digit = ($start / $MAX_ITEMS);
// Get the first & last digits (0-based)
$digit_start = 0;
$digit_end = ($total_digits - 1);
if ($total_digits > $MAX_DIGITS)
{
if ($current_digit > ($MAX_DIGITS / 2))
{
$digit_start = $current_digit - ($MAX_DIGITS / 2);
if ($digit_start > ($total_digits - $MAX_DIGITS))
$digit_start = ($total_digits - $MAX_DIGITS);
}
$digit_end = $digit_start + ($MAX_DIGITS - 1);
}
// Print the digits
for ($x = $digit_start; $x <= $digit_end; $x++)
{
if ($x != $current_digit)
{
// 0-based, so ($x + 1) is used
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($x * $MAX_ITEMS);
print "">".($x + 1)."</a> ";
}
else print ($x + 1)." ";
}
}Code: Select all
$start -= ($start % $MAX_ITEMS);Code: Select all
if ($start < ($array_count - $MAX_ITEMS))
{
print "<a href="";
print $_SERVERї'PHP_SELF'];
print "?start=".($start + $MAX_ITEMS)."">Next</a> \n";
}