-1

i can make pagination by query database and it works just fine. but when i use form to search from database, i can only get the first page data, the next page data won't show up.

i just cannot figure out how to maintain the search query.. this is my code. the problem should be in the url links in the pagination, but i just cannot see the problem

<?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');
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
lapmer
  • 13
  • 2

2 Answers2

1

Looks like there could be a couple things missing here. For instance:

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

in this line you are missing a $ for the prevpage: $prevpage but the querystring in the URL should only start with a ? and not contain question marks elsewhere, so this line should read more like

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

I'm not 100% sure if that's going to fix your issue, but there is one big thing that I would ask you to look into before actually using this code anywhere and that's the SQL Injection that you have in your query.

You might read a bit of How can I prevent SQL injection in PHP? to get a better idea of how to rewrite your sql queries.

So, checkout your links, make sure the querystring is formatted correctly (http://host.com/script.php?querystring=something&var2=anothervar) where variables are separated only by &

Community
  • 1
  • 1
aaronott
  • 396
  • 1
  • 6
  • yes, i miss a $ there. but the problem here is, the pagination is not work. the result just show for first page, result for page 2 and so on is empty. thanks for your suggestion for sql injection prevention. i will go through it after this one – lapmer Aug 09 '15 at 18:55
  • Yes, but the key is in the other code I mentioned. In the next page link, you are now adding a ? to your search term. Previously if you searched for 'cat' you'd get all the results for %cat% on the first page. The link would append a ? to the search term so it would then try and show results for %cat?% on all other pages. – aaronott Aug 09 '15 at 18:58
  • thanks a lot @aaronott , it works wonderful now.. i stll have long way to learn – lapmer Aug 09 '15 at 19:08
1

As @aaronott pointed out, most of your links are wrong.

You are using cari=$cari which is not set anywhere, while in fact I guess you should add search=1 (or search=$search, but it's not really useful).

Also, you cannot have more than on ? in your querystring, so fix all your links like this:

...

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> ";
Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30