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

pagination

Post by Simplecat »

Pimptastic | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Can someone please help me solve this search form pagination thing. I am willing to pay now I am so desparate. This is one topic that no-one seems to know about. here is my code

There are examples of pagination everywhere on this site why is it so difficult

Here is the form from the html page

<form name ="search" id ="search" action = buying_2.php method=GET>
<p align="left" class="style24">Search >>> </p>
<p align="left">
<span class="style8">&nbsp;Location:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

<select name=location value="<? if(isset($_GET['location'])) echo $_GET['location']; ?>" >
<option value=Canterbury>Canterbury
<option value=Whitstable>Whitstable
<option value=Herne Bay>Herne Bay
<option value=Faversham>Faversham

</select>
</p>
<p align="left"> <span class="style8">Price range:</span>&nbsp;&nbsp;&nbsp; 
<select name=price_range value="<? if(isset($_GET['price_range'])) echo $_GET['price_range']; ?>" >

<option value=&pound;100,000 to &pound;200,000>&pound;100,000 to &pound;200,000
<option value=&pound;200,000 to &pound;300,000>&pound;200,000 to &pound;300,000
<option value=&pound;300,000 to &pound;400,000>&pound;300,000 to &pound;400,000
<option value=&pound;400,000 to &pound;500,000>&pound;400,000 to &pound;500,000
<option value=&pound;500,000 to &pound;600,000>&pound;500,000 to &pound;600,000
<option value=&pound;600,000 to &pound;700,000>&pound;600,000 to &pound;700,000
<option value=&pound;700,000 to &pound;800,000>&pound;700,000 to &pound;800,000
<option value=&pound;800,000 to &pound;900,000>&pound;800,000 to &pound;900,000
<option value=&pound;900,000 to &pound;1000,000>&pound;900,000 to &pound;1000,000
<option value=&pound;1000,000 and Above>1000,000 and Above

</select>
&nbsp;</p>
<p align="left">
<span class="style8">Property type:</span>&nbsp; 
<select name=property_type value="<? if(isset($_GET['property_type'])) echo $_GET['property_type']; ?>"> 
<option value=House>House
<option value=Flat>Flat
<option value=Rented>Rented

</select>
&nbsp;
<input name="Submit" type = submit value = "Search" tabindex="3" >
</p>
<p align="left">&nbsp;</p> 
</span> 
</form></td>


Here is the php page that collects user data from the form on the html page

?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 current page number, use it 
// if not, set one! 

if(!isset($_GET['page'])){ 
$page = 1; 
} else { 
$page = $_GET['page']; 
} 

// Define the number of results per page 
$max_results =4; 

// Figure out the limit for the query based 
// on the current page number. 
$from = (($page * $max_results) - $max_results); 

// Perform MySQL query on only the current page number's results 
$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%' LIMIT $from, $max_results");
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM sellers_properties WHERE location LIKE '$new_location%' AND price_range LIKE '$new_price_range%' AND property_type LIKE '$new_property_type%'"),0); 
?>


