Page 1 of 1

Navagation Script??

Posted: Sat Jul 13, 2002 8:16 pm
by chris12295
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?

Posted: Sat Jul 13, 2002 9:49 pm
by gnu2php
I have some sample code that might be useful.

Let's say you have an array:

Code: Select all

$array = array("this", "array", "contains", "the", "data", "you", "want", "to", "display");
And the following settings:

Code: Select all

$start = 0;
if (isset($_GET&#123;'start'&#125;)) $start = $_GET&#123;'start'&#125;;

$MAX_ITEMS = 3; // We'll use 3 for this example

$array_count = count($array); // The number of items in the array
You can print the data that is in the array, based on the above settings, like this:

Code: Select all

$num_items = ($array_count - $start);

if ($num_items > $MAX_ITEMS) $num_items = $MAX_ITEMS;


for ($x = $start; $x < ($num_items + $start); $x++)
&#123;
	print "$array&#1111;$x]<br>\n";
&#125;
Then for the "prev" button, use:

Code: Select all

if ($start > ($MAX_ITEMS - 1))
&#123;
	print "<a href="";
	print $_SERVER&#1111;'PHP_SELF'];
	print "?start=".($start - $MAX_ITEMS)."">Prev</a> \n";
&#125;
For the "next" button, use:

Code: Select all

if ($start < ($array_count - $MAX_ITEMS))  // Fixed (see below)
&#123;
	print "<a href="";
	print $_SERVER&#1111;'PHP_SELF'];
	print "?start=".($start + $MAX_ITEMS)."">Next</a> \n";
&#125;
Then you can print the digits like this:

Code: Select all

if ($array_count > $MAX_ITEMS)
&#123;
	for ($x = 0; $x < $array_count; $x += $MAX_ITEMS)
	&#123;
		$digit = (($x / $MAX_ITEMS) + 1);

		if ($x != $start)
		&#123;
			print "<a href="";
			print $_SERVER&#1111;'PHP_SELF'];
			print "?start=$x">$digit</a> ";
		&#125;
		else print "$digit ";
	&#125;
&#125;

Posted: Sun Jul 14, 2002 12:25 pm
by chris12295
ok the array works, i haven't tested on a databse query, but how can i limit the number of digits to display only 10 then if there are more than 10 pages go from 10 to 20 when they get to page 10?

Posted: Sun Jul 14, 2002 10:26 pm
by gnu2php
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:

Code: Select all

if ($array_count > $MAX_ITEMS)
&#123;
	$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)
	&#123;
		if ($current_digit > ($MAX_DIGITS / 2))
		&#123;
			$digit_start = $current_digit - ($MAX_DIGITS / 2);

			if ($digit_start > ($total_digits - $MAX_DIGITS))
				$digit_start = ($total_digits - $MAX_DIGITS);
		&#125;

		$digit_end = $digit_start + ($MAX_DIGITS - 1);
	&#125;

	// Print the digits
	for ($x = $digit_start; $x <= $digit_end; $x++)
	&#123;
		if ($x != $current_digit)
		&#123;
		// 0-based, so ($x + 1) is used
			print "<a href="";
			print $_SERVER&#1111;'PHP_SELF'];
			print "?start=".($x * $MAX_ITEMS);
			print "">".($x + 1)."</a> ";
		&#125;
		else print ($x + 1)." ";
	&#125;
&#125;
Also, to prevent invalid entries, you'll need to put this after $MAX_ITEMS is defined:

Code: Select all

$start -= ($start % $MAX_ITEMS);
Finally, there's a bug in the code I gave you. Use this for the "next" button, instead:

Code: Select all

if ($start < ($array_count - $MAX_ITEMS))
&#123;
   print "<a href="";
   print $_SERVER&#1111;'PHP_SELF'];
   print "?start=".($start + $MAX_ITEMS)."">Next</a> \n";
&#125;

Posted: Sun Jul 14, 2002 10:41 pm
by chris12295
This script is perfect ( I had already discovered the bug), thank you very much. :D