Page Numbering

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
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Page Numbering

Post by mccommunity »

I am pulling a list of about 10,000 items. There are 25 items to display on the page at a time. I would like to ad page numbering at the top. I am able to add page numbering at the top fine but if there are 100 pages it displays page 1 - page 100 which takes up the whole screeen. I would like for whever it is over ten to display page 20 page 30 page 40 etc. I am not sure how to do this though. Can anyone help? Here is my code I am using:

Code: Select all

// Set up pagesets for all of the records.

$now_date = date("m-d-Y");
$query = "select count(1) from inventory where auction='$SESSIONїauction]' and consignor='$SESSIONїconsignor]' AND field1 >= '$now_date'";

if (!$c){

	$c = pg_connect($runlistdb);

}

$r = pg_exec($c,$query);

$numrecords = pg_result($r,0,0);


$pages = $numrecords/$limit;

echo '<p class="nav"><center><table><tr>';

for ($p=0;$p<$pages;$p++)&#123;

	$newoffset = $p*$limit;

	if ($p > 0)&#123;

		echo '  ';

	&#125;
       
	echo "<td ><center><a href="$PHP_SELF?orderby=$orderby&sort=$sort&limit=$limit&offset=$newoffset">Page " . ($p+1) . '</a></center></td> ';

&#125;

echo '</tr></table></td></tr></table>';
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

I use this as a page navigator:

Code: Select all

Function Page_Navigator ($Offset, $DataBase, $Link, $Limit, $Show)
	&#123;
		$Value = $Offset;
		$result= MySQLQuery("SELECT * FROM $DataBase", "Sorry, A is temporarily unavailable.");
		$Check2= mysql_num_rows($result);
		$Pages= ceil($Check2/$Limit);
		$LastPageOffset= ($Pages-1) * $Limit;
		$NumHundreds= floor($Check2/(100*$Limit));

		$ThisPage= ($Offset/$Limit) + 1;
		$Hundreds= floor($ThisPage / 100);
		$Temp= $ThisPage % 100;
		$Tens= floor($Temp / 10);
		$Rest= $Temp % 10;

		$Check= $Check2 - $Value - $Limit;

		echo "<TABLE>\n";
		echo "<TR>\n";
		echo "<TD WIDTH=20 HEIGHT=45> &nbsp; \n";
		echo "</TD><TD WIDTH=80>\n";
		if ($Offset != 0)
		&#123;
			$Offset= $Value - $Limit;
			echo "<A HREF="" . $Link . ".php?Offset=" . $Offset . """;
			echo "onmouseover='switchImagesBack("on")' onmouseout='switchImagesBack("off")'>\n";
			echo "<IMG NAME="imagenameBack" SRC="/Pictures/Back.jpg"></A>\n";
		&#125;
		echo "</TD><TD ALIGN=CENTER>\n";
		if ($Show == "1")
		&#123;
			echo "<FONT COLOR=DARKRED><B>Page " . $ThisPage . " of " . $Pages . "</B></FONT><BR>\n";
			## Show first line in Navigator.
			## Calculate the start and end values first. Then Print and print current in Black.

			$StartValue= (($Hundreds * 100) + ($Tens * 10) + 1);
			$EndValue= (($Hundreds * 100) + ($Tens * 10) + 10);

			echo "<A HREF="" . $Link . ".php?Offset=0"><SMALL><SMALL>First</SMALL></SMALL></A>\n";

			for ($i=$StartValue; $i <= $EndValue; $i++)
			&#123;
				$Offset= ($i - 1) * $Limit;
				if ($Value == $Offset)
				&#123;
					echo "<FONT COLOR="BLACK"><B> " . $i . "</B>\n";
				&#125;
				else if ($Offset <= $Check2)
				&#123;
					echo "<A HREF="" . $Link . ".php?Offset=" . $Offset . ""><SMALL><SMALL> " . $i . "</SMALL></SMALL></A>\n";
				&#125;
			&#125;

			echo "<A HREF="" . $Link . ".php?Offset=" . $LastPageOffset . ""><SMALL><SMALL>Last</SMALL></SMALL></A>\n";
			echo "<BR>";

			for ($i=1; $i <= 10; $i++)
			&#123;
				$Reali = ($i * 10) + ($Hundreds * 100);
				$Offset= ($Reali - 1) * $Limit;
				if ($Value == $Offset)
				&#123;
					echo "<FONT COLOR="BLACK"><B> " . $Reali . "</B>\n";
				&#125;
				else if ($Offset <= $Check2)
				&#123;
					echo "<A HREF="" . $Link . ".php?Offset=" . $Offset . ""><SMALL><SMALL> " . $Reali . "</SMALL></SMALL></A>\n";
				&#125;
			&#125;
			
			echo "<BR>";

			for ($i=1; $i <= $NumHundreds; $i++)
			&#123;
				$Reali = $i * 100;
				$Offset= ($Reali - 1) * $Limit;
				if ($Value == $Offset)
				&#123;
					echo "<FONT COLOR="BLACK"><B> " . $Reali . "</B>\n";
				&#125;
				else
				&#123;
					echo "<A HREF="" . $Link . ".php?Offset=" . $Offset . ""><SMALL><SMALL> " . $Reali . "</SMALL></SMALL></A>\n";
				&#125;
			&#125;
		&#125;
		else
		&#123;
			echo "&nbsp;\n";
		&#125;
		echo "</TD><TD WIDTH=80 ALIGN=RIGHT>\n";
		if ($Check > 0)
		&#123;
			$Offset= $Value + $Limit;
			echo "<A HREF="" . $Link . ".php?Offset=" . $Offset . """;
			echo "onmouseover='switchImagesForward("on")' onmouseout='switchImagesForward("off")'>\n";
			echo "<IMG NAME="imagenameForward" SRC="/Pictures/Forward.jpg"></A>\n";
		&#125;
		echo "</TD><TD WIDTH=20> &nbsp; \n";
		echo "</TD></TR>\n";
		echo "</TABLE>\n";
	&#125;
It selects the number of lines from a database, the limit is the number of items per page, link is the link to the page.

If you have more questions just lemme know.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, you can find which page you're currently on, and you can output links to arbitrary pages. Those are the only two stumbling blocks I can think of. Why don't you just use the current offset and display links from $current_offset - 10 to $current_offset + 10.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply