Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php搜索分页不起作用_Php_Mysql_Pagination - Fatal编程技术网

php搜索分页不起作用

php搜索分页不起作用,php,mysql,pagination,Php,Mysql,Pagination,我可以通过查询数据库进行分页,效果很好。但当我使用表单从数据库中搜索时,我只能获取第一页数据,下一页数据不会显示 我就是不知道如何维护搜索查询。。 这是我的密码。问题应该出在分页中的url链接上,但我就是看不到问题所在 <?php require('koneksi.php'); if(isset($_GET['search'])) { $search = $_GET['search']; $keyword = $_GET['keyword']; $koneksi =

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

我就是不知道如何维护搜索查询。。 这是我的密码。问题应该出在分页中的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');
?>

看起来这里可能缺少一些东西。例如:

echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>&laquo;</a></li> ";
我不能百分之百确定这是否能解决您的问题,但在实际使用此代码之前,有一件大事我会要求您在任何地方进行研究,那就是查询中的SQL注入

为了更好地了解如何重写sql查询,您可能会阅读一些


因此,签出您的链接,确保querystring的格式正确(),其中变量仅以&

分隔,正如@aaronott所指出的,您的大多数链接都是错误的

您使用的是
cari=$cari
,它没有设置在任何位置,而事实上,我想您应该添加
search=1
(或者
search=$search
,但它不是真正有用的)

此外,您不能拥有超过个?在查询字符串中,请按如下方式修复所有链接:

...

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> ";
。。。
如果($currentpage>1){
回声“”;
...
如果($x==$currentpage){
回声“
  • ”; }否则{ 回声“
  • ”; }//结束其他 ... 回声“
  • ”;
    是的,我在那里漏掉了一个$。但问题是,分页不起作用。第一页显示的结果、第2页的结果等等都是空的。感谢您对sql注入预防的建议。我会在这之后再看一遍。是的,但关键在我提到的其他代码中。在下一页链接中,您现在添加了一个?to您的搜索词。以前,如果您搜索“cat”,您将在第一页上获得%cat%的所有结果。该链接将在搜索词后附加一个?以便它将尝试在所有其他页面上显示%cat?%的结果。非常感谢@aaronott,它现在工作得非常好。我还有很长的路要学
    ...
    
    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> ";