sford999 Posted May 28, 2007 Share Posted May 28, 2007 Hi all I`ve built a script to search a db, and to show 20 results per page. The problem I`m having is that when I click to view "Page 2/3/4/5 etc" I get zero results, like the variables are being erased when I click to view the next 20 results. Page 1 of the search results: Page 2 of the search results: There's too much information to be parsed via the url ($_GET method) in my opinion, and I was wondering what, if anything I`d done wrong for the next pages to show up as blank. This is the code so far: <?php include('functions.php'); $p = isset($_GET["p"]) ? $_GET["p"] : $_POST["p"]; switch($p) { case "results": $name = mysql_real_escape_string($_POST['name']); $town = mysql_real_escape_string($_POST['town']); $county = mysql_real_escape_string($_POST['county']); $postcode = mysql_real_escape_string($_POST['postcode']); $disabled = mysql_real_escape_string($_POST['disabled']); $night_fishing = mysql_real_escape_string($_POST['night_fishing']); $parking_onsite = mysql_real_escape_string($_POST['parking_onsite']); $toilets_onsite = mysql_real_escape_string($_POST['toilets_onsite']); $disabled_toilets = mysql_real_escape_string($_POST['disabled_toilets']); $accomodation = mysql_real_escape_string($_POST['accomodation']); $barbless = mysql_real_escape_string($_POST['barbless']); $boilies = mysql_real_escape_string($_POST['boilies']); $groundbait = mysql_real_escape_string($_POST['groundbait']); $unhooking_mats = mysql_real_escape_string($_POST['unhooking_mats']); $fishery_type = mysql_real_escape_string($_POST['fishery_type']); //----------------------------------------- // Start Building the query //----------------------------------------- if($fishery_type == "2") { $whereParts[] = "fishery_type = '2'"; } if($fishery_type == "1") { $whereParts[] = "fishery_type = '1'"; } if($fishery_type == "0") { $whereParts[] = "fishery_type = '0'"; } if($town) { $whereParts[] = "town LIKE '%$town%'"; } if($county == "0") { /* Don`t do anything */ } else { $whereParts[] = "county = '$county'"; } if($postcode) { $whereParts[] = "postcode LIKE '%$postcode%'"; } if($disabled) { $whereParts[] = "disabled_access = '$disabled'"; } if($night_fishing) { $whereParts[] = "night_fishing_allowed = '$night_fishing'"; } if($parking_onsite) { $whereParts[] = "parking_onsite = '$parking_onsite'"; } if($toilets_onsite) { $whereParts[] = "toilets_onsite = '$toilets_onsite'"; } if($disabled_toilets) { $whereParts[] = "disabled_toilets = '$disabled_toilets'"; } if($accomodation) { $whereParts[] = "accomodation = '$accomodation'"; } if($barbless) { $whereParts[] = "barbless_only = '$barbless'"; } if($boilies) { $whereParts[] = "boilies_allowed = '$boilies'"; } if($groundbait) { $whereParts[] = "groundbait_allowed = '$groundbait'"; } if($unhooking_mats) { $whereParts[] = "unhooking_mats_compulsary = '$unhooking_mats'"; } $WhereClause = implode(' AND ', $whereParts); db_connect(); $sql = "SELECT * FROM fisheries WHERE " . $WhereClause; $result = mysql_query($sql) or die(sql_error(mysql_error(), $_SERVER['REQUEST_URI'], $_SERVER['REMOTE_ADDR'], getenv($_SERVER['REMOTE_ADDR']), $sql)); if(!$result) { show_header($title = "Search Results", $active = 'search_fisheries'); echo 'Sorry, we didn`t find anything matching your search query, please go back and try again.'; show_footer(); exit(); } $num_rows = mysql_num_rows($result); // Get The page (Page ID) $page = addslashes($_GET['page']); if(!$page) { // If page is blank set it to 1 $page = 1; } // Define the number of results per page $max_results = 20; // Figure out the limit for the query based on the current page number. $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results $sql = "SELECT * FROM fisheries WHERE ".$WhereClause." AND auth = '1' ORDER BY id ASC LIMIT $from, $max_results"; $result = mysql_query($sql); show_header($title = "Search Results", $active = 'search_fisheries'); echo '<p>We have '.$num_rows.' results matching your query.</p>'; while($row = mysql_fetch_array($result)) { extract($row); echo '<p><a href="fisheries.php?id='.$id.'">'.$name.' in '.$town.'</a><br />'; } echo '<br /><br />'; // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM fisheries WHERE ".$WhereClause." AND auth = '1'"),0); // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks // Build Previous Link if($page > 1) { $prev = ($page -1); echo '<a href="'.$PHP_SELF.'?p=results&page='.$prev.'"><span class="pagelink">Previous Page</span></a> '; } // Current Page id for($i = 1; $i <= $total_pages; $i++) { if(($page) == $i) { echo '<span class="pagecurrent"><strong>'.$i.'</strong></span> '; } else { echo '<a href="new_search.php?p=results&page='.$i.'"><span class="pagelink">'.$i.'</span></a> '; } } // Build Next Link if($page < $total_pages) { $next = ($page +1); echo '<a href="new_search.php?p=results&page='.$next.'"><span class="pagelink">Next Page</span></a>'; } echo '<p><a href="new_search.php?p=fisheries"><< Search Again</a></p>'; show_footer(); break; // Search Form case "fisheries": show_header($title = "Search Fisheries", $active = 'search_fisheries'); echo '<form id="fisheries_search" name="fisheries_search" method="post" action="new_search.php?p=results"> <table width="98%" border="0" class="tablea"> <tr> <td colspan="2" class="table_header"><div align="center"> <h3>Search Fisheries</h3> </div></td> </tr> <tr> <td width="50%" class="tablec" class="tablec">Fishery Name </td> <td width="50%" class="tablec"><input name="name" type="text" class="forms" size="24" id="name" /></td> </tr> <tr> <td width="50%" class="tablec">Town</td> <td width="50%" class="tablec"><input name="town" type="text" class="forms" size="24" id="town" /></td> </tr> <tr> <td width="50%" class="tablec">County</td> <td width="50%" class="tablec">'; db_connect(); $sql = "SELECT * FROM counties ORDER BY county_name ASC"; echo '<select name="county" class="forms"> <option value="0" selected>All Counties</option>'; $result = mysql_query($sql) or die(sql_error(mysql_error(), $_SERVER['REQUEST_URI'], $_SERVER['REMOTE_ADDR'], getenv($_SERVER['REMOTE_ADDR']), $sql)); while($row = mysql_fetch_array($result)) { extract($row); echo '<option value="'.$id.'">'.$county_name.'</option>'; } echo '</td> </tr> <tr> <td width="50%" class="tablec">Postcode</td> <td width="50%" class="tablec"><input name="postcode" type="text" class="forms" size="24" class="forms" id="postcode" /></td> </tr> <tr> <td width="50%" class="tablec">Disabled Access</td> <td width="50%" class="tablec"> <input name="disabled" type="radio" value="1" checked="checked" />Yes <input name="disabled" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Parking Onsite</td> <td width="50%" class="tablec"> <input name="parking_onsite" type="radio" value="1" checked="checked" />Yes <input name="parking_onsite" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Toilets Onsite</td> <td width="50%" class="tablec"> <input name="toilets_onsite" type="radio" value="1" checked="checked" />Yes <input name="toilets_onsite" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Disabled Toilets</td> <td width="50%" class="tablec"> <input name="disabled_toilets" type="radio" value="1" checked="checked" />Yes <input name="disabled_toilets" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Night Fishing</td> <td width="50%" class="tablec"> <input name="night_fishing" type="radio" value="1" checked="checked" />Yes <input name="night_fishing" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Accomodation</td> <td width="50%" class="tablec"> <input name="accomodation" type="radio" value="1" checked="checked" />Yes <input name="accomodation" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Barbless Hooks Only</td> <td width="50%" class="tablec"> <input name="barbless" type="radio" value="1" checked="checked" />Yes <input name="barbless" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Boilies Allowed</td> <td width="50%" class="tablec"> <input name="boilies" type="radio" value="1" checked="checked" />Yes <input name="boilies" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Groundbait Allowed</td> <td width="50%" class="tablec"> <input name="groundbait" type="radio" value="1" checked="checked" />Yes <input name="groundbait" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Unhooking Mats Compulsary</td> <td width="50%" class="tablec"> <input name="unhooking_mats" type="radio" value="1" checked="checked" />Yes <input name="unhooking_mats" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Fishery Type:</td> <td width="50%" class="tablec"> <input name="fishery_type" type="radio" value="1" checked="checked" />Coarse <br /> <input name="fishery_type" type="radio" value="0" />Game <br /> <input name="fishery_type" type="radio" value="2" />Game & Coarse </td> </tr> <tr> <td colspan="2" class="tablec"><div align="center"> <input type="Submit" name="Submit" class="forms" value="Start Search" /> </div></td> </tr> </table> </form>'; show_footer(); break; default: // Temp redirect until I get the above working header("Location:new_search.php?p=fisheries"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53226-mysql-search-pagination-problems/ Share on other sites More sharing options...
AndyB Posted May 28, 2007 Share Posted May 28, 2007 At first glance it looks as though none of the values required to construct the query ever get passed to page 2. You might consider saving the complete query string (except for the LIMIT start value) in a session variable and retrieving it on each page before querying the database again. Quote Link to comment https://forums.phpfreaks.com/topic/53226-mysql-search-pagination-problems/#findComment-262993 Share on other sites More sharing options...
sford999 Posted May 28, 2007 Author Share Posted May 28, 2007 OK I`ll openly admit that I`m terrible with sessions and its not my favourite part of php I`m able to put the query into the session so that when I echo out the session it outputs whats stored in $WhereClause eg: echo $_SESSION['query']; Will show as: fishery_type = '1' AND parking_onsite = '1' AND toilets_onsite = '1' AND barbless_only = '1' AND boilies_allowed = '1' AND groundbait_allowed = '1' AND unhooking_mats_compulsary = '1' So I added these lines: <?php if(isset($_SESSION['query'])) { $sql = "SELECT * FROM fisheries WHERE ".$_SESSION['query']; } else { $sql = "SELECT * FROM fisheries WHERE ".$WhereClause; } ?> So the code now looks like this: <?php error_reporting(E_ALL); include('functions.php'); $p = isset($_GET["p"]) ? $_GET["p"] : $_POST["p"]; switch($p) { case "results": // Get The page (Page ID) $page = $_GET['page']; if(!$page) { // If page is blank set it to 1 $page = 1; } $name = mysql_real_escape_string($_POST['name']); $town = mysql_real_escape_string($_POST['town']); $county = mysql_real_escape_string($_POST['county']); $postcode = mysql_real_escape_string($_POST['postcode']); $disabled = mysql_real_escape_string($_POST['disabled']); $night_fishing = mysql_real_escape_string($_POST['night_fishing']); $parking_onsite = mysql_real_escape_string($_POST['parking_onsite']); $toilets_onsite = mysql_real_escape_string($_POST['toilets_onsite']); $disabled_toilets = mysql_real_escape_string($_POST['disabled_toilets']); $accomodation = mysql_real_escape_string($_POST['accomodation']); $barbless = mysql_real_escape_string($_POST['barbless']); $boilies = mysql_real_escape_string($_POST['boilies']); $groundbait = mysql_real_escape_string($_POST['groundbait']); $unhooking_mats = mysql_real_escape_string($_POST['unhooking_mats']); $fishery_type = mysql_real_escape_string($_POST['fishery_type']); //----------------------------------------- // Start Building the query //----------------------------------------- if($fishery_type == "2") { $whereParts[] = "fishery_type = '2'"; } if($fishery_type == "1") { $whereParts[] = "fishery_type = '1'"; } if($fishery_type == "0") { $whereParts[] = "fishery_type = '0'"; } if($town) { $whereParts[] = "town LIKE '%$town%'"; } if($county == "0") { /* Don`t search via county */ } else { $whereParts[] = "county = '$county'"; } if($postcode) { $whereParts[] = "postcode LIKE '%$postcode%'"; } if($disabled) { $whereParts[] = "disabled_access = '$disabled'"; } if($night_fishing) { $whereParts[] = "night_fishing_allowed = '$night_fishing'"; } if($parking_onsite) { $whereParts[] = "parking_onsite = '$parking_onsite'"; } if($toilets_onsite) { $whereParts[] = "toilets_onsite = '$toilets_onsite'"; } if($disabled_toilets) { $whereParts[] = "disabled_toilets = '$disabled_toilets'"; } if($accomodation) { $whereParts[] = "accomodation = '$accomodation'"; } if($barbless) { $whereParts[] = "barbless_only = '$barbless'"; } if($boilies) { $whereParts[] = "boilies_allowed = '$boilies'"; } if($groundbait) { $whereParts[] = "groundbait_allowed = '$groundbait'"; } if($unhooking_mats) { $whereParts[] = "unhooking_mats_compulsary = '$unhooking_mats'"; } $WhereClause = implode(' AND ', $whereParts); $_SESSION['query'] = $WhereClause; db_connect(); /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// if(isset($_SESSION['query'])) { $sql = "SELECT * FROM fisheries WHERE ".$_SESSION['query']; } else { $sql = "SELECT * FROM fisheries WHERE ".$WhereClause; } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// $result = mysql_query($sql) or die(sql_error(mysql_error(), $_SERVER['REQUEST_URI'], $_SERVER['REMOTE_ADDR'], getenv($_SERVER['REMOTE_ADDR']), $sql)); if(!$result) { show_header($title = "Search Results", $active = 'search_fisheries'); echo 'Sorry, we didn`t find anything matching your search query, please go back and try again.'; show_footer(); exit(); } $num_rows = mysql_num_rows($result); // Define the number of results per page $max_results = 20; // Figure out the limit for the query based on the current page number. $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// if(isset($_SESSION['query'])) { $sql = "SELECT * FROM fisheries WHERE ".$_SESSION['query']." AND auth = '1' ORDER BY id ASC LIMIT $from, $max_results"; } else { $sql = "SELECT * FROM fisheries WHERE ".$WhereClause." AND auth = '1' ORDER BY id ASC LIMIT $from, $max_results"; } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// $result = mysql_query($sql); show_header($title = "Search Results", $active = 'search_fisheries'); echo '<p>We have '.$num_rows.' results matching your query.</p>'; while($row = mysql_fetch_array($result)) { extract($row); echo '<p><a href="fisheries.php?id='.$id.'">'.$name.' in '.$town.'</a><br />'; } echo $_SESSION['query']; echo '<br /><br />'; // Figure out the total number of results in DB: /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// if(isset($_SESSION['query'])) { $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM fisheries WHERE ".$_SESSION['query']." AND auth = '1'"),0); } else { $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM fisheries WHERE ".$WhereClause." AND auth = '1'"),0); } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks // Build Previous Link if($page > 1) { $prev = ($page -1); echo '<a href="'.$_SERVER['PHP_SELF'].'?p=results&page='.$prev.'"><span class="pagelink">Previous 20 Results</span></a> '; } // Build Next Link if($page < $total_pages) { $next = ($page +1); echo '<a href="'.$_SERVER['PHP_SELF'].'?p=results&page='.$next.'"><span class="pagelink">Next 20 Results</span></a>'; } echo '<p><a href="'.$_SERVER['PHP_SELF'].'?p=fisheries"><< Search Again</a></p>'; show_footer(); break; // Search Form case "fisheries": show_header($title = "Search Fisheries", $active = 'search_fisheries'); echo '<form id="fisheries_search" name="fisheries_search" method="post" action="new_search.php?p=results"> <table width="98%" border="0" class="tablea"> <tr> <td colspan="2" class="table_header"><div align="center"> <h3>Search Fisheries</h3> </div></td> </tr> <tr> <td width="50%" class="tablec" class="tablec">Fishery Name </td> <td width="50%" class="tablec"><input name="name" type="text" class="forms" size="24" id="name" /></td> </tr> <tr> <td width="50%" class="tablec">Town</td> <td width="50%" class="tablec"><input name="town" type="text" class="forms" size="24" id="town" /></td> </tr> <tr> <td width="50%" class="tablec">County</td> <td width="50%" class="tablec">'; db_connect(); $sql = "SELECT * FROM counties ORDER BY county_name ASC"; echo '<select name="county" class="forms"> <option value="0" selected>All Counties</option>'; $result = mysql_query($sql) or die(sql_error(mysql_error(), $_SERVER['REQUEST_URI'], $_SERVER['REMOTE_ADDR'], getenv($_SERVER['REMOTE_ADDR']), $sql)); while($row = mysql_fetch_array($result)) { extract($row); echo '<option value="'.$id.'">'.$county_name.'</option>'; } echo '</td> </tr> <tr> <td width="50%" class="tablec">Postcode</td> <td width="50%" class="tablec"><input name="postcode" type="text" class="forms" size="24" class="forms" id="postcode" /></td> </tr> <tr> <td width="50%" class="tablec">Disabled Access</td> <td width="50%" class="tablec"> <input name="disabled" type="radio" value="1" checked="checked" />Yes <input name="disabled" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Parking Onsite</td> <td width="50%" class="tablec"> <input name="parking_onsite" type="radio" value="1" checked="checked" />Yes <input name="parking_onsite" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Toilets Onsite</td> <td width="50%" class="tablec"> <input name="toilets_onsite" type="radio" value="1" checked="checked" />Yes <input name="toilets_onsite" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Disabled Toilets</td> <td width="50%" class="tablec"> <input name="disabled_toilets" type="radio" value="1" checked="checked" />Yes <input name="disabled_toilets" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Night Fishing</td> <td width="50%" class="tablec"> <input name="night_fishing" type="radio" value="1" checked="checked" />Yes <input name="night_fishing" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Accomodation</td> <td width="50%" class="tablec"> <input name="accomodation" type="radio" value="1" checked="checked" />Yes <input name="accomodation" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Barbless Hooks Only</td> <td width="50%" class="tablec"> <input name="barbless" type="radio" value="1" checked="checked" />Yes <input name="barbless" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Boilies Allowed</td> <td width="50%" class="tablec"> <input name="boilies" type="radio" value="1" checked="checked" />Yes <input name="boilies" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Groundbait Allowed</td> <td width="50%" class="tablec"> <input name="groundbait" type="radio" value="1" checked="checked" />Yes <input name="groundbait" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Unhooking Mats Compulsary</td> <td width="50%" class="tablec"> <input name="unhooking_mats" type="radio" value="1" checked="checked" />Yes <input name="unhooking_mats" type="radio" value="0" />No </td> </tr> <tr> <td width="50%" class="tablec">Fishery Type:</td> <td width="50%" class="tablec"> <input name="fishery_type" type="radio" value="1" checked="checked" />Coarse <br /> <input name="fishery_type" type="radio" value="0" />Game <br /> <input name="fishery_type" type="radio" value="2" />Game & Coarse </td> </tr> <tr> <td colspan="2" class="tablec"><div align="center"> <input type="Submit" name="Submit" class="forms" value="Start Search" /> </div></td> </tr> </table> </form>'; show_footer(); break; default: // Temp redirect until I get the above working header("Location:new_search.php?p=fisheries"); } ?> But its still doing the same thing on page 2/3/4 etc.... Can anyone help me here as I`d love to get this working Thanks Quote Link to comment https://forums.phpfreaks.com/topic/53226-mysql-search-pagination-problems/#findComment-263004 Share on other sites More sharing options...
sford999 Posted May 28, 2007 Author Share Posted May 28, 2007 Anyone have any ideas please? Quote Link to comment https://forums.phpfreaks.com/topic/53226-mysql-search-pagination-problems/#findComment-263201 Share on other sites More sharing options...
AndyB Posted May 29, 2007 Share Posted May 29, 2007 Remember that if you want to set or retrieve session values then you need to invoke sessions earlier in the script, e.g. <?php session_start(); if(isset($_SESSION['query'])) { $sql = "SELECT * FROM fisheries WHERE ".$_SESSION['query']; } Also, since you are showing only a limited number of results to the page, your actual query on each page should also include a LIMIT clause. Quote Link to comment https://forums.phpfreaks.com/topic/53226-mysql-search-pagination-problems/#findComment-263806 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.