Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch result pagination return all result when next is clicked
    primarykey
    data
    text
    <p>I am trying to build a basic search but am getting a small code issue here, i want to paginate my searched result set and this is what i have 1) an input field named "input", a and within a form of post method 2) when user submits the form it calls search.php with the follow code bellow.</p> <h2>The problem</h2> <p>the pagination returns a properly paginated result set but when i move to next page all records in the search table are returned including those that do not meet the criteria in the where clause. </p> <p>Here is the code: Let me know if it's too much code here. I can paste somewhere else and provide a link; Thanks guys</p> <pre><code>&lt;?php $input = $_POST['input']; $categories = $_POST['category']; $state = $_POST['state']; $targetpage = "search.php"; $limit = 3; //This query checks for data $qq = " SELECT * FROM classified where confirm='0' "; $qq = $db-&gt;prepare($qq); $qq-&gt;execute(); $total_pages =$qq-&gt;rowCount(); $stages = 3; $page = ($_GET['page']); if ($page){ $start = ($page - 1) * $limit; } else { $start = 0; } $rows = $qq-&gt;fetchAll(); if ($rows &gt; 0){ $q = " SELECT * FROM classified where confirm='0' "; if (!empty( $input)) { $q .= "AND title LIKE '%".$input."%' "; } if (!empty($_POST['search_category']) ) { $q .= "AND id_cat = ".$categories." "; } if (!empty($_POST['state']) ) { $q .= "AND id_state = ".$state." "; } $q .= "ORDER BY date DESC LIMIT $start, $limit "; } $q = $db-&gt;prepare($q); $q-&gt;execute(); //// Just for testing purposes to see what's coming out print_r($q); /*** echo number of columns ***/ $resultt = $q-&gt;fetchAll(); // Initial page num setup if ($page == 0){ $page = 1; } $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total_pages/$limit); $LastPagem1 = $lastpage - 1; $paginate = ''; if ($lastpage &gt; 1) { $paginate .= "&lt;div class='paginate'&gt;"; // Previous if ($page &gt; 1){ $paginate.= "&lt;a href='$targetpage?page=$prev'&gt;Previous&lt;/a&gt;"; } else { $paginate.= "&lt;span class='disabled'&gt;Previous&lt;/span&gt;"; } // Pages if ($lastpage &lt; 7 + ($stages * 2)) { // Not enough pages to breaking it up for ($counter = 1; $counter &lt;= $lastpage; $counter++) { if ($counter == $page) { $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;"; } else { $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;"; } } } else if ($lastpage &gt; 5 + ($stages * 2)) {// Enough pages to hide a few? // Beginning only hide later pages if ($page &lt; 1 + ($stages * 2)) { for ($counter = 1; $counter &lt; 4 + ($stages * 2); $counter++) { if ($counter == $page) { $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;"; } else { $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;"; } } $paginate.= "..."; $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;"; $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;"; } else if ($lastpage - ($stages * 2) &gt; $page &amp;&amp; $page &gt; ($stages * 2)) { // Middle hide some front and some back $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;"; $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;"; $paginate.= "..."; for ($counter = $page - $stages; $counter &lt;= $page + $stages; $counter++) { if ($counter == $page) { $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;"; } else { $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;"; } } $paginate.= "..."; $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;"; $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;"; } else { // End only hide early pages $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;"; $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;"; $paginate.= "..."; for ($counter = $lastpage - (2 + ($stages * 2)); $counter &lt;= $lastpage; $counter++) { if ($counter == $page) { $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;"; } else { $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;"; } } } } // Next if ($page &lt; $counter - 1) { $paginate.= "&lt;a href='$targetpage?page=$next'&gt;Next&lt;/a&gt;"; } else { $paginate.= "&lt;span class='disabled'&gt;Next&lt;/span&gt;"; } $paginate.= "&lt;/div&gt;"; } // pagination echo $total_pages.' Results'; echo $paginate; //////// if (count($resultt) !== 0) { foreach ($resultt as $row) { echo $row['title']; echo $row['categories']; echo $row['state']; } } else { echo "No data available"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload