Page 1 of 1

Pagination problem. Only 1st page of results is displayed.

Posted: Sat Oct 23, 2004 9:45 am
by alpor
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.

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>&nbsp;</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>&nbsp;</td>
    <td><form name="form1" method="post" action="">
        <input name="page" type="hidden" id="page" value="<? echo "$id"?>">
      </form></td>
    <td>&nbsp;</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>"; 
?>

Posted: Sat Oct 23, 2004 1:02 pm
by Christopher
First, I think that because you set $max_page - 1; that you will always get $no_of_page_numbers = $max_pages;.

Second, I would not waste all the processing time on the huge list of assignments in your loop.

Code: Select all

// your code
Group: (<? echo "$cargroup"?>)

// is not different nor less clear than
Group: (<? echo $r['grp']; ?>)
Finally, you can also improve your script by doing repeated calculations only once, for example

Code: Select all

$total_pages = ceil($total_results / $max_results);
if ($total_pages < $max_pages) {
    $no_of_page_numbers = $total_pages;
} else {
    $no_of_page_numbers = $max_pages;
}

Posted: Sat Oct 23, 2004 1:59 pm
by John Cartwright
Do a little debugging, echo the variables throughought your script to make sure they are set to the proper values.

Also make sure you have error_reporting(E_ALL);

Posted: Sat Oct 23, 2004 3:11 pm
by timvw
after a quick glance, i don't see you passing the $srch variable between the pages???

Posted: Sat Oct 23, 2004 5:54 pm
by alpor
Thank you arborint.
I have now incorporated the changes to the echo statements. This has reduced the amount of code, but I still have the pagination problem.

I get page 1 of 3 for the 1st results page, but then page 2 shows no results. I currently have 25 results in my database but want to solve this problem before filling my database table with more data.

I am not sure how to incorporate your suggestion for the alteration to the page numbering code. My code now reads as follows:-

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))
{   	
?>
</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=$r["linkcarphotos"]; ?> 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=$r["grp"]; ?>)</span> 
        <span class="s4greynorm"><? echo $comp=$r["company"]; ?>-<? echo $id=$r["c4m"]; ?> Minimum Driver 
        Age <? echo $mindriverage=$r["mindriverage"]; ?></span></div></td>
  </tr>
  <tr> 
    <td width="410" colspan="2" class="s4colourblacknorm"> <div align="left"><span class="s3blackbold"><? echo $mke=$r["make"]; ?> 
        <? echo $modl=$r["model"]; ?></span> (<? echo $doors=$r["doors"]; ?> door)</div></td>
  </tr>
  <tr> 
    <td colspan="2" class="s4colourrednorm"><div align="left">Options included: 
        <? echo $airc=$r["aircon"]; ?> <? echo $rad=$r["radio"]; ?> <? echo $powersteering=$r["powersteering"]; ?> <? echo $electricwindows=$r["electricwindows"]; ?> 
        <? echo $centrallocking=$r["centrallocking"]; ?> <? echo $airbag=$r["airbag"]; ?> <? echo $absbrakes=$r["absbrakes"]; ?></div>
      </td>
  </tr>
  <tr> 
    <td width="410" colspan="2" class="s4colourgreennorm"> <div align="left"><span class="s4greenbold"><? echo $mnth=$r["month"]; ?></span> 
        price per week: <span class="s4blackbold"><? echo $curry=$r["currency"]; ?><? echo $pweek=$r["perweek"]; ?></span> 
      </div></td>
  </tr>
  <tr> 
    <td class="s4colourorangenorm"><div align="left">Child Seat: <span class="s4orangebold"><? echo $childseatamount=$r["childseatamount"]; ?> 
        <? echo $childseatperiod=$r["childseatperiod"]; ?></span> </div></td>
    <td class="s4colourorangenorm"><div align="left">Airport Surcharge: <span class="s4orangebold"><? echo $airportsurchargeamount=$r["airportsurchargeamount"]; ?></span></div></td>
  </tr>
  <tr> 
    <td class="s4colourorangenorm"><div align="left">Roof Rack: <span class="s4orangebold"><? echo $roofrackamount=$r["roofrackamount"]; ?> 
        <? echo $roofrackperiod=$r["roofrackperiod"]; ?></span> </div></td>
    <td class="s4colourorangenorm"><div align="left">Green Card: <span class="s4orangebold"><? echo $greencardamount=$r["greencardamount"]; ?></span></div></td>
  </tr>
  <tr> 
    <td class="s4colourorangenorm"><div align="left">Insurance Excess: <span class="s4orangebold"><? echo $insuranceexcessamount=$r["insuranceexcessamount"]; ?></span></div></td>
    <td class="s4colourorangenorm"><div align="left">Additional Driver: <span class="s4orangebold"><? echo $additionaldriveramount=$r["additionaldriveramount"]; ?></span></div></td>
  </tr>
  <tr> 
    <td colspan="2" class="s4colourorangenorm"> <div align="left">Excess Waiver: 
        <span class="s4orangebold"><? echo $insuranceexcesswaiveramount=$r["insuranceexcesswaiveramount"]; ?> <? echo $excesswaiverperiod=$r["excesswaiverperiod"]; ?></span></div></td>
  </tr>
  <tr> 
    <td width="410"><div align="left"><a href="<? echo $linkreservationform=$r["linkreservationform"]; ?>" class="s4orangebold"> 
        CLICK HERE to book online now</a>&nbsp;</div></td>
    <td width="410"><div align="left"><a href="<? echo $linkgeneralconditions=$r["linkgeneralconditions"]; ?>" class="s4orangebold">CLICK 
        HERE for the General Conditions</a></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</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 page $page of $total_pages<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>";
