php - how pagination work with sorting and search function -
i facing problem in pagination, searching , sorting function. function if dint combine pagination. once sorting, result did not change @ every page mean change in current page. example, @ page 2 ,then sorting,the result in page 2 have change due sort by.however, other page did not change , still default.may know going wrong. appreciate help.
<?php include('db_connection.php'); $sql = "select count(*) product"; $r = mysql_fetch_array(mysql_query($sql)); $numrows = $r[0]; // number of rows show per page $rowsperpage = 2; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // current page or set default if (isset($_get['currentpage']) && is_numeric($_get['currentpage'])) { // cast var int $currentpage = (int) $_get['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page greater total pages... if ($currentpage > $totalpages) { // set current page last page $currentpage = $totalpages; } // end if // if current page less first page... if ($currentpage < 1) { // set current page first page $currentpage = 1; } // end if // offset of list, based on current page $offset = ($currentpage - 1) * $rowsperpage; $sorder = "order product_id asc "; if(isset($_post["submit"])) { $sort = $_post["sort"]; if($sort == "latest") { $sorder = "order date_update desc "; } else if ($sort == "lp") { $sorder = "order pro_price asc"; } else if ($sort == "hp") { $sorder = "order pro_price desc" ; } else if ($sort == "az") { $sorder = "order pro_name asc"; } else if ($sort == "za") { $sorder = "order pro_name desc"; } else { $sorder = " "; } if ( isset($_post['ssearch']) && $_post['ssearch'] != "" ) { $search=mysql_real_escape_string( $_post['ssearch'] ); $swhere = " pro_name '%".mysql_real_escape_string( $_post['ssearch'] )."%' " ; } else{ $swhere = " "; } $currentpage = 1; } ?> <html> <head> </head> <body> <form method = "post"> sort <select name ="sort"> <option value="all" checked>all</option> <option value="latest">latest item</option> <option value="lp">lowest price first</option> <option value="hp">highest price first</option> <option value="az">alphabets a-z</option> <option value="za">alphabets z-a</option> </select> <input type="text" name="ssearch" /> <input type="submit" name="submit" value="submit"> </form> <?php // info db $sql = "select * product $swhere $sorder limit $offset, $rowsperpage"; $result = mysql_query($sql) ; // while there rows fetched... while ($list = mysql_fetch_assoc($result)) { $pid = $list['product_id']; ?> <div class='grid'> <ul class="products"> <li class="cart_items"> <a href="prodetails.php?pid=<?php echo $pid ?>" class="product-image"> <img src="<?php echo "product/".$list['pro_photo']; ?>" style="max-height:140px;max- width:140px" /> <?php echo "<br/>".htmlspecialchars($list['pro_name']); ?> </a> <?php echo "<br/> rm ".$list['pro_price']; ?> <form name = "btn" method = "post" action="" > <input type="hidden" name="product_id" value="<?php echo $pid ?>"/> <input type="number" name="qty" min="1" max="30" value="1" size="30" /> </br> <a href="" name="favourite" title="favourite"><img src="images/heart-add-icon.png" name="favourite" onclick=""/></a> <input type="submit" name="add" value="add cart" class="add-to-cart" /> </form> </li> </ul>
<?php } /****** build pagination links ******/ // range of num links show $range = 3; // if not on page 1, don't show links if ($currentpage > 1) { // show << link go page 1 echo " <a href='{$_server['php_self']}?currentpage=1'><<</a> "; // previous page num $prevpage = $currentpage - 1; // show < link go 1 page echo " <a href='{$_server['php_self']}?currentpage=$prevpage'><</a> "; } // end if // loop show links range of pages around current page ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' don't make link echo " [<b>$x</b>] "; // if not current page... } else { // make link echo " <a href='{$_server['php_self']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end // if not on last page, show forward , last page links if ($currentpage != $totalpages) { // next page $nextpage = $currentpage + 1; // echo forward link next page echo " <a href='{$_server['php_self']}?currentpage=$nextpage'>></a> "; // echo forward link lastpage echo " <a href='{$_server['php_self']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?> </body> </html>
in first line retrieving database records count. therefore $numrows
, $totalpages
wrong , equal rows retrieved sorting without search.
to real counting should 1st build search query. example:
$querypart = ' table ...... order ....'; $countquery = 'select count(*) '.$querypart; $query = 'select * '.$querypart;
now if execute $countquery
row count given search , sort. , executing $query
rows. of course should add ad end of $query .= ' limit '.($page*$onpage).','.$onpage';
retrieve required page of rows instead of everyone.
Comments
Post a Comment