jon4433 Posted July 25, 2012 Share Posted July 25, 2012 I'm using a pagination script for the front of my statistics website, and now i'm trying to put it in my search page. The search feature works, it displays the found user's. But it also displays everyone else on the next page. I think it's gotta be one of the queries, but I can't seem to find it.... Can anybody help me out, please? <?php include 'db.php'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link REL="SHORTCUT ICON" HREF="http://www.dawncraftmc.com/stats/favicon.ico"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/style.css" rel="stylesheet" type="text/css" /> <title>Dawncraft Stats</title> <style type="text/css"> body { background-color: #E9E9E9; background-image: url(images/stripe_8c8e7da1ce9e02b0e3ad46e2f2896ec2.png); } </style> </head> <body> <div class="top_holder"> <div class="top_inner"> <table width="100%" border="0" cellspacing="5"> <tr> <td width="73%"><a href="index.php"><img src="images/logo.png" width="150" height="40" /></a></td> <td width="27%"> <form action="search.php" method="POST"> <input type="text" size="15" name="searchterm" id="s" placeholder="Search..."/> <input type="submit" class="search_button" value=""/> </form> </td> </tr> </table> </div> </div> <div class="menu" align="center"> <a href="index.php"><img src="images/home.png" width="40" height="20" /></a> <a href="help.php"><img src="images/help.png" width="40" height="20" /></a> <a href="statistics.php"><img src="images/statistics.png" width="55" height="20" /></a> </div> <div class="search_content"> <br /> <br /> <table width="90%" border="0" align="center" cellspacing="0" class="main_table"> <?php echo "<tr bgcolor='#5C5C5C', align=\"center\"><td><font color='white'>Status</font></td><td><font color='white'>Username</font></td><td><font color='white'>First Seen</font></td><td><font color='white'>Last Seen</font></td></tr>"; $id = $name['id']; // How many adjacent pages should be shown on each side? $adjacents = 4; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(*) as num FROM players"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; /* Setup vars for query. */ $targetpage = "search.php"; //your file name (the name of this file) $limit = 45; //how many items to show per page $page = $_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 $search = mysql_real_escape_string(trim($_POST['searchterm'])); $find_users = mysql_query("SELECT * FROM players WHERE username LIKE '%$search%' ORDER BY id LIMIT $start, $limit"); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\" align=\"center\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev\">previous</a>"; else $pagination.= "<span class=\"disabled\">previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next\">next</a>"; else $pagination.= "<span class=\"disabled\">next</span>"; $pagination.= "</div>\n"; } while($row = mysql_fetch_assoc($find_users)){ if($row['isonline'] == 1){ echo "<tr bgcolor='#BFFFFF'><td bgcolor='#00FF00'><center>Online</center></td>"; }else{ echo "<tr bgcolor='#BFFFFF'><td bgcolor='#FF0000'><center>Offline</center></td>"; } echo "<td><center><a href='user.php?id=" . $row['id'] . "'>".$row['username']."</a></center></td>"; echo "<td><center>".date('d/m/Y H:i:s', ($row['firstseen'] / 1000))."</center></td>"; echo "<td><center>".date('d/m/Y H:i:s', ($row['lastseen'] / 1000))."</center></td></tr>"; } ?> </table> <?=$pagination?> </div> <div class="footer_holder" align="center"> <p><b>Dawncraft Stats (en) Version 1.0 (01/07/2012) - (c) 2012 www.dawncraftmc.com | Made By Dtoyee</b></p> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266241-pagination-with-search-results/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 25, 2012 Share Posted July 25, 2012 1) The WHERE clause you are building and putting into the second query, must also be put into the first (COUNT()) query so that the $total_pages calculation will be correct for the number of records that match that WHERE clause. 2) You must carry the $_POST['searchterm'] value as a $_GET parameter in the url so that it will be present on each page request. Changing your search form to use method='get' would be acceptable since you are specifying what the page should output as the result of a request. 3) The pagination links that you produce must carry any existing $_GET parameters and just set the $_GET['page'] value. See the following post for how you can produce the pagination links so that they only set the $_GET parameter used for pagination and leave all other $_GET parameters as is - http://forums.phpfreaks.com/index.php?topic=348834.msg1646676#msg1646676 Quote Link to comment https://forums.phpfreaks.com/topic/266241-pagination-with-search-results/#findComment-1364317 Share on other sites More sharing options...
peipst9lker Posted July 26, 2012 Share Posted July 26, 2012 I can recommend DataTables. It's an jQuery extension which automatically provides pagination, livesearch box and tons of more features (highly configurable and extendable) You should take a look at it Quote Link to comment https://forums.phpfreaks.com/topic/266241-pagination-with-search-results/#findComment-1364494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.