473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help me with the PAGINATION code

114 New Member
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
3 1571
code green
1,726 Recognized Expert Top Contributor
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
raaman rai
114 New Member
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
1,726 Recognized Expert Top Contributor
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

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

Similar topics

2
2936
by: Somerset Bob | last post by:
I've got pagination errors in a phpBB board. Some forums are showing "page 1 of 0". Previous enquiries both here and at the phpBB forum have given me half-answers, the latest of which contained...
7
2048
by: redneck_kiwi | last post by:
Need some opinions on how best to display in excess of 14K records based on users search criteria. Application Concept: ----------------------- User enters various search criteria (as many...
2
2790
by: Kurzman | last post by:
Let me know what you think about the following code: DECLARE @MaxIdValue int DECLARE @MaxSortFieldValue nvarchar(50) SELECT TOP 1 @MaxIdValue = , @MaxSortFieldValue = FROM ( SELECT TOP...
4
3629
by: Ed Jay | last post by:
I generate a DHTML page (a medical report) with dynamically generated text based on user input (answers to questions). The page length changes dynamically. I desire that when the page is printed...
0
1449
by: Sheau Wei | last post by:
I am using php version 4.3.3. The below was my pagination code. My problem is that when i run this code in my computer, i wall fail because of the hyperlink of the pagination not function when i...
2
2062
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
4
1439
by: ilayacute | last post by:
hi im using below code for pagination. i want 1 help,when i click the name(link)it should go to editpage.asp.my question is how can i add links to names? <% Option Explicit Const adOpenStatic =...
16
2746
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
15
1784
omerbutt
by: omerbutt | last post by:
hi i have made a simple pagination but it is creating problem , the problem is that if there are any products under the category that is clicked in the left menu then the page works fine but if it...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.