kartul Posted August 3, 2009 Share Posted August 3, 2009 i've been trying to make pagination to search but no luck. search is working. but pagination is not. i've tried many different things but the results are same. next page is always empty. altho it gives me right amount of pages. i think i have to use sessions but no idea how. when I just assaign the post/get values to sessions but then when I click next page then this is still empty. prolly b/c the session variables are overwritten with empty values from form. any ideas how do do it? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 Post your script and lets see. Quote Link to comment Share on other sites More sharing options...
kartul Posted August 3, 2009 Author Share Posted August 3, 2009 <form action="search.php" method="get"> <input type="text" name="name" /> <select name="type"> <option value="all">All</option> <?php foreach($sort_by_s AS $num => $type) { echo "<option value=\"".$num."\">".$type."</option>\n"; } ?> </select> <select name="encoder"> <option value="all">All</option> <?php foreach($encoders AS $e) { echo "<option value=\"".$e."\">".$e."</option>\n"; } ?> </select> <input type="submit" name="search" value="Search"/> </form> <?php if(isset($_GET['search'])) { $name = mss($_GET['name']); $type = mss($_GET['type']); $encoder = mss($_GET['encoder']); if($name && $type && $encoder) { if(strlen($name) < 2) { echo $name . " " . $type . " " . $encoder . "<br />shorts"; }else { if($type == "all") { $type = ""; }else { $type = " AND `type`='".$type."'"; } if($encoder == "all") { $encoder = ""; }else { $encoder = " AND `encoder`='".$encoder."'"; } $searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder; #echo $sql; //searchRes($sql); $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; } } echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"border\"> <tr> <td> <table class=\"en-table\" cellspacing=\"0\" cellpadding=\"3\"> <tr>"; $td = 0; $select_res = mysql_query($searchSql." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error()); while($row = mysql_fetch_assoc($select_res)) { $td++; if($td == 6) { echo "</tr><tr>"; echo "<td class=\"encodes\">\n<font class=\"title\"><a href=\"".$row['topic']."\" target=\"_blank\">".wordwrap($row['title'], 20, "<br />")."</a></font> <font class=\"year\">".$row['year']."\n</font><p class=\"all\">\nEncoded by: <font>".$row['encoder']."</font>\n<br />\n<a href=\"".$row['imdb']."\" target=\"_blank\">IMDb</a> | \n<a href=\"".$row['cover']."\" onmouseover=\"\" title=\"header=[]body=[<img src='".$row['cover']."' alt='cover' />]\">Cover</a>\n<br />In <font>".type($row['type'])."</font>"; if(isset($_SESSION['name'])) { echo "<a href=\"edit.php?id=".$row['id']."\"><img src=\"./img/edit.png\" alt=\"Edit\" width=\"20px\" align=\"right\"></a></p>"; }else { echo "</p>"; } echo "</td>\n"; $td = 1; }else { echo "<td class=\"encodes\">\n<font class=\"title\"><a href=\"".$row['topic']."\" target=\"_blank\">".wordwrap($row['title'], 20, "<br />")."</a></font> <font class=\"year\">".$row['year']."\n</font><p class=\"all\">\nEncoded by: <font>".$row['encoder']."</font>\n<br />\n<a href=\"".$row['imdb']."\" target=\"_blank\">IMDb</a> | \n<a href=\"".$row['cover']."\" onmouseover=\"\" title=\"header=[]body=[<img src='".$row['cover']."' alt='cover' />]\">Cover</a>\n<br />In <font>".type($row['type'])."</font>"; if(isset($_SESSION['name'])) { echo "<a href=\"edit.php?id=".$row['id']."\"><img src=\"./img/edit.png\" alt=\"Edit\" width=\"20px\" align=\"right\"></a></p>"; }else { echo "</p>"; } echo "</td>\n"; } } $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 { //in href there is all the ways i'v tried to do this $pagesystem .= "<a href=\"?sort=search&page=1\">« First</a> "; } if($pages > 1) { if($page > 1) { $pagesystem .= " <a href=\"search.php?name=".$name."&type=".$type."&encoder=".$encoder."&search=Search&page=".($page - 1)."\">Prev.</a> "; }else { $pagesystem .= " Prev. "; } if(($page - 2) >= 1) { if(($page -2) > 1) { $pagesystem .= "..."; } $pagesystem .= " <a href=\"search.php?name=".$name."&type=".$type."&encoder=".$encoder."&search=Search&page=".($page - 2)."\">".($page - 2)."</a> <a href=\"search.php?name=".$name."&type=".$type."&encoder=".$encoder."&search=Search&page=".($page - 1)."\">".($page - 1)."</a> "; }else { if(($page - 1) >= 1) { $pagesystem .= " <a href=\"".$_SERVER['PHP_SELF']."&page=".($page - 1)."\">".($page - 1). "</a> "; } } } $pagesystem .= $page . " "; if($pages > 1) { if(($page + 2) <= $pages) { $pagesystem .= " <a href=\"".$_SERVER['PHP_SELF']."&page=".($page + 1)."\">".($page + 1)."</a> <a href=\"".$_SERVER['PHP_SELF']."&page=".($page + 2)."\">".($page + 2)."</a> "; if(($page + 2) < $pages) { $pagesystem .= "..."; } }else { if(($page + 1) <= $pages) { $pagesystem .= " <a href=\"".$_SERVER['PHP_SELF']."&page=".($page + 1)."\">".($page + 1)."</a> "; } } if($page < $pages) { $pagesystem .= " <a href=\"search.php?name=".$name."&type=".$type."&encoder=".$encoder."&search=Search&page=".($page + 1)."\">Next</a> "; }else { $pagesystem .= " Next "; } } if($page == $pages) { $pagesystem .= " Last »"; }else { $pagesystem .= " <a href=\"".$_SERVER['PHP_SELF']."&page=".($pages)."\">Last »</a> "; } } } echo "</table> </tr> </td> </table> <table border=\"0\" cellspacing=\"0\"> <tr> <td class=\"left\"></td> <td width=\"100%\" height=\"5px\" class=\"pagination\">".$pagesystem."</td> <td class=\"right\"></td> </tr> <tr> <td class=\"spacer\"><img src=\"./img/lower-left-corner.png\"></td> <td width=\"100%\" class=\"lower\"></td> <td class=\"spacer\"><img src=\"./img/lower-right-corner.png\"></td> </tr> </table>"; } } } Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 the $last should be the number of rows returned by your SQL times the number of results you want to show per page.... also make sure you have over 25 rows in your mysql Quote Link to comment Share on other sites More sharing options...
kartul Posted August 3, 2009 Author Share Posted August 3, 2009 the $last should be the number of rows returned by your SQL times the number of results you want to show per page.... also make sure you have over 25 rows in your mysql the pagination itself works well. i'm 100% sure of it. anyone know how the search works in phpbb3 or somewhere? also, any code/tut/website you know where they teach/show how do make such thing is very welcome. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted August 3, 2009 Share Posted August 3, 2009 Your over complicating it. You just need the total and page reference And dont forget ceil Example: $page = 1; $tpage = ceil( $total_pics / 30 ); $startfrom = ( $page - 1 ) * 30; $start_num = $startfrom + 1; $end_num = $startfrom + $total_pics; $prev = $page - 1; $next = $page + 1; $adjust = 9; if ( 1 < $page ) { $page_links_pics .= "<a class=pagination_prev href='/hdpics/".$prev."'><< Prev</a><a class=pagination_next href='/hdpics/1'>1</a>...."; } $k = $page - $adjust; for ( $page ; $k <= $page + $adjust; ++$k ) { if (( 0 < $k ) && ( $k <= $tpage ) ) { if(($page)==($k)){ $page_links_pics.="<span class=\"pagination_active\"> ".$k." </span>"; } else { $page_links_pics .= "<a class=pagination href='/hdpics/".$k."'>{$k}</a>"; } } } if ( $page < $tpage ) { $page_links_pics .= ".....<a class=pagination_next href='/hdpics/".$tpage."'>".$tpage."</a><a class=pagination_next href='/hdpics/".$next."'>Next >></a>"; } Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 3, 2009 Share Posted August 3, 2009 PHPfreaks has a tutorial on it... http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment Share on other sites More sharing options...
kartul Posted August 3, 2009 Author Share Posted August 3, 2009 ok, let me ask other way... how does phpbb3 search works? or how do store post/get data into sessions so I can somehow use sessions to store search query in there. cause example, when I have constant information to pull from database and divide the results into several pages (forum topics (select * from table where forumid=5) <- this query never changes) but my query depends on the user input and it checks if post/get IS submited, then displays. therefore when I click on next page, script checks again and then post/get is empty and gives me no results. there is my porblem that i need help with. pagination itself works perfectly. only thing is the query and i'm not really sure what to put in anchor tags yet. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted August 3, 2009 Share Posted August 3, 2009 You need to $_GET the page and use startfrom in your query http://site.com/index.php?q=searching&page=3 This is an example //get the total for the search here $total = //query here $tpage = ceil( $total / 30 ); $startfrom = ( $page - 1 ) * 30; $sql = "SELECT * FROM table where something ='query' LIMIT {$startfrom}, 30"; $result = mysql_query( $sql ); //now page stuff $content_count = mysql_num_rows( $result ); $start_num = $startfrom + 1; $end_num = $startfrom + $content_count; $prev = $page - 1; $next = $page + 1; $adjust = 9; if ( 1 < $page ) { $page_links .= "<a class=pagination_prev href='".$prev."'><< Prev</a><a class=pagination_next href='1'>1</a>...."; } $k = $page - $adjust; for ( $page ; $k <= $page + $adjust; ++$k ) { if (( 0 < $k ) && ( $k <= $tpage ) ) { if(($page)==($k)){ $page_links.="<span class=\"pagination_active\"> ".$k." </span>"; } else { $page_links .= "<a class=pagination href='".$k."'>{$k}</a>"; } } } if ( $page < $tpage ) { $page_links .= ".....<a class=pagination_next href='".$tpage."'>".$tpage."</a><a class=pagination_next href='".$next."'>Next >></a>"; } Quote Link to comment Share on other sites More sharing options...
kartul Posted August 4, 2009 Author Share Posted August 4, 2009 You need to $_GET the page and use startfrom in your query http://site.com/index.php?q=searching&page=3 This is an example //get the total for the search here $total = //query here $tpage = ceil( $total / 30 ); $startfrom = ( $page - 1 ) * 30; $sql = "SELECT * FROM table where something ='query' LIMIT {$startfrom}, 30"; $result = mysql_query( $sql ); //now page stuff $content_count = mysql_num_rows( $result ); $start_num = $startfrom + 1; $end_num = $startfrom + $content_count; $prev = $page - 1; $next = $page + 1; $adjust = 9; if ( 1 < $page ) { $page_links .= "<a class=pagination_prev href='".$prev."'><< Prev</a><a class=pagination_next href='1'>1</a>...."; } $k = $page - $adjust; for ( $page ; $k <= $page + $adjust; ++$k ) { if (( 0 < $k ) && ( $k <= $tpage ) ) { if(($page)==($k)){ $page_links.="<span class=\"pagination_active\"> ".$k." </span>"; } else { $page_links .= "<a class=pagination href='".$k."'>{$k}</a>"; } } } if ( $page < $tpage ) { $page_links .= ".....<a class=pagination_next href='".$tpage."'>".$tpage."</a><a class=pagination_next href='".$next."'>Next >></a>"; } ok. this show quite good how i'm supposed to do that. thank you.! but one more thing. i have search.php page build like this: header - navigations and search box. body - search results. now. my body starts with if(isset($_GET['search'])) {} - do I need that? Quote Link to comment 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.