如果不能正常工作,请在内部分页

时间:2014-05-07 10:25:39

标签: php mysql pagination

我正在构建一个搜索页面,用户可以通过字段进行搜索然后显示该表。我正在尝试通过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行,但是在进入下一页时,没有显示任何表格,尽管表格中还有其他数据。

1 个答案:

答案 0 :(得分:0)

错误是你必须在第一个文件的顶部添加

$pagenum = filter_input(INPUT_GET, 'pagenum', FILTER_SANITIZE_NUMBER_INT);

您无法直接访问$ pagenum而无需为$ _GET参数指定值。

我希望这很有用!