Navagation Script??

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
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Navagation Script??

Post 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?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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;
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:

Post 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?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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;
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

This script is perfect ( I had already discovered the bug), thank you very much. :D
Post Reply