php搜索分页不起作用

时间:2015-08-09 18:26:11

标签: php mysql pagination

我可以通过查询数据库进行分页,它运行得很好。但是当我使用表单从数据库中搜索时,我只能获得第一页数据,下一页数据不会显示出来。

我只是想不通如何维护搜索查询.. 这是我的代码。问题应该在分页中的url链接中,但我只是看不到问题

<?php
require('koneksi.php');


if(isset($_GET['search'])) {
   $search = $_GET['search'];
   $keyword = $_GET['keyword'];

   $koneksi = mysqli_connect("localhost","root","","mycompany");

   // find out how many rows are in the table
   $sql = "SELECT COUNT(*) FROM product WHERE deskripsi LIKE '%" . $keyword . "%'";
   $result = mysqli_query($koneksi,$sql);
   $rss = mysqli_fetch_row($result);
   $numrows = $rss[0];

   //numbers or rows to show per page
   $rowperpage = 6;
   //find out total pages
   $totalpages = ceil($numrows/$rowperpage);

   //get the current page or set default
   if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
       // cast var as int
       $currentpage = (int) $_GET['currentpage'];
   } else {
       // default page number
       $currentpage = 1;
   } // end if

   // if the current page is greater than total pages...
   if($currentpage > $totalpages) {
       // set current page to last page
       $currentpage = $totalpages;
   } // end if

   // if current page is less than total pages...
   if($currentpage < 1) {
        // set current page to first page
        $currentpage = 1;
   } // end if

   // the offset of the list, based on current page
   $offset = ($currentpage - 1) * $rowperpage;

   $sql = "SELECT * FROM product WHERE deskripsi LIKE '%" . $keyword . "%' LIMIT $offset, $rowperpage";

    $result = mysqli_query($koneksi, $sql); 

    // while there are rows to be fetched
    while ($list = mysqli_fetch_assoc($result)) {
        // echo data
        echo $list['product_code'];
        echo "<br>";
        echo $list['deskripsi'];

    }

    /******  build the pagination links ******/
    // range of num links to show
    $range = 6;
    $url = "searchbar.php";
    // if not on page 1, don't show back links
    if($currentpage > 1) {
        // show << link to go to page 1
        echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'> << </a>";

        //get previous page num
        $prevpage = $currentpage - 1;

    } // end if

    echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>&laquo;</a></li> ";

    for($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
        // if it is a valid page number
        if(($x > 0) && ($x <= $totalpages)) {
            // if we are on current page
            if($x == $currentpage) {
                echo "<li class=''><a href=''> $x </a></li>";
            } else {
                // make it a link
                //echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'> $x </a> ";
                //echo "<li class=''><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x </a></li>";
                echo "<li class=''><a href='$url?currentpage=$x?&keyword=$keyword?cari=$cari'> $x </a></li>";
            } // 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 
       echo " <li class='arrow'><a href='$url?currentpage=$nextpage?&keyword=$keyword?cari=$cari'>&raquo;</a></li> ";
       // echo forward link for lastpage
       // echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a> ";
    } // end if

} // end if get search

require('footer.php');
?>

2 个答案:

答案 0 :(得分:1)

看起来这里可能会遗漏几件事。例如:

echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>&laquo;</a></li> ";

在这行中你缺少一个$的prevpage:$prevpage但是URL中的查询字符串应该只以一个?开头?并且在其他地方不包含问号,所以这一行应该更像是

echo " <li class='arrow'><a href='$url?currentpage=$prevpage&keyword=$keyword&cari=$cari'>&laquo;</a></li> ";

我不是百分之百确定这是否能解决您的问题,但在实际使用此代码之前我会要求您查看一件大事,那就是您在查询中使用的SQL注入。

您可能会阅读一些How can I prevent SQL injection in PHP?以更好地了解如何重写SQL查询。

因此,请检查您的链接,确保查询字符串格式正确(http://host.com/script.php?querystring=something&var2=anothervar),其中变量仅由&amp;

分隔

答案 1 :(得分:1)

正如@aaronott指出的那样,你的大部分链接都是错误的。

您正在使用未在任何地方设置的cari=$cari,而实际上我猜您应该添加search=1(或search=$search,但它并不真正有用。)< / p>

另外,你不能超过?在您的查询字符串中,所以修复所有链接,如下所示:

...

if($currentpage > 1) {
    echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1&search=1&keyword=$keyword'> << </a>";
    $prevpage = $currentpage - 1;
}

echo " <li class='arrow'><a href='$url?currentpage=$prevpage&search=1&keyword=$keyword'>&laquo;</a></li> ";

...

if($x == $currentpage) {
    echo "<li class=''><a href=''> $x </a></li>";
} else {
    echo "<li class=''><a href='$url?currentpage=$x&search=1&keyword=$keyword'> $x </a></li>";
} // end else

...

echo " <li class='arrow'><a href='$url?currentpage=$nextpage&search=1&keyword=$keyword'>&raquo;</a></li> ";