Display pagination in php

I have a search form and I want to place the pagination, but when I click the next button or the second page in the search using pagination, all the results will be mixed up and the data will not be received from the request. I don't know where the problem might be, but this is my search code:

include ('paginate.php'); //include of paginat page

$per_page = 5;   
$find = $_POST['find'];
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find);       // number of results to show per page
$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%$find%'");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have

//------------if page is setcheck-----------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        // error - show first set of results
        $start = 1;              
        $end = $per_page;
    }
} else {
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page;
    $show_page=1;
}
// display pagination
if(isset($_GET['page'])){
    $page = intval($_GET['page']);
}else{
     $page =1;
}


$tpages=$total_pages;
if ($page <= 0)
    $page = 1;
?>
 <?php
                    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
                    echo '<div class="pagination"><ul>';
                    if ($total_pages > 1) {
                        echo paginate($reload, $show_page, $total_pages);
                    }
                    echo "</ul></div>";
                    // display data in table
                    echo "<table class='table table-bordered'>";
                    echo "<thead><tr><th>Project</th> <th>Country</th> <th>Active</th>
                </tr></thead>";
                     for ($i = $start; $i < $end; $i++) {
                        // make sure that PHP doesn't try to show results that don't exist
                        if ($i == $total_results) {
                            break;
                        }
                     ?><form name="frmactive" method="POST" action=""> 
                     <input type="hidden" name="id" value="<?php echo mysql_result($result, $i, 'p_id');?>" />
                       <?php
                        // echo out the contents of each row into a table
                        echo '<tr><td>' . mysql_result($result, $i, 'p_name') . '</td>';
                        echo '<td>' . mysql_result($result, $i, 'p_country') . '</td>';
                        if (mysql_result($result, $i, 'p_isActive')=='1'){ 
                        echo '<td>Active</td>';
                         echo '<td align="center"><a href="activeproject.php?id=' . mysql_result($result, $i, 'p_id') . '">Edit</a></td>';
 }
 else{
    echo'<td>Inactive</td> ';
    echo '<td  align="center"><a href="activeproject.php?id=' . mysql_result($result, $i, 'p_id') . '">Edit</a></td>';
                        echo "</tr>";
                    }       
                    // close table>
                     }
                echo "</table>";
            // pagination
            ?>

and this is paginate.php

function paginate($reload, $page, $tpages) {
    $adjacents = 2;
    $prevlabel = "&lsaquo; Prev";
    $nextlabel = "Next &rsaquo;";
    $out = "";
    // previous
    if ($page == 1) { 
        $out.= "<span>" . $prevlabel . "</span>\n";
    } elseif ($page == 2) {
        $out.= "<li><a  href=\"" . $reload . "\">" . $prevlabel . "</a>\n</li>";
    } else {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n</li>";
    }

    $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
    $pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
    for ($i = $pmin; $i <= $pmax; $i++) {
        if ($i == $page) {
            $out.= "<li  class=\"active\"><a href=''>" . $i . "</a></li>\n";
        } elseif ($i == 1) {
            $out.= "<li><a  href=\"" . $reload . "\">" . $i . "</a>\n</li>";
        } else {
            $out.= "<li><a  href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n</li>";
        }
    }

    if ($page < ($tpages - $adjacents)) {
        $out.= "<a style='font-size:11px' href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
    }
    // next
    if ($page < $tpages) {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n</li>";
    } else {
        $out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
    }
    $out.= "";
    return $out;
}

Could you indicate where the error occurs? Thank you for your time.

+4
source share
1 answer

Try it (note the comments, please):

<?php
 /*
 Assuming that your search form has a POST method set to it,
 be sure to do a redirect to this page and send the find parameter gotten
 from the posted form urlencoded

 ie path_to_my_file?find='.urlencode(trim($_POST['find']))
 */
 include ('paginate.php'); //include of paginat page

 $per_page = 5;   
 $find = strip_tags(strtoupper(trim(urldecode($_GET['find']))));
 $page = intval($_GET['page']);

 $result_total = mysql_query("SELECT COUNT(*) AS total FROM projects WHERE p_name LIKE '%".$find."%'");
 $tmp_total = mysql_fetch_assoc($results_total);

 $totalpages = ceil($tmp_total['total'] / $per_page);

 if ($totalpages != 0 && $page > $totalpages) {
      $page = $totalpages;
 } else if ($page < 1) {
      $page = 1;
 }

 $offset = ($page - 1) * $per_page;

 $result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%".$find."%' LIMIT ".$offset.", ".$per_page);

 $show_page = 1;
 //You need to pass the phrase you are searching for in every link in order for the pagination to work (or user Sessions to save the keyword somewhere if you don't want it in the link)
 $reload = $reload = 'http://localhost/Personal/Stackoverflow/2/index.php?find='.urlencode($find);

 echo '    <div class="pagination">
           <ul>';

 if ($totalpages > 1) {
     echo paginate($reload, $show_page, $totalpages);
 }

 echo '    </ul>
           </div>';

 // display data in table
 if(mysql_num_rows($result) > 0) {
      echo '    <table class="table table-bordered">
                <thead>
                <tr>
                     <th>Project</th>
                     <th>Country</th>
                     <th>Active</th>
                </tr>
                </thead>';

      while($row = mysql_fetch_assoc($result)) { 
           echo '    <tr>
                          <td>'.$row['p_name'].'</td>
                          <td>'.$row['p_country'].'</td>
                          <td>'.((intval($row['p_isActive']) == 1) ? 'Active' : 'Inactive').'</td>
                          <td align="center">
                               <a href="activeproject.php?id='.$row['p_id'].'">Edit</a>
                          </td>
                     </tr>';
      }

      echo '    </table>';
 }
?>
0
source

All Articles