?>
Phenom -
I try not to incorporate too much error reporting as I can only work on a live web site and believe that error reporting can divulge too much information if any errors occur when testing new pages.

timvw -
Thanks for not helping- "I am a proffessional and I know what is wrong because you have not passed the variable $srch, but I am not going to help you solve the problem because I am a proffessional!"
I guess this forum can do without wasted posts like this!! Please help me if you know what is wrong with my code, that is why I have posted my problem.

Posted: Sat Oct 23, 2004 6:16 pm
by John Cartwright
Phenom -
I try not to incorporate too much error reporting as I can only work on a live web site and believe that error reporting can divulge too much information if any errors occur when testing new pages.
:idea: Download your own local server... problem solved. :)

Posted: Sat Oct 23, 2004 7:08 pm
by alpor
Phenom

I do not run a local server as I am unable to download my database (current hosting package does not allow).

I also run windows XP (not proffessional) which does not have a local server incorporated. I did look at installing apache, and in fact have it installed on my pc, but in the end decided to work with what I already have as I would have to set up the exact same local server protocols as my host in order to test my code properly.

Most of my coding is pretty standard run of the mill reporting and doesn't normally require me to perform error reporting. Although this pagination is a problem and I understand quite a few people have problems with it displaying the 2nd/next pages. Do you think that error reporting would highlight the problem area? if so, what error codes should I incorporate and where?

I understand that pagination may be solved by passing the variable in the url, but I am not that knowledgible about how to incorporate it into my code which I did not originally write, as my code is modified from other code snippets/tutorials.

I guess one day I will be able to write and modify complete codes myself when I have gained the knowledge that other experienced posters on this forum site have. You have to start somewhere.

Problem solved

Posted: Mon Oct 25, 2004 4:42 pm
by alpor
After much tweeking with the code, a result. :D
I Finally managed to create a code that works by changing the query and by including the search fields in the url. Not quite sure why I had to include the button Submit=Search though? Anyway, here is the code that works.

Code: Select all

<?php
//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 = 10;
// 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='".$_REQUEST['fieldsearch']."' ORDER BY perweek ASC LIMIT $from, $max_results");

