PHP PAGINATION

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
User avatar
Simplecat
Forum Newbie
Posts: 11
Joined: Wed Aug 24, 2005 6:27 am

PHP PAGINATION

Post by Simplecat »

Hi

I am having real trouble with this pagination. Excuse my ignorence but I cant seem to grasp the concept. Do I need to create the html pages that the user will view the results on? here is what I have so far

?php
include("dbinfo_2.inc.php");



// Connect to contact database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// create variables to hold users input from form
$new_location = $_GET['location'];
$new_price_range = $_GET['price_range'];
$new_property_type = $_GET['property_type'];

if ($new_location=="")
{
$new_location='%';
}

if ($new_price_range=="")
{
$new_price_range='%';
}

if ($new_property_type=="")
{
$new_property_type='%';
}
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}

// Query both tables with UNION
$query = ("SELECT count(*) FROM sellers_properties WHERE location LIKE '$new_location%' AND price_range LIKE '$new_price_range%' AND property_type LIKE '$new_property_type%'");
$result = mysql_query ("SELECT * FROM sellers_properties WHERE location LIKE '$new_location%' AND price_range LIKE '$new_price_range%' AND property_type LIKE '$new_property_type%' ");

$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 2;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno < 1) {
$pageno = 1;
} elseif ($pageno > $lastpage) {
$pageno = $lastpage;
}
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM sellers_properties $limit";
//$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);

if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
}
echo " ( Page $pageno of $lastpage ) ";
if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
}


?>






&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><a href="/buying_2.htm" class="style136">Search
again</a> <span class="style1"><span class="style14"><br>
</span></span></div></td>
<td width="6">&nbsp;</td>
<td width="9">&nbsp;</td>
<td width="159" rowspan="6" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="159" height="364" valign="top"><p><br>
<br>
<img src="file:///C|/KS%20PROPERTY/mortgage_header.jpg" width="159" height="17"><img src="file:///C|/KS%20PROPERTY/mortgage.gif" width="159" height="95"><br>
<br>
<img src="/conveyancing_header.jpg" width="157" height="17"><br>
<img src="/conveyancing.gif" width="159" height="93"></p>
</td>
</tr>
</table></td>
</tr>
<tr>
<td height="26"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td></td>
</tr>
<?
if ($row=mysql_fetch_array($result)){
do{

?>
<tr>
<td height="96"></td>
<td colspan="2" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="2">
<!--DWLayoutTable-->
<tr bgcolor="#AFC0ED">
<td height="18
" colspan="2" valign="top"><strong>Price</strong><span class="style76"><span class="style171"><font size="2" face="Verdana" color="#003366"><font size="2" >&nbsp;&nbsp;<font size="2" face="Verdana" color="#003366"><font size="2" ><font face="Verdana" color="#003366"><span class="style111"><strong><font size="2" face="Verdana" color="#003366"><font size="2" ><? print $row["Price"]; ?>
<font size="2" face="Verdana" color="#003366"><font size="2" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<? print $row["Category"]; ?>
<font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<? print $row["No_bedrooms"]; ?>
</font></font></font></font></font></font> </font></font> </font></font></strong></span></font></font></font> </font></font></span></span></td>
</tr>
<tr>
<td width="115" rowspan="2" valign="top"><span class="style76"><span class="style171"><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" >
<? $image = $row['picture_name'];
print "<img src='$image'border = 1 >"; ?>
</font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></span></span></td>
<td width="411" height="47" valign="top"><div align="left"><span class="style76"><span class="style171"><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><strong>
<? print $row["Address"]; ?></strong>
<br>
<font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" >
<? print $row["Description"]; ?>
</font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font> </strong></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font>
</font></font></span></span></div></td>
</tr>
<tr>
<td height="23" valign="top"><div align="left"><span class="style76"><span class="style171"><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" ><font size="2" face="Verdana" color="#003366"><font size="2" >
&nbsp;<? print $row["href_words"]; ?>
</font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></span></span></div></td>
</tr>
</table></td>
<td></td>
</tr>
<tr>
<td height="18"></td>
<td colspan="2" valign="top">
<?

// Space between tables
//print("<p>");

}
while ($row = mysql_fetch_array($result));

}
// Let user know if no matching data found
//else {print "Sorry no records found";}
else print "<script>document.location.href='no_results.htm'</script>";






