spiceweasel Posted November 24, 2010 Share Posted November 24, 2010 This is my first php script and im a bit confused. I have written a search form (search.html) sent via POST which is processed by results.php. This works and calls back the correct data from the database which i have paginated. The problem is that results are only showed on the first page although pagination shows there are more pages of results to be viewed. To try and figure out what is going on i echoed back the search term which shows correctly but then disappears on the subsequent pages as if it 'forgets' what the user searched for -is this why no results are returned on any page other than page one?. What am i doing wrong?? Its probably a simple fix but i just got done reading a php book and this is my first go at anything real world and useful. Any suggestions would be greatly appreciated. <link href="css/main.css" rel="stylesheet" type="text/css" /> <div id="container"> <?php include('includes\connection.php'); //connect $db_connect = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if (!$db_connect){ echo "Could not connect to database ".mysql_error(); } //select $db_select = mysql_select_db(DB_NAME,$db_connect); if (!$db_connect){ echo "Could not select database ".mysql_error(); } $search = $_POST['area']; /****** pagination start ******/ // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM hotels WHERE location = '$search'"; $result = mysql_query($sql, $db_connect) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 5; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db //query echo 'You searched '.$search; $query = mysql_query("SELECT * FROM hotels WHERE location = '$search' LIMIT $offset, $rowsperpage"); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($query)) { // echo data echo '<div id="listing">'; echo '<br><strong>'.$list['name'].'</strong'; echo '<br>Location: '.$list['location']; echo '<br>Price From: £'.$list['price']; echo '<br><img src="'.$list['awards'].'"/><br>'; echo '<img src="'.$list['image'].'"/><br>'; echo '<br>'.$list['description'].'<br>'; } // end while /****** build the pagination links ******/ // range of num links to show $range = 3; // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?> </div> Quote Link to comment Share on other sites More sharing options...
jim_keller Posted November 24, 2010 Share Posted November 24, 2010 The problem is that $_POST['area'] is only going to be set on the first postback, meaning only on the first page. Try replacing $search = $_POST['area'] with the following: // Start a session if we don't have one if ( !isset($_SESSION) ) { session_start(); } if ( isset($_POST['area']) && $_POST['area'] ) { // This is the first page, so our search will come from $_POST // We use mysql_real_escape_string to make sure the string is safe to pass to mysql $search = mysql_real_escape_string($_POST['area']); // Save the search data in the session for subsequent pages $_SESSION['search'] = $_POST['area']; } else { // We didn't find $_POST['area'], so we must not be on the first page. // So pull the search data from the session if ( isset($_SESSION['search']) ) { $search = mysql_real_escape_string($_SESSION['search']); } else { die( 'No search query found. Please try again'); } } // put your search code here Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 24, 2010 Author Share Posted November 24, 2010 Hi jim_keller, Thanks alot that seems to do the trick! 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.