while($r=mysql_fetch_array($result))
{   	
?>
</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=$r["linkcarphotos"]; ?> 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=$r["grp"]; ?>)</span> 
        <span class="s4greynorm"><? echo $comp=$r["company"]; ?>-<? echo $id=$r["c4m"]; ?> 
        Minimum Driver Age <? echo $mindriverage=$r["mindriverage"]; ?></span></div></td>
  </tr>
  <tr> 
    <td width="410" colspan="2" class="s4colourblacknorm"> <div align="left"><span class="s3blackbold"><? echo $mke=$r["make"]; ?> 
        <? echo $modl=$r["model"]; ?></span> (<? echo $doors=$r["doors"]; ?> door)</div></td>
  </tr>
  <tr> 
    <td colspan="2" class="s4colourrednorm"><div align="left">Options included: 
        <? echo $airc=$r["aircon"]; ?> <? echo $rad=$r["radio"]; ?> <? echo $powersteering=$r["powersteering"]; ?> 
        <? echo $electricwindows=$r["electricwindows"]; ?> <? echo $centrallocking=$r["centrallocking"]; ?> 
        <? echo $airbag=$r["airbag"]; ?> <? echo $absbrakes=$r["absbrakes"]; ?></div></td>
  </tr>
  <tr> 
    <td width="410" colspan="2" class="s4colourgreennorm"> <div align="left"><span class="s4greenbold"><? echo $mnth=$r["month"]; ?></span> 
        price per week: <span class="s4blackbold"><? echo $curry=$r["currency"]; ?><? echo $pweek=$r["perweek"]; ?></span> 
      </div></td>
  </tr>
  <tr> 
    <td class="s4colourorangenorm"><div align="left">Child Seat: <span class="s4orangebold"><? echo $childseatamount=$r["childseatamount"]; ?> 
        <? echo $childseatperiod=$r["childseatperiod"]; ?></span> </div></td>
    <td class="s4colourorangenorm"><div align="left">Airport Surcharge: <span class="s4orangebold"><? echo $airportsurchargeamount=$r["airportsurchargeamount"]; ?></span></div></td>
  </tr>
  <tr> 
    <td class="s4colourorangenorm"><div align="left">Roof Rack: <span class="s4orangebold"><? echo $roofrackamount=$r["roofrackamount"]; ?> 
        <? echo $roofrackperiod=$r["roofrackperiod"]; ?></span> </div></td>
    <td class="s4colourorangenorm"><div align="left">Green Card: <span class="s4orangebold"><? echo $greencardamount=$r["greencardamount"]; ?></span></div></td>
  </tr>
  <tr> 
    <td class="s4colourorangenorm"><div align="left">Insurance Excess: <span class="s4orangebold"><? echo $insuranceexcessamount=$r["insuranceexcessamount"]; ?></span></div></td>
    <td class="s4colourorangenorm"><div align="left">Additional Driver: <span class="s4orangebold"><? echo $additionaldriveramount=$r["additionaldriveramount"]; ?></span></div></td>
  </tr>
  <tr> 
    <td colspan="2" class="s4colourorangenorm"> <div align="left">Excess Waiver: 
        <span class="s4orangebold"><? echo $insuranceexcesswaiveramount=$r["insuranceexcesswaiveramount"]; ?> 
        <? echo $excesswaiverperiod=$r["excesswaiverperiod"]; ?></span></div></td>
  </tr>
  <tr> 
    <td width="410"><div align="left"><a href="<? echo $linkreservationform=$r["linkreservationform"]; ?>" class="s4orangebold"> 
        CLICK HERE to book online now</a>&nbsp;</div></td>
    <td width="410"><div align="left"><a href="<? echo $linkgeneralconditions=$r["linkgeneralconditions"]; ?>" class="s4orangebold">CLICK 
        HERE for the General Conditions</a></div></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='".$_REQUEST['fieldsearch']."'"),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 page $page of $total_pages<br>";
if($page > 1) echo "<a href='$PHP_SELF?page=".($page - 1)."&fieldsearch=".$_REQUEST['fieldsearch']."&Submit=Search'><< 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='$PHP_SELF?page=$i&fieldsearch=".$_REQUEST['fieldsearch']."&Submit=Search'>$i</a> | ";
}
if($page < $total_pages) echo "<a href='$PHP_SELF?page=".($page + 1)."&fieldsearch=".$_REQUEST['fieldsearch']."&Submit=Search'>Next >></a>";
?>