By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

help me with the PAGINATION code

100+
P: 114
Please help me with the following code. I have used this pagination script from the net and customized it but unfortunately it doesnt work as expected. The problem is, for the trial purpose i have 3 records in the table and i have set the limit value to 2 only for now (for trial). So it is expected that i have 2 records displayed in the first page and 1 record in the second page. The first two records are displayed normally with the NEXT link. When i click on the NEXT link i get the third record but it is not actully the expected record as in my table. Instead the first record is displayed again. So, i feel there is a problem in the limit used in the second page. Please help me out.

Expand|Select|Wrap|Line Numbers
  1.  <?php   
  2.            $searchstr=$_GET["class"]; 
  3.          ?> 
  4.          <h3>List of Class <?php echo $searchstr; ?> Contractors</h3>
  5.          <br>
  6.          <?php 
  7.            $page=$_GET["page"];  
  8.  
  9.            if($page == "") $page=1; 
  10.  
  11.  
  12.            $q="SELECT * FROM ".TBL_COMPANY." WHERE class='".$searchstr."' ORDER BY cdbno"; 
  13.            $result = $database->query($q);
  14.            $num_rows = mysql_num_rows($result); 
  15.  
  16.            $limit = 2;
  17.            $pages=ceil($num_rows/$limit); 
  18.  
  19.            $query = "SELECT * FROM ".TBL_COMPANY." WHERE class='".$searchstr."' ORDER BY cdbno LIMIT " . ($page-1)*$limit . ",$limit";
  20.            $searchresult = $database->query($query); 
  21.            $numrows = mysql_numrows($searchresult);
  22.  
  23.            if(!$result || ($numrows < 0)){
  24.               echo "Error displaying info";
  25.            }
  26.            if($numrows == 0){
  27.               echo "<span class=\"info\">No Contractor information found for this Class</span>";
  28.            }
  29.            else
  30.            {
  31.          ?>
  32.            <table width="100%">
  33.              <tr>
  34.                <td width="61" align="center" id="headstyle">CDB #</td>
  35.                <td width="81" id="headstyle">Company</td>
  36.                <td width="46" align="center" id="headstyle">Class</td>
  37.                <td width="122" id="headstyle">Propietor</td>
  38.                <td width="126" id="headstyle">Details</td>
  39.              </tr>
  40.          <?php
  41.            for($i=0; $i<$numrows; $i++){
  42.          ?>
  43.              <tr>
  44.                <td class="details" align="center"><?php echo mysql_result($result,$i,"cdbno"); ?></td>
  45.                <td class="details"><?php echo mysql_result($result,$i,"company"); ?></td>
  46.                <td class="details" align="center"><?php echo mysql_result($result,$i,"class"); ?></td>
  47.                <td class="details"><?php echo mysql_result($result,$i,"propietor"); ?></td>
  48.                <td class="details"><a href="process.php?viewprofile=true&&cdbno=<?php echo mysql_result($result,$i,"cdbno"); ?>"><strong>MORE DETAILS</strong></a></td>
  49.              </tr>
  50.          <?php
  51.            }
  52.          ?>
  53.         </table>
  54.          <?php 
  55.            }
  56.  
  57.            $nav=""; 
  58.            if($page > 1) { 
  59.              $nav .= "<a href=\"displaycontractors.php?page=" . ($page-1) . "&class=" .urlencode($searchstr) . "\">
  60.              << Prev&nbsp;&nbsp;&nbsp;</a>"; 
  61.            } 
  62.            for($i = 1 ; $i <= $pages ; $i++) { 
  63.              if($i == $page) { 
  64.                $nav .= "<B>$i</B>"; 
  65.              }
  66.              else
  67.              { 
  68.                $nav .= "<a href=\"displaycontractors.php?page=" . $i . "&class=" .urlencode($searchstr) . "\">
  69.                &nbsp;&nbsp;&nbsp;$i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>"; 
  70.              } 
  71.            } 
  72.            if($page < $pages) { 
  73.              $nav .= "<a href=\"displaycontractors.php?page=" . ($page+1) . "&class=" .urlencode($searchstr) . "\">
  74.              &nbsp;&nbsp;&nbsp;Next >></a>"; 
  75.            } 
  76.            echo "<br><br>" . $nav; 
  77.         ?> 
  78.  
Sep 4 '08 #1
Share this Question
Share on Google+
3 Replies


code green
Expert 100+
P: 1,726
The SELECT query generally uses ORDER BY DESC because ASC is default.
So the latest values are selected.
You generally specify a > greater than value or use BETWEEN.
The 'next' link passes the last highest value.
So the greater than value becomes the last highest value,

As the pagination develops you will probably find that you will also need to
store and pass between pages the current page number and the limit
Sep 4 '08 #2

100+
P: 114
thankyou, for your reply but i must say it didnt work. Plus i wanna inform you that i cannot use > or BETWEEN condition in my query cause its not relevant at all. Let me give you an idea i.e i am displaying a bunch of records from my table in order with the number specified (each number is unique for each contractor). So i want the records displayed in ASC order of its number. So i just want the records be displayed correctly in each page. Please help me further.

The SELECT query generally uses ORDER BY DESC because ASC is default.
So the latest values are selected.
You generally specify a > greater than value or use BETWEEN.
The 'next' link passes the last highest value.
So the greater than value becomes the last highest value,

As the pagination develops you will probably find that you will also need to
store and pass between pages the current page number and the limit
Sep 4 '08 #3

code green
Expert 100+
P: 1,726
I wasn't trying to give you a solution.
It was more of a guide to how pagination works because you do not seem to be applying the technique

use > or BETWEEN condition in my query cause its not relevant at all.
It is relevant in all pagination.
You just cannot see a use with only three records
Expand|Select|Wrap|Line Numbers
  1. So i want the records displayed in ASC order of its number
You can do this in php after you have got the results.
It looks to me that your LIMIT is pulling out the first rather than the last because ORDER BY is ASC
Sep 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.