nita Posted June 10, 2007 Share Posted June 10, 2007 Hi I have problem with results pagination. When search is applied browser is showing me first 10 results, and links to other pages, but there is a problem with these links (passing variables i think). When i press on one of the links browser is showing me info that search input must be longer then 2 char. (so it clear that search input has not been passed thru the link.) Here is the code: else if(isset($_POST['submit'])||isset($_POST['search'])||isset($_GET['search'])) // when search button is pressed { $PHP_SELF = $_SERVER['HTTP_REFERER']; $search=$_POST["search"]; $minchar = 2; if (strlen($search) < $minchar) { echo "<span class='info1'>Search must be longer than 2 characters.</span> } else { if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } $max_results = 10; $from = (($page * $max_results) - $max_results); $result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' OR cast LIKE '%$search%' OR director LIKE '%$search%' OR year LIKE '%$search%' OR production LIKE '%$search%' OR genere LIKE '%$search%' OR language LIKE '%$search%' ORDER BY name LIMIT $from, $max_results"); $numrows=mysql_num_rows($result); if ($numrows == 0) { echo "<p>Sorry, your search returned 0 results</p> } else { while($row=mysql_fetch_array($result)) { include "display_rec.php"; } $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM movies WHERE name LIKE '%$search%' OR cast LIKE '%$search%' OR director LIKE '%$search%' OR year LIKE '%$search%' OR production LIKE '%$search%' OR genere LIKE '%$search%' OR language LIKE '%$search%' ORDER BY name"),0); // Figure out the total number of pages. $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks // Build Previous Link if($page > 1){ $prev = ($page - 1); echo " <td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$prev\">Previous</a></td> <td width='2'></td> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "<td class='nav'>$i</td> <td width='2'></td>"; } else { echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$i\">$i</a></td> <td width='2'></td>"; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$next\">Next</a><td>"; } } } } go to http://www.webdev.nita-on-line.com/new/movies.php to see this script in action - (search words that will give you more than 10 results - james, red - for example) I need some help with this one .... Thank you in advance. Nita Link to comment https://forums.phpfreaks.com/topic/54955-solved-search-results-pagination-problem/ Share on other sites More sharing options...
paul2463 Posted June 10, 2007 Share Posted June 10, 2007 you problems stems from the fact that $search is only set by $_POST['search'], when you click a pagination link it changes to a $_GET['search'] variable. I have altered your code slightly for you to incorporate this change from POST to GET <?php else if(isset($_POST['submit'])||isset($_POST['search'])||isset($_GET['search'])) // when search button is pressed { $PHP_SELF = $_SERVER['HTTP_REFERER']; //this is the changed bit here if (isset $_GET['search']) //if this is set then a pagination link has been clicked { $search = $_GET['search']; //use this as the search string } else { $search=$_POST["search"]; //otherwise use this } $minchar = 2; if (strlen($search) < $minchar) { echo "<span class='info1'>Search must be longer than 2 characters.</span> } else { if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } $max_results = 10; $from = (($page * $max_results) - $max_results); $result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' OR cast LIKE '%$search%' OR director LIKE '%$search%' OR year LIKE '%$search%' OR production LIKE '%$search%' OR genere LIKE '%$search%' OR language LIKE '%$search%' ORDER BY name LIMIT $from, $max_results"); $numrows=mysql_num_rows($result); if ($numrows == 0) { echo "<p>Sorry, your search returned 0 results</p> } else { while($row=mysql_fetch_array($result)) { include "display_rec.php"; } $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM movies WHERE name LIKE '%$search%' OR cast LIKE '%$search%' OR director LIKE '%$search%' OR year LIKE '%$search%' OR production LIKE '%$search%' OR genere LIKE '%$search%' OR language LIKE '%$search%' ORDER BY name"),0); // Figure out the total number of pages. $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks // Build Previous Link if($page > 1){ $prev = ($page - 1); echo " <td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$prev\">Previous</a></td> <td width='2'></td> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "<td class='nav'>$i</td> <td width='2'></td>"; } else { echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$i\">$i</a></td> <td width='2'></td>"; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$next\">Next</a><td>"; } } } } ?> Link to comment https://forums.phpfreaks.com/topic/54955-solved-search-results-pagination-problem/#findComment-271759 Share on other sites More sharing options...
nita Posted June 10, 2007 Author Share Posted June 10, 2007 Thank you paul2463 I applied your change and is working, but another issue came up. While browsing search results (on pages wher number more then 1) i cant do another search (browser is displaying records from previous search). Have no clue why, seems like variable search is not getting new value. go to http://webdev.nita-on-line.com/new/movies.php?search=james&page=2 to check it out ... Need some help Nita Link to comment https://forums.phpfreaks.com/topic/54955-solved-search-results-pagination-problem/#findComment-271781 Share on other sites More sharing options...
paul2463 Posted June 10, 2007 Share Posted June 10, 2007 The new problem now stems from what I did for you, because when you start a new search the $_GET variable is strill set so the new $_POST variable never gets set try changing the if part around such as if (isset $_POST['search']) //if this is set then a new search has been initated { $search = $_POST['search']; //use this as the search string } else { $search=$_GET["search"]; //otherwise continue with this } Link to comment https://forums.phpfreaks.com/topic/54955-solved-search-results-pagination-problem/#findComment-271787 Share on other sites More sharing options...
nita Posted June 10, 2007 Author Share Posted June 10, 2007 Thanks again... i spot the bug, it was in search form, i had to change value of action from $php_self to movies.php and is all good now. Just another think regarding result paging. What i would like to do is that when results are on 1 page, browser will not display number of the page. As is doing now. I have following code for paging results. // Build Page Number Hyperlinks echo " <table width='0' border='0' cellspacing='0' cellpadding='3' align='center'> <tr>"; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo " <td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$prev\">Previous</a></td><td width='2'></td> "; } for($i = 1; $i <= $total_pages; $i++) { if(($page) == $i){ echo "<td class='nav'>$i</td><td width='2'></td>"; } else { echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$i\">$i</a></td><td width='2'></td>"; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$next\">Next</a><td>"; } echo "</tr> </table>"; Any advice... Thank you in advance Nita Link to comment https://forums.phpfreaks.com/topic/54955-solved-search-results-pagination-problem/#findComment-271796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.