Navagation Script??
Moderator: General Moderators
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
Navagation Script??
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?
I have some sample code that might be useful.
Let's say you have an array:
And the following settings:
You can print the data that is in the array, based on the above settings, like this:
Then for the "prev" button, use:
For the "next" button, use:
Then you can print the digits like this:
Let's say you have an array:
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 ";
}
}
Last edited by gnu2php on Fri Jul 19, 2002 11:23 pm, edited 1 time in total.
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
You could try the "Google" method. The following will scroll the digits when you go past "6"--so that the "marker" will be in the center:
Also, to prevent invalid entries, you'll need to put this after $MAX_ITEMS is defined:
Finally, there's a bug in the code I gave you. Use this for the "next" button, instead:
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";
}-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact: