分页如果不能正常工作


pagination within if not working properly

我正在构建一个搜索页面,用户可以通过字段搜索,然后显示表。我试图通过php实现分页,但分页不工作。任何分页指南都会有所帮助。如果用户输入了第一个字段,而其他字段为空,那么我将呈现一个的代码,还有其他条件。

if($item!="" && $brand=="" && $model=="") {
    $sql ="SELECT * FROM table WHERE Product LIKE '%".$item."%' ";
    $result = mysql_query($sql);
    //$result1 = mysql_query($sql1);
    $r = mysql_fetch_array($result);
    $p=$r['Product'];
    $b=$r['Brand'];
    $m=$r['Model'];
    if ($item!=$p) {
        echo "<font style='"color:Red;'"><h3>No Item Found!!!</h3></font>";
    } else  {
        echo "<table class='table table-striped table-bordered bootstrap-datatable datatable'>
            <thead>
            <tr>
            <th>Item</th>
            <th>Brand</th>
            <th>Model</th>
            <th>Dealer Price</th>
            <th>Quantity</th>
            <th>Description</th>
            </tr>
            </thead>";
        if (!(isset($pagenum)))  { 
            $pagenum = 1; 
        } 
        //Here we count the number of results 
        //Edit $data to be your query 
        $data = mysql_query("SELECT * FROM table WHERE Product='$item' ") or die(mysql_error()); 
        $rows = mysql_num_rows($data); 
        //This is the number of results displayed per page 
        $page_rows = 10; 
        //This tells us the page number of our last page 
        $last = ceil($rows/$page_rows); 
        //this makes sure the page number isn't below one, or more than our maximum pages 
        if ($pagenum < 1) { 
            $pagenum = 1; 
        } elseif ($pagenum > $last) { 
            $pagenum = $last; 
        } 
        //This sets the range to display in our query 
        $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
        $sqll ="SELECT * FROM table WHERE Product='$item'  $max ";
        $resultt = mysql_query($sqll);
        echo "<tbody>";
        while($row = mysql_fetch_array($resultt)) {
            echo "<tr>";
            echo "<td>" . $row['Product'] . "</td>";
            echo "<td>" . $row['Brand'] . "</td>";
            echo "<td>" . $row['Model'] . "</td>";
            echo "<td>" . $row['Dprice'] . "</td>";
            echo "<td>" . $row['Quantity'] . "</td>";
            echo "<td>" . $row['Quality'] . "</td>";
            echo "</tr>";
        }
        echo "</tbody>";
        echo "</table>";
        echo " --Page $pagenum of $last-- <p>";
        // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
        if ($pagenum == 1) {
        } else {
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
            echo " ";
            $previous = $pagenum-1;
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
        } 
        //just a spacer
        echo " ---- ";
        //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
        if ($pagenum == $last) {
        } else {
            $next = $pagenum+1;
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
            echo " ";
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
        } 
    } elseif($item=="" && $brand!="" && $model=="") {

…所以在

我已经尝试对表进行分页,在第一页中可见10行,但在转到下一页时没有表显示,虽然表中还有其他数据。

错误在于您必须在第一个文件

的顶部添加
$pagenum = filter_input(INPUT_GET, 'pagenum', FILTER_SANITIZE_NUMBER_INT);

如果不给$_GET参数赋值,就不能直接访问$pagenum。

我希望这是有用的!