<?
if ($row=mysql_fetch_array($result)){
do{

?>

//here is all my formatted data to show results

<?

// 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>";


// Figure out the total number of results in DB: 
//$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM sellers_properties WHERE location LIKE '$new_location%' AND price_range LIKE '$new_price_range%' AND property_type LIKE '$new_property_type%'"),0); 

// Figure out the total number of pages. Always round up using ceil() 
$total_pages = ceil($total_results / $max_results); 

// Build Page Number Hyperlinks 
echo "<center>Select a Page<br />"; 

// Build Previous Link 
if($page > 1){ 
$prev = ($page - 1); 
// echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Previous</a> "; 
echo '<a href="'.$_SERVER['PHP_SELF'].'?page=' .$prev. '&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">prev</a>'; 
//echo'<a href="buying_2.php?page='.$prev.'&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">PREVIOUS</a> '; 


} 



for($i = 1; $i <= $total_pages; $i++){ 
if(($page) == $i){ 
echo "$i "; 
} else { 
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
// echo '<a href="buying_2.php?page='.$i.$new_location, $new_price_range, $new_property_type.'">$i</a>'; 


} 
} 

// Build Next Link 
if($page < $total_pages){ 
$next = ($page + 1); 
// echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>"; 
echo '<a href="'.$_SERVER['PHP_SELF'].'?page=' .$next. '&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">next</a>'; 
//echo'<a href="buying_2.php?page='.$prev.'&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">NEXT</a> '; 
} 
echo "</center>"; 
?> 

Hope this helps

Your help is much appreciated

Simplecat

[color=red][b]Pimptastic[/b] | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

i don't have your answer yet, but i would be able to give you one probably if you where to use the

Code: Select all

tags. edit your post before fyed sees, hes a angry old bitter man about these things
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

we're all pretty old bitter men about it. :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

It would help us to help you if you posted a description of the actual problem. I would think from a brief look you are missing an OFFSET [var] in your SQL.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

feyd wrote:we're all pretty old bitter men about it. :)
I'm not that old.
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 »

Because it hurts me to see people so desparate that they want to pay for help, here's the code in a prettier format:
Simplecat wrote:

Code: Select all

<form name ="search" id ="search" action = buying_2.php method=GET>
<p align="left" class="style24">Search >>> </p>
<p align="left">
<span class="style8">&nbsp;Location:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<select name=location value="<? if(isset($_GET['location'])) echo $_GET['location']; ?>" >
<option value=Canterbury>Canterbury
<option value=Whitstable>Whitstable
<option value=Herne Bay>Herne Bay
<option value=Faversham>Faversham

</select>
</p>
<p align="left"> <span class="style8">Price range:</span>&nbsp;&nbsp;&nbsp;
<select name=price_range value="<? if(isset($_GET['price_range'])) echo $_GET['price_range']; ?>" >

<option value=&pound;100,000 to &pound;200,000>&pound;100,000 to &pound;200,000
<option value=&pound;200,000 to &pound;300,000>&pound;200,000 to &pound;300,000
<option value=&pound;300,000 to &pound;400,000>&pound;300,000 to &pound;400,000
<option value=&pound;400,000 to &pound;500,000>&pound;400,000 to &pound;500,000
<option value=&pound;500,000 to &pound;600,000>&pound;500,000 to &pound;600,000
<option value=&pound;600,000 to &pound;700,000>&pound;600,000 to &pound;700,000
<option value=&pound;700,000 to &pound;800,000>&pound;700,000 to &pound;800,000
<option value=&pound;800,000 to &pound;900,000>&pound;800,000 to &pound;900,000
<option value=&pound;900,000 to &pound;1000,000>&pound;900,000 to &pound;1000,000
<option value=&pound;1000,000 and Above>1000,000 and Above

</select>
&nbsp;</p>
<p align="left">
<span class="style8">Property type:</span>&nbsp;
<select name=property_type value="<? if(isset($_GET['property_type'])) echo $_GET['property_type']; ?>">
<option value=House>House
<option value=Flat>Flat
<option value=Rented>Rented

</select>
&nbsp;
<input name="Submit" type = submit value = "Search" tabindex="3" >
</p>
<p align="left">&nbsp;</p>
</span>
</form></td>
Here is the php page that collects user data from the form on the html page

Code: Select all

<?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 current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

// Define the number of results per page
$max_results =4;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results
$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%' LIMIT $from, $max_results");
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM sellers_properties WHERE location LIKE '$new_location%' AND price_range LIKE '$new_price_range%' AND property_type LIKE '$new_property_type%'"),0);
?>


<?
if ($row=mysql_fetch_array($result)){
do{

?>

//here is all my formatted data to show results

<?

// 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>";


// Figure out the total number of results in DB:
//$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM sellers_properties WHERE location LIKE '$new_location%' AND price_range LIKE '$new_price_range%' AND property_type LIKE '$new_property_type%'"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
$prev = ($page - 1);
// echo "<a href="".$_SERVER['PHP_SELF']."?page=$prev">Previous</a> ";
echo '<a href="'.$_SERVER['PHP_SELF'].'?page=' .$prev. '&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">prev</a>';
//echo'<a href="buying_2.php?page='.$prev.'&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">PREVIOUS</a> ';


}



for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href="".$_SERVER['PHP_SELF']."?page=$i">$i</a> ";
// echo '<a href="buying_2.php?page='.$i.$new_location, $new_price_range, $new_property_type.'">$i</a>';


}
}

// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
// echo "<a href="".$_SERVER['PHP_SELF']."?page=$next">Next>></a>";
echo '<a href="'.$_SERVER['PHP_SELF'].'?page=' .$next. '&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">next</a>';
//echo'<a href="buying_2.php?page='.$prev.'&new_location='.$_GET['location'].'&new_price_range='.$_GET['price_range'].'&new_property_type='.$_GET['property_type'].'">NEXT</a> ';
}
echo "</center>";
?>

