943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 76
  • PHP RSS
Sep 3rd, 2010
0

cannot paginate select join query properly

Expand Post »
I am frustrated to no end. I absolutely cannot find the solution to paginating a search result that pulls info from two tables. Everything seems fine at the start. I get a page with the correct number of results (2) and the correct number of page links on the bottom (2-3) But when I click on one of the bottom links, it seems as if the code has thrown out the original query and just pulls the entire database. The number of pages at the bottom match the total entries in the database (19 pages) and shows two random entries. Continuing on to the other links, I see that it's just going through the database and pulling every entry. Not just the 6 entries from my original query.

I'm posting the entire script here, because I have no idea where the problem is. I've tried a hundred changes, and rewritten the script. I still get the same problem.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $host = "databaseconnect.js";
  4.  
  5.  
  6. $select = mysql_select_db($db, $con);
  7. if(!$select){
  8. die(mysql_error());
  9. }
  10.  
  11.  
  12.  
  13. // How many adjacent pages should be shown on each side?
  14. $adjacents = 3;
  15.  
  16. /*
  17. First get total number of rows in data table.
  18. If you have a WHERE clause in your query, make sure you mirror it here.
  19. */
  20. $query = "SELECT COUNT(*) as num
  21. FROM
  22. descriptors
  23. JOIN
  24. plantae ON (descriptors.plant_id = plantae.plant_id)
  25. WHERE
  26. descriptors.leaf_shape LIKE '%$select1%'
  27. AND descriptors.leaf_venation LIKE '%$select3%'
  28. AND descriptors.leaf_margin LIKE '%$select4%'";
  29.  
  30.  
  31. $total_pages = mysql_fetch_array(mysql_query($query));
  32. $total_pages = $total_pages[num];
  33.  
  34. /* Setup vars for query. */
  35. $targetpage = "leafsearch4.php"; //your file name (the name of this file)
  36. $limit = 2; //how many items to show per page
  37. $page = $_GET['page'];
  38. if($page)
  39. $start = ($page - 1) * $limit; //first item to display on this page
  40. else
  41. $start = 0; //if no page var is given, set start to 0
  42.  
  43. /* Get data. */
  44.  
  45. $sql = "SELECT
  46. descriptors.*
  47. ,plantae.*
  48. FROM
  49. descriptors
  50. INNER JOIN
  51. plantae ON (descriptors.plant_id = plantae.plant_id)
  52. WHERE
  53. descriptors.leaf_shape LIKE '%$select1%'
  54. AND descriptors.leaf_venation LIKE '%$select3%'
  55. AND descriptors.leaf_margin LIKE '%$select4%'
  56. ORDER BY plantae.scientific_name ASC LIMIT $start, $limit";
  57.  
  58.  
  59. $result = mysql_query($sql);
  60.  
  61. /* Setup page vars for display. */
  62. if ($page == 0) $page = 1; //if no page var is given, default to 1.
  63. $prev = $page - 1; //previous page is page - 1
  64. $next = $page + 1; //next page is page + 1
  65. $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
  66. $lpm1 = $lastpage - 1; //last page minus 1
  67.  
  68. /*
  69. Now we apply our rules and draw the pagination object.
  70. We're actually saving the code to a variable in case we want to draw it more than once.
  71. */
  72. $pagination = "";
  73. if($lastpage > 1)
  74. {
  75. $pagination .= "<div class=\"pagination\">";
  76. //previous button
  77. if ($page > 1)
  78. $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
  79. else
  80. $pagination.= "<span class=\"disabled\">« previous</span>";
  81.  
  82. //pages
  83. if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
  84. {
  85. for ($counter = 1; $counter <= $lastpage; $counter++)
  86. {
  87. if ($counter == $page)
  88. $pagination.= "<span class=\"current\">$counter</span>";
  89. else
  90. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  91. }
  92. }
  93. elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
  94. {
  95. //close to beginning; only hide later pages row 85 or 86
  96. if($page < 1 + ($adjacents * 2))
  97. {
  98. for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
  99. {
  100. if ($counter == $page)
  101. $pagination.= "<span class=\"current\">$counter</span>";
  102. else
  103. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  104. }
  105. $pagination.= "...";
  106. $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
  107. $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
  108. }
  109. //in middle; hide some front and some back
  110. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  111. {
  112. $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
  113. $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
  114. $pagination.= "...";
  115. for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  116. {
  117. if ($counter == $page)
  118. $pagination.= "<span class=\"current\">$counter</span>";
  119. else
  120. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  121. }
  122. $pagination.= "...";
  123. $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
  124. $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
  125. }
  126. //close to end; only hide early pages
  127. else
  128. {
  129. $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
  130. $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
  131. $pagination.= "...";
  132. for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
  133. {
  134. if ($counter == $page)
  135. $pagination.= "<span class=\"current\">$counter</span>";
  136. else
  137. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  138. }
  139. }
  140. }
  141.  
  142. //next button row 132
  143. if ($page < $counter - 1)
  144. $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
  145. else
  146. $pagination.= "<span class=\"disabled\">next »</span>";
  147. $pagination.= "</div>\n";
  148. }
  149. ?>
  150.  
  151. <?php
  152.  
  153. echo '<table align="center" border=1 cellspacing="0" cellpading-"5">
  154. <tr>
  155. <td align="left"><b></b></td>
  156. <td align="left"><b></b></td>
  157. <td align="left"><b>Scientific name</b></td>
  158. <td align="left"><b>Common name</b></td>
  159. <td align="left"><b>Leaf shape</b></td>
  160. </tr>';
  161. while($row = mysql_fetch_array($result))
  162. {
  163. echo '<tr>
  164. <td align="left"> <a href="link.php">View plant</a> &nbsp; &nbsp;</td>
  165. <td align="left"> nothing here &nbsp; &nbsp;</td>
  166. <td align="left">' . $row['scientific_name'] . '</td>
  167. <td align="left">test' . $row['common_name'] . '</td>
  168. <td align="left">' . $row['leaf_shape'] . '</td>
  169. </tr>';
  170. }
  171. echo '</table>';
  172.  
  173. // lost curly b
  174. ?>
  175.  
  176. <?=$pagination?>
Last edited by turpentyne; Sep 3rd, 2010 at 5:09 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
turpentyne is offline Offline
10 posts
since Jul 2010
Sep 4th, 2010
0
Re: cannot paginate select join query properly
do you have a demo link to your page so I can see the output of the above script?
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007

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: How to keep track of no. of visitors
Next Thread in PHP Forum Timeline: PHP





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


Follow us on Twitter


© 2011 DaniWeb® LLC