Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple page result in PHP

Status
Not open for further replies.

elck

Programmer
Apr 19, 2004
176
NL
I have the feeling that some parts of the next code are not particulaire well written or thought out.
Specialy where I calculate the total # of records.
Can you help me make this better code?

I have not tested it yet, but it demonstrates the general idea. No garantees it will work as is.

Code:
<?php
import_request_variables("GP");
$records_per_page=50;
if (!$pag) 
	{$limit=" limit " . $records_per_page;}
else 
	{$limit=" limit " . ($page * $records_per_page) . "," . $records_per_page;}

$query="SELECT * FROM `table` WHERE `forum`='$forum' ";
$result=mysql_query($query);
$total=msql_num_rows($result) ;
$pages=ceil($total / $records_per_page);

$query="SELECT * FROM `table` WHERE `forum`='$forum' " . $limit;
$result=mysql_query($query);

while($line=mysql_fetch_array($result,MYSQL_ASSOC))
	{
		/* list entries */
		}

if ($total>$records_per_page) {
for ($i=1;$i<$pages;$i++) {
	print "<a href='thisfile.php?forum=$forum&pag=$i'>-$i-</a>&nbsp;"; }}
?>
 
You're displaying a page of 50 entries, and providing links to the other pages, is that right? In that case, your first query only needs to get the number of records; you could replace it with:
[tt]
SELECT COUNT(*) numrecs FROM table WHERE forum='$forum'
[/tt]
 
Right! That's a good improvement, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top