We're still going to need some more info as to what the problem is though.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Simplecat
Forum Newbie
Posts: 11
Joined: Wed Aug 24, 2005 6:27 am

Post by Simplecat »

Ahh that code looks so nice.. Sorry for the original I know its harder to understand
I really apreciate your help guys...

What more info do you need from me to help solve this?

Regards
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

What the actual problem is would be of help. Is it that you have only the first X entries no matter which page you are on ?

If this is the case you need to use the SQL OFFSET parameter. If limit is ten the sql for the first command would be...

Code: Select all

SELECT * FROM tablename LIMIT 10
For the second page

Code: Select all

SELECT * FROM tabletable LIMIT 10 OFFSET 10;


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

Post by Simplecat »

Ok Sorry I was not specific

After the user has made thier choices from the form the first 4 results are shown from the query no problem. But when I click on the next link the original users criteria is lost and the links just show the unfiltered rows from the database that should not be in the query
Also the last two rows on page one are shown on page two... maybe thats to do with the offset thing?

Hope this helps

thanks for your help
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

What does the url link show... Does it show all the expected variable values ?

I would guess it doesn't because you have special characters in the mix. You probably have to change the creation to...

echo '<a href="'.$_SERVER['PHP_SELF'].'?page=' .$next. '&new_location='.urlencode($_GET['location']).'&new_price_range='.urlencode($_GET['price_range']).'&new_property_type='.urlencode($_GET['property_type']).'">next</a>';

In truth I would change the option lists to use arrays and array values (getting rid of the need for urlencode) e.g...

Code: Select all

$optionlist=array("&pound;100,000 to &pound;200,000","&pound;200,000 to &pound;300,000","&pound;400,000 to &pound;500,000",
"&pound;500,000 to &pound;600,000","&pound;600,000 to &pound;700,000","&pound;700,000 to &pound;800,000",
"&pound;800,000 to &pound;900,000","&pound;900,000 to &pound;1000,000","&pound;1000,000 and Above");

Code: Select all

<option value=0>&pound;100,000 to &pound;200,000</option>
<option value=1>&pound;200,000 to &pound;300,000</option>
<option value=2>&pound;300,000 to &pound;400,000</option>
<option value=3>&pound;400,000 to &pound;500,000</option>
<option value=4>&pound;500,000 to &pound;600,000</option>
<option value=5>&pound;600,000 to &pound;700,000</option>
<option value=6>&pound;700,000 to &pound;800,000</option>
<option value=7>&pound;800,000 to &pound;900,000</option>
<option value=8>&pound;900,000 to &pound;1000,000</option>
<option value=9>1000,000 and Above </option>
You could actually build the option list from array using a foreach loop and even get information from a database as to which options exist.

One final possible change is I would validate the $_GET values once and store them in variables (ensure they are numeric for instance using floor($_GET['value']). These variables would then be used in all subsequent places. You should never trust any passed parameters as they could lead to SQL injection where people find out all the details of your database and can do things like modify information and delete things.
User avatar
Simplecat
Forum Newbie
Posts: 11
Joined: Wed Aug 24, 2005 6:27 am

Post by Simplecat »

Yes the URL link does display all the expected values but when clicked does not show the required results

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

Post by Simplecat »

Hi guys still no luck with this pagination tried several things including all that you have said inyour posts
any ideas
Post Reply