943,170 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 79
  • PHP RSS
Sep 2nd, 2010
0

pagination

Expand Post »
Ok, So I found a pagination class fro ma tutorial and decided to try and impliment it into my mysql search query I use here at work to search past Parts Ordered records. I got it working to the point that it only shows 10 results on a page, and it shows the next page links at the bottom of the page. However, when I click on page "2" or any of the page links at the bottom, it takes me to a page showing 0 results. I am still learning PHP, and just need a fresh eye to see if they know what may be the issue. My code is below.

PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  4.  
  5. <head>
  6. <title>Search For A PO</title>
  7. <link href="style.css" rel="stylesheet" type="text/css" />
  8. </head>
  9.  
  10. <div id="static">
  11. <div class="static">
  12. <a href="http://lmtl-linux.lmtl.local/podat">Home</a> </div>
  13. </div>
  14.  
  15. <div id="static2">
  16. <div class="static2">
  17. <a href="http://lmtl-linux.lmtl.local/podat/insert.php">Create A New PO</a> </div>
  18. </div>
  19.  
  20. <body>
  21. <div id="content">
  22. <div class="contentbox">
  23. <h1>Search For A PO</h1>
  24. <form method="post" action="">
  25. <table class="search">
  26. <tr><td> Location: <input type="radio" name="loc" value="IA" <?php if(isset($_REQUEST['loc']) && $_REQUEST['loc']=='IA'){print "checked";}?>>Iowa</td>
  27. <td><input type="radio" name="loc" value="GA" checked="checked" <?php if(isset($_REQUEST['loc']) && $_REQUEST['loc']=='GA'){print "checked";}?>>Georgia</td>
  28. </tr>
  29.  
  30.  
  31. <tr><td> Search: <select name="type">
  32. <option value="po_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='po_num'){print "selected";}?>>PO Number</option>
  33. <option value="date" <?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='date'){print "selected";}?>>Date</option>
  34. <option value="vin_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='vin_num'){print "selected";}?>>VIN Number</option>
  35. </select></td>
  36. <td>for: <input type="text" name="term" /><br /></td></tr>
  37. <tr><td> <input type="submit" name="submit" value="Search" /></td>
  38. <td class="date">* date format: yyyymmdd</td></tr>
  39. </table>
  40. </form>
  41. </div>
  42. <hr width='100%'></hr>
  43. <?php
  44.  
  45. if($_SERVER['REQUEST_METHOD'] === "POST") {
  46. // database connection info
  47. $conn = mysql_connect('localhost','user','pass') or trigger_error("SQL", E_USER_ERROR);
  48. $db = mysql_select_db('partsorder',$conn) or trigger_error("SQL", E_USER_ERROR);
  49.  
  50. // find out how many rows are in the table
  51. $sql = "SELECT COUNT(*) FROM podat";
  52. $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
  53. $r = mysql_fetch_row($result);
  54. $numrows = $r[0];
  55.  
  56. // number of rows to show per page
  57. $rowsperpage = 10;
  58. // find out total pages
  59. $totalpages = ceil($numrows / $rowsperpage);
  60.  
  61. // get the current page or set a default
  62. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  63. // cast var as int
  64. $currentpage = (int) $_GET['currentpage'];
  65. } else {
  66. // default page num
  67. $currentpage = 1;
  68. } // end if
  69.  
  70. // if current page is greater than total pages...
  71. if ($currentpage > $totalpages) {
  72. // set current page to last page
  73. $currentpage = $totalpages;
  74. } // end if
  75. // if current page is less than first page...
  76. if ($currentpage < 1) {
  77. // set current page to first page
  78. $currentpage = 1;
  79. } // end if
  80.  
  81. // the offset of the list, based on current page
  82. $offset = ($currentpage - 1) * $rowsperpage;
  83.  
  84.  
  85. $loc = mysql_real_escape_string($_POST['loc']);
  86. $type = mysql_real_escape_string($_POST['type']);
  87. $term = trim(mysql_real_escape_string($_POST['term']));
  88. $sql = "select * from podat where $type like '%$term%' AND location='$loc' AND date<>'0' LIMIT $offset, $rowsperpage";
  89. $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
  90.  
  91. while ($row = mysql_fetch_assoc($result)){
  92. //var_dump($row);
  93.  
  94. echo "<br/><table class='results'>
  95. <tr><td class='short'><B>PO Number:</B> </td><td>".$row['po_num']."</td></tr>
  96. <tr><td><B>Vendor:</B> </td><td>".$row['vendor']."</td></tr>
  97. <tr><td><B>Date:</B> </td><td>".$row['date']."</td></tr>
  98. <tr><td><B>VIN Number:</B> </td><td>".$row['vin_num']."</td></tr>
  99. <tr><td><B>Description:</B> </td><td>".$row['descr']."</td></tr>
  100. <tr><td><B>Invoice Number:</B> </td><td>".$row['inv_num']."</td></tr>
  101. <tr><td><B>Purchase Agent:</B> </td><td>".$row['agt']."</td></tr>
  102. <tr><td colspan='2'><a href=\"edit.php/?id=".$row['po_num']."\">Edit</a></td></tr></table>";
  103.  
  104. }
  105. /****** build the pagination links ******/
  106. // range of num links to show
  107. $range = 3;
  108.  
  109. // if not on page 1, don't show back links
  110. if ($currentpage > 1) {
  111. // show << link to go back to page 1
  112. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
  113. // get previous page num
  114. $prevpage = $currentpage - 1;
  115. // show < link to go back to 1 page
  116. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
  117. } // end if
  118.  
  119. // loop to show links to range of pages around current page
  120. for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
  121. // if it's a valid page number...
  122. if (($x > 0) && ($x <= $totalpages)) {
  123. // if we're on current page...
  124. if ($x == $currentpage) {
  125. // 'highlight' it but don't make a link
  126. echo " [<b>$x</b>] ";
  127. // if not current page...
  128. } else {
  129. // make it a link
  130. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
  131. } // end else
  132. } // end if
  133. } // end for
  134.  
  135. // if not on last page, show forward and last page links
  136. if ($currentpage != $totalpages) {
  137. // get next page
  138. $nextpage = $currentpage + 1;
  139. // echo forward link for next page
  140. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
  141. // echo forward link for lastpage
  142. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
  143. } // end if
  144. /****** end build pagination links ******/
  145. }
  146. ?>
  147. <br/>
  148. <hr width='100%'></hr>
  149. </div>
  150. </body>
  151. </html>
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
dschuett is offline Offline
92 posts
since Aug 2010
Sep 2nd, 2010
0
Re: pagination
Update: I just found out that if I search for a term, it shows the first 10 results. I then click on page 2, and it shows 0 results as I explained earlier. But here's the catch: If i search the same term from page 2 (that is showing 0 results) it shows the results that were SUPPOSED to be there in the first place. Why do i have re-search the term to get it to show up? I should just be able to click page 2 and the results should show automatically. Thanks for your time!
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
dschuett is offline Offline
92 posts
since Aug 2010
Sep 2nd, 2010
0
Re: pagination
you need to use Sessions.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
CyberSpatium is offline Offline
9 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Geolocation - how possible is this?
Next Thread in PHP Forum Timeline: Syntax Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC