Page 1 of 1

pagination

Posted: Tue Sep 06, 2005 10:22 am
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]

Posted: Tue Sep 06, 2005 10:24 am
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

Posted: Tue Sep 06, 2005 10:36 am
by feyd
we're all pretty old bitter men about it. :)

Posted: Tue Sep 06, 2005 10:36 am
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.

Posted: Tue Sep 06, 2005 10:53 am
by patrikG
feyd wrote:we're all pretty old bitter men about it. :)
I'm not that old.

Posted: Tue Sep 06, 2005 5:01 pm
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.

Posted: Wed Sep 07, 2005 4:08 am
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

Posted: Wed Sep 07, 2005 4:55 am
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.

Posted: Wed Sep 07, 2005 5:29 am
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

Posted: Wed Sep 07, 2005 7:02 am
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.

Posted: Wed Sep 07, 2005 7:26 am
by Simplecat
Yes the URL link does display all the expected values but when clicked does not show the required results

Thanks

Posted: Thu Sep 08, 2005 4:59 am
by Simplecat
Hi guys still no luck with this pagination tried several things including all that you have said inyour posts
any ideas