?>

I know this code is messy

Would apreciate any help and bear in mind that I am a newbie

Regards

Simplecat
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Welcome to phpdn, Simplecat,

two things we like around here:
1. we like things without CAPITALISATION
2. we like code being surrounded by

Code: Select all

tags (see the buttons above the formfield in which you type your post).

Why do we like it?

1. Capitalisation is like being shouted at. Don't.
2. It makes your code so much easier to read.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

It seems like you're doing a lot of unnecessary steps.

Here is a simple pagination script that I found somewhere on the net not too long ago

Code: Select all

// Set up pagination variables

$per_page = 25;
$sql_text = "SELECT username FROM users WHERE sex = 'Male' AND activated = 'y' ORDER BY username asc";
if (!$page) { $page = 1; }
$prev_page = $page - 1; 
$next_page = $page + 1; 
$query = mysql_query($sql_text);
$page_start = ($per_page * $page) - $per_page; 
$num_rows = mysql_num_rows($query); 

if ($num_rows <= $per_page) { $num_pages = 1; } ELSE IF 
(($num_rows % $per_page) == 0) { $num_pages = ($num_rows / $per_page); } ELSE {
$num_pages = ($num_rows / $per_page) + 1; }
$num_pages = (int) $num_pages;

if (($page > $num_pages) || ($page < 0))
{ 
	echo "<p class=\"main\"> You have specified an invalid page number</p>"; require 'footer.php'; die();
}

$sql_text = $sql_text . " LIMIT $page_start, $per_page"; 
$query = mysql_query($sql_text);
You now have your $query set up with proper LIMITs
You also have the number of pages ($num_pages)

Then the following script I wrote myself (not extensively tested) this is for displaying PREV/NEXT links

Code: Select all

$limit = $page+10;
if($num_pages >= $limit)
{
	if($page != 1)
	{
	$previouspage = $page - 1;
	$previousstart = $previouspage - 9;
		if($page == 2)
		{
			echo "<small><<</small> <a href=\"$PHP_SELF?s=$s&page=1\">Page 1</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		} ELSE
		{
			if($previousstart <= 0)
			{
				echo"<small><<</small> <a href=\"$PHP_SELF?s=$s&page=1\">Pages 1 - $previouspage</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			} ELSE
			{
				echo "<small><<</small> <a href=\"$PHP_SELF?s=$s&page=$previousstart\">Pages $previousstart - $previouspage</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			}
		}
	}
	for($i = $page; $i<$limit; $i++)
	{
		echo " <a href=\"$PHP_SELF?s=$s&page=$i\">$i</a> ";
	}
	$limit2 = $i+9;
	if($num_pages <= $limit2)
	{
		echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"$PHP_SELEF?s=$s&page=$i\">Pages $i - $num_pages</a> <small>>></small>";
	} ELSE
	{
		echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"$PHP_SELEF?s=$s&page=$i\">Pages $i - $limit2</a> <small>>></small>";
	}
} ELSE
{
	for($i = $page; $i<=$num_pages; $i++)
	{
		echo " <a href=\"$PHP_SELF?s=$s&page=$i\">$i</a> ";
	}
}
?>
Of course you'd have to go through and change the links to suit your needs. Hope this helps in some way.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

User avatar
Simplecat
Forum Newbie
Posts: 11
Joined: Wed Aug 24, 2005 6:27 am

Post by Simplecat »

Thanks Guys for your rapid response

But i really need things spelt out here,, Change what links to suit myself
Post Reply