rodhow Posted November 30, 2011 Share Posted November 30, 2011 Hello, I adapted the pagination script from the tutorial (great instruction and detail) from this site and it works perfectly. However, I added a search engine to it and it works like it should. The problem comes in with the following SQL query: $sql = "SELECT COUNT(*) FROM qa"; If i modify it to fit my query, the number at the bottom of the page disappears and the > >> links go to blank pages. Also all queried rows show on one page and doesn't stop at ten. How do i modify my pagination script with a search engine so people can query their results without 'breaking' the pagination script?? Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/ Share on other sites More sharing options...
trq Posted November 30, 2011 Share Posted November 30, 2011 Were going to need to see some relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1292547 Share on other sites More sharing options...
rodhow Posted November 30, 2011 Author Share Posted November 30, 2011 okay here is the entire code with the part in question near the top. This works perfectly: $sql = "SELECT COUNT(*) FROM qa"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result);$numrows = $r[0]; $rowsperpage = 10; $totalpages = ceil($numrows / $rowsperpage); if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $sql = "SELECT link FROM qa LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); while ($list = mysql_fetch_assoc($result)) { echo $list['link'] . " " . $list['number'] . "<br />"; } $range = 3; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } ?> Now this is the part where I added the search engine, some error checks and changed the sql query which seems to break the pagination. I fixed the number count by keeping the SELECT COUNT in the first query and the variables in the second query. The only problem now is that if I have more than one page (11 records or more) the next pages as well as the > >> links return blank pages. I guess its now how can I access the remaining records: <form name="form" action="pagination2.php" method="get"> <input type="text" name="keywords" /> <input type="submit" name="Submit" value="Search" /> </form> <?php $var = @$_GET['keywords'] ; $trimmed = trim($var); if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } $sql = $sql = "SELECT COUNT(*) FROM qa WHERE tags LIKE '%$trimmed%'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result);$numrows = $r[0]; echo "<p>You searched for: "" . $var . ""</p>"; $rowsperpage = 10; $totalpages = ceil($numrows / $rowsperpage); if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $sql = "SELECT link FROM qa WHERE tags LIKE '%$trimmed%' LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); echo "<table>"; while ($list = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $list['link'] . "</td>"; echo "</tr>"; } echo "</table>"; $range = 3; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1292692 Share on other sites More sharing options...
rodhow Posted December 1, 2011 Author Share Posted December 1, 2011 can anyone see what is wrong with the query statement(s) at all, if that is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1293272 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2011 Share Posted December 1, 2011 I'm not sure is this is the only reason for a blank page (or do you mean just empty data/no data), but you must build the links with the $_GET['keywords'] value on the end of the URL so that each page knows what to search for. If you replace your code, starting at the $range = 3 statement, with the following, it should work - <?php $range = 3; $links = ''; // build links in a string (output it later in your actual content on the page) // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 $_GET['currentpage'] = 1; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page $_GET['currentpage'] = $prevpage; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link $links .= " [<b>$x</b>] "; // if not current page... } else { // make it a link $_GET['currentpage'] = $x; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page $_GET['currentpage'] = $nextpage; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>></a> "; // echo forward link for lastpage $_GET['currentpage'] = $totalpages; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>>></a> "; } // end if echo $links; // echo the links wherever you want in the content on your page The above code only modifies/sets the $_GET['currentpage'] value when building the links, but leaves any other $_GET value as is. It builds the navigation links in a variable so that you can output them anywhere on your page, for instance if you wanted to put links above and below the resultant display of the data. It also corrects the use of < and > characters in the navigation in favor of html entities that won't break the html on the page. Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1293277 Share on other sites More sharing options...
rodhow Posted December 2, 2011 Author Share Posted December 2, 2011 Hello, I replaced the code and inserted your code and now there is no pagination (< << prev,next,>> >) showing up at the bottom now. I am so lost right now. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1293371 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2011 Share Posted December 2, 2011 If you post your current code, someone can probably help with what it is or is not doing. Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1293373 Share on other sites More sharing options...
rodhow Posted December 3, 2011 Author Share Posted December 3, 2011 I want to apologize. It was an error on my part. I put the code in correctly and now it works, thank you PFMaBiSmAd for your expertise! I couldn't have figured it out without ur help! Quote Link to comment https://forums.phpfreaks.com/topic/252091-almost-there-problems-with-search-engine-and-pagination-page/#findComment-1293751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.