Pagination problem. Only 1st page of results is displayed.
Posted: Sat Oct 23, 2004 9:45 am
I have been unable to find the answer to my pagination problem despite reading numerous tutorials/forum threads.
I have a search page (price-comparison-search.php) with a form and a search field/list, the field name is (fieldsearch).
I retrieve the fieldsearch on another page (price-comparison.php) using the GET method and change this into a variable ($srch).
I then use the $srch to retrieve the results from my database table (cars).
For some reason, the code only produces the 1st page of 10 results. The next pages only display html but no results.
If I use the code and input information directly without retrieving from a form search field, the code produces all of the paged table results ok.
I hope someone here can help me to solve this problem as I have spent days trying to tweek the code to correct the problem.
Sorry its a bit long but maybe you need to see all the code to see where the problem lies.
I have a search page (price-comparison-search.php) with a form and a search field/list, the field name is (fieldsearch).
I retrieve the fieldsearch on another page (price-comparison.php) using the GET method and change this into a variable ($srch).
I then use the $srch to retrieve the results from my database table (cars).
For some reason, the code only produces the 1st page of 10 results. The next pages only display html but no results.
If I use the code and input information directly without retrieving from a form search field, the code produces all of the paged table results ok.
I hope someone here can help me to solve this problem as I have spent days trying to tweek the code to correct the problem.
Sorry its a bit long but maybe you need to see all the code to see where the problem lies.
Code: Select all
<?php
$srch = $_GET['fieldsearch'];
//Database Connection
include 'dbc.php';
// If current page number, use it if not, set one!
if(!$_GET['page'])$page = 1;
else $page = $_GET['page'];
// Define the number of results per page
$max_results = 10;
//Max amount of pages
$max_pages=1;
// Figure out the limit for the query based on the current page number.
$from = (($page * $max_results) - $max_results);
//Query
$result = mysql_query("select * from cars where grp='$srch' ORDER BY perweek ASC LIMIT $from, $max_results");
while($r=mysql_fetch_array($result))
{
$comp=$r["company"];
$cargroup=$r["grp"];
$mke=$r["make"];
$modl=$r["model"];
$id=$r["c4m"];
$doors=$r["doors"];
$airc=$r["aircon"];
$rad=$r["radio"];
$powersteering=$r["powersteering"];
$electricwindows=$r["electricwindows"];
$centrallocking=$r["centrallocking"];
$airbag=$r["airbag"];
$absbrakes=$r["absbrakes"];
$mindriverage=$r["mindriverage"];
$childseatamount=$r["childseatamount"];
$childseatperiod=$r["childseatperiod"];
$roofrackamount=$r["roofrackamount"];
$roofrackperiod=$r["roofrackperiod"];
$airportsurchargeamount=$r["airportsurchargeamount"];
$greencardamount=$r["greencardamount"];
$additionaldriveramount=$r["additionaldriveramount"];
$insuranceexcessamount=$r["insuranceexcessamount"];
$insuranceexcesswaiveramount=$r["insuranceexcesswaiveramount"];
$excesswaiverperiod=$r["excesswaiverperiod"];
$curry=$r["currency"];
$pweek=$r["perweek"];
$mnth=$r["month"];
$linkreservationform=$r["linkreservationform"];
$linkgeneralconditions=$r["linkgeneralconditions"];
$fieldlinkcarphotos=$r["linkcarphotos"];
?>
</p>
<table width="610" border="0" align="center" cellpadding="0" cellspacing="0" class="dashedlinetablecellborder">
<tr>
<td width="200" height="150" rowspan="9"> <div align="center"><img name="linkcarphotos" src=<? echo "$fieldlinkcarphotos"?> width="150" height="90" alt="Algarve car hire photo."><br>
</div></td>
<td colspan="2" class="s3dkbluebold"> <div align="left"><span class="s4colourbluenorm">
Group:</span><span class="s3colourbluebold"> (<? echo "$cargroup"?>)</span>
<span class="s4greynorm"><? echo "$comp"?>-<? echo "$id"?> Minimum Driver
Age <? echo "$mindriverage"?></span></div></td>
</tr>
<tr>
<td width="410" colspan="2" class="s4colourblacknorm"> <div align="left"><span class="s3blackbold"><? echo "$mke"?>
<? echo "$modl"?></span> (<? echo "$doors"?> door)</div></td>
</tr>
<tr>
<td colspan="2" class="s4colourrednorm"><div align="left">Options included:
<? echo "$airc"?> <? echo "$rad"?> <? echo "$powersteering"?> <? echo "$electricwindows"?>
<? echo "$centrallocking"?> <? echo "$airbag"?> <? echo "$absbrakes"?></div>
</td>
</tr>
<tr>
<td width="410" colspan="2" class="s4colourgreennorm"> <div align="left"><span class="s4greenbold"><? echo "$mnth"?></span>
price per week: <span class="s4blackbold"><? echo "$curry"?><? echo "$pweek"?></span>
</div></td>
</tr>
<tr>
<td class="s4colourorangenorm"><div align="left">Child Seat: <span class="s4orangebold"><? echo "$childseatamount"?>
<? echo "$childseatperiod"?></span> </div></td>
<td class="s4colourorangenorm"><div align="left">Airport Surcharge: <span class="s4orangebold"><? echo "$airportsurchargeamount"?></span></div></td>
</tr>
<tr>
<td class="s4colourorangenorm"><div align="left">Roof Rack: <span class="s4orangebold"><? echo "$roofrackamount"?>
<? echo "$roofrackperiod"?></span> </div></td>
<td class="s4colourorangenorm"><div align="left">Green Card: <span class="s4orangebold"><? echo "$greencardamount"?></span></div></td>
</tr>
<tr>
<td class="s4colourorangenorm"><div align="left">Insurance Excess: <span class="s4orangebold"><? echo "$insuranceexcessamount"?></span></div></td>
<td class="s4colourorangenorm"><div align="left">Additional Driver: <span class="s4orangebold"><? echo "$additionaldriveramount"?></span></div></td>
</tr>
<tr>
<td colspan="2" class="s4colourorangenorm"> <div align="left">Excess Waiver:
<span class="s4orangebold"><? echo "$insuranceexcesswaiveramount"?> <? echo "$excesswaiverperiod"?></span></div></td>
</tr>
<tr>
<td width="410"><div align="left"><a href="<? echo "$linkreservationform"?>" class="s4orangebold">
CLICK HERE to book online now</a> </div></td>
<td width="410"><div align="left"><a href="<? echo "$linkgeneralconditions"?>" class="s4orangebold">CLICK
HERE for the General Conditions</a></div></td>
</tr>
<tr>
<td> </td>
<td><form name="form1" method="post" action="">
<input name="page" type="hidden" id="page" value="<? echo "$id"?>">
</form></td>
<td> </td>
</tr>
</table>
<div align="center"><br>
<span class="s3blackbold">
<?
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM cars where grp='$srch'"),0);
//No. of Page number links at bottom
if (ceil($total_results / $max_results) < $max_pages)
{
$no_of_page_numbers = ceil($total_results / $max_results);
}
else
{
$no_of_page_numbers = $max_pages;
}
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
echo "Showing record <b>$page</b> of <b>$total_pages</b><br>";
if($page > 1) echo "<a href='".$_SERVER['PHP_SELF']."?page=".($page - 1)."'><< Previous |</a> ";
$start = $page-ceil((($no_of_page_numbers-1)/2));
$stop = $page+ceil((($no_of_page_numbers-1)/2));
if($start<=1) { $start=1; $stop = $no_of_page_numbers; }
elseif($stop>=$total_pages) { $start=($total_pages-$no_of_page_numbers); $stop = $total_pages; }
for($i = $start; $i <= $stop; $i++){
if($page==$i) echo "$i | ";
else echo "<a href='".$_SERVER['PHP_SELF']."?page=$i'>$i</a> | ";
}
if($page < $total_pages) echo "<a href='".$_SERVER['PHP_SELF']."?page=".($page + 1)."'>Next >></a>";
?>