kartul Posted August 6, 2009 Share Posted August 6, 2009 ok. i need to add pagination to search but i have no idea how to do it. i have form at top of the page - work perfectly i have working search - it selects from mysql and displays results fine i also have perfectly working pagination - divides results into different pages my problem is i dont know how do build this all up. i mean, how do bind search and pagination. right now, when i search for something, then it displays first 25 results ( all works so far ). but when i click on next page or choose any page from pagination, it gives me empty page (not blank, just empty, no results taken from db). altho, it gives me right amount of pages as well. i just dont know what i've made wrong that the other pages dont work. i've tried google for help but nothing that helps me so far. heres my code: <form action="search.php" method="post"> <input type="text" name="name" /> <select name="type"> <option value="all">All</option> <?php // there is array for those foreach($sort_by_s AS $num => $type) { echo "<option value=\"".$num."\">".$type."</option>\n"; } ?> </select> <select name="encoder"> <option value="all">All</option> <?php // there is array for those foreach($encoders AS $e) { echo "<option value=\"".$e."\">".$e."</option>\n"; } ?> </select> <input type="submit" name="search" value="Search"/> </form> <?php if(isset($_POST['search'])) { //gets user input from form $name = mss($_POST['name']); $type = mss($_POST['type']); $encoder = mss($_POST['encoder']); //checks if name and type and encoder are selected if($name && $type && $encoder) { //name must be more than 2 chars if(strlen($name) < 2) { echo $name . " " . $type . " " . $encoder . "<br />shorts"; }else { // if type isn't selected then make it empty if($type == "all") { $type = ""; //otherwise search from db for selected type }else { $type = " AND `type`='".$type."'"; } //if encoder isn't selected make it empty if($encoder == "all") { $encoder = ""; //otherwise search from db for selected encoder }else { $encoder = " AND `encoder`='".$encoder."'"; } // search query based on the checking on top $searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder; #echo $sql; //pagination security $page =(!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page']; $page = ceil($page); $limit = 25; $start = $limit; $end = $page*$limit-($limit); if(isset($_GET['page'])) { if(!is_int($start) && !is_int($end)) { echo "ERRRORR"; die; }elseif($start < 0 || $end < 0) { echo "ERRRORR"; die; } } //end of pagination security //selects first 25 results from search query $select_res = mysql_query($searchSql." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error()); while($row = mysql_fetch_assoc($select_res)) { //display results here } //pagination starts //amount check is search query $amount_check = $searchSql; $amount_check_res = mysql_query($amount_check) or die(mysql_error()); $amount_count = mysql_num_rows($amount_check_res); $pages = ceil($amount_count/$limit); $pag = $page - 1; $pagesystem = ""; if($pages == 0) { $pagesystem .= "No pages"; }else { if($pages == 1) { $pagesystem .= "Page 1"; }else { if($pag == 0) { $pagesystem .= "« First "; }else { $pagesystem .= "<a href=\"search.php?page=1\">« First</a> "; } if($pages > 1) { if($page > 1) { $pagesystem .= " <a href=\"search.php?page=".($page - 1)."\">Prev.</a> "; }else { $pagesystem .= " Prev. "; } if(($page - 2) >= 1) { if(($page -2) > 1) { $pagesystem .= "..."; } $pagesystem .= " <a href=\"search.php?page=".($page - 2)."\">".($page - 2)."</a> <a href=\"search.php?page=".($page - 1)."\">".($page - 1)."</a> "; }else { if(($page - 1) >= 1) { $pagesystem .= " <a href=\"search.php?page=".($page - 1)."\">".($page - 1). "</a> "; } } } $pagesystem .= $page . " "; if($pages > 1) { if(($page + 2) <= $pages) { $pagesystem .= " <a href=\"search.php?page=".($page + 1)."\">".($page + 1)."</a> <a href=\"search.php?page=".($page + 2)."\">".($page + 2)."</a> "; if(($page + 2) < $pages) { $pagesystem .= "..."; } }else { if(($page + 1) <= $pages) { $pagesystem .= " <a href=\"search.php?page=".($page + 1)."\">".($page + 1)."</a> "; } } if($page < $pages) { $pagesystem .= " <a href=\"search.php?page=".($page + 1)."\">Next</a> "; }else { $pagesystem .= " Next "; } } if($page == $pages) { $pagesystem .= " Last »"; }else { $pagesystem .= " <a href=\"search.php?page=".($pages)."\">Last »</a> "; } } } //display pagination echo $pagesystem; } }// end of if($name && $type && $encoder) }//end of if(isset[post[search]]) ?> Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/ Share on other sites More sharing options...
MatthewJ Posted August 6, 2009 Share Posted August 6, 2009 I would gess that you need to try and store the query string or the values you use in the query in a session variable that you can pass to the next page. The pagination query is going to run again, but the data being searched for is not being sent with it, so you're running a query that has no matches. Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892264 Share on other sites More sharing options...
kartul Posted August 6, 2009 Author Share Posted August 6, 2009 I would gess that you need to try and store the query string or the values you use in the query in a session variable that you can pass to the next page. The pagination query is going to run again, but the data being searched for is not being sent with it, so you're running a query that has no matches. ok, but how? when i assign name, type and encoder before query then doesn't it get's over written on next page cause then the form is empty? Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892291 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2009 Share Posted August 6, 2009 The thing is you don't submit the form when changing the page so it won't get overwritten. Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892297 Share on other sites More sharing options...
MatthewJ Posted August 6, 2009 Share Posted August 6, 2009 The thing is you don't submit the form when changing the page so it won't get overwritten. Exactly Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892300 Share on other sites More sharing options...
kartul Posted August 6, 2009 Author Share Posted August 6, 2009 of course! ok, I made following changes: // search query based on the checking on top $searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder; #echo $sql; TO // search query based on the checking on top $searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder; #echo $sql; $_SESSION['searchSql'] = $searchSql; //echo $_SESSION['searchSql']; before displaying //selects first 25 results from search query $select_res = mysql_query($searchSql." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error()); TO //selects first 25 results from search query $select_res = mysql_query($_SESSION['searchSql']." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error()); and in pagination $amount_check = $searchSql; TO $amount_check = $_SESSION['searchSql']; ok, basicly. i've changed all querys to use query that is inside the session but still same. all other pages are empty. Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892424 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 there is a tutorial on this site for it: http://www.phpfreaks.com/tutorial/basic-pagination hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892429 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2009 Share Posted August 6, 2009 there is a tutorial on this site for it: http://www.phpfreaks.com/tutorial/basic-pagination hope that helps If you used this you would still have to implement the searching to it separately because the tutorial handles only pagination, not searching + sorting combined to it. Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892471 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 well I assumed he knows how to do a simple mysql search. there is also a tutorial on that: http://www.phpfreaks.com/tutorial/simple-sql-search Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892472 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2009 Share Posted August 6, 2009 kartul, have you made your script also use that $_SESSION query when page is changed? Means about this.. if (isset($_GET['page']) && isset($_SESSION['searchSql'])) { $sql = $_SESSION['searchSql']; // plus any additional info to query that you may have.. } else { $sql = $your_normal_query_here; } // And then get the results and show them. Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892473 Share on other sites More sharing options...
kartul Posted August 7, 2009 Author Share Posted August 7, 2009 there is a tutorial on this site for it: http://www.phpfreaks.com/tutorial/basic-pagination hope that helps like I said, I have working pagination and search. I just don't know how do make them work together. kartul, have you made your script also use that $_SESSION query when page is changed? Means about this.. if (isset($_GET['page']) && isset($_SESSION['searchSql'])) { $sql = $_SESSION['searchSql']; // plus any additional info to query that you may have.. } else { $sql = $your_normal_query_here; } // And then get the results and show them. YES! that's what I needed. now everything works perfectly. Thank You guys.! Quote Link to comment https://forums.phpfreaks.com/topic/169116-solved-adding-pagination-to-search/#findComment-892742 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.