Smee Posted April 30, 2010 Share Posted April 30, 2010 Hi, I have a working search that i need a tutorial or some help where the PHP shows a next page link if more results are given than a certain amount specified. If anyone can give me some advice or decent tutorial i'd be very grateful. Thanks <?php // This script allows us to search our database require_once ('./includes/config.inc.php'); $page_title = 'Search'; include_once ('./includes/header.html'); ?> <div id="search"> <h1> Search </h1> <form action="search.php?go" method="post" /> <p class="post_code"><label> Post Code: </label> <input type="text" name="post_code" maxlength="40" /> <p class="team_supported"><label> Team Supported: </label> <input type="text" name="team_supported" maxlength="40" /> <p class="submit"> <input type="submit" name="submit" value="Search" /> </form> </div> <div id="searchconfirmation"> <?php $error = false; if (isset($_POST['submit'])) { if(isset($_GET['go'])){ require_once ('../mysql_connect.php'); $error = false; if (preg_match ('/^[[:alnum:]]{4,20}$/i', stripslashes(trim($_POST['post_code'])))) { $pc = $_POST['post_code']; } else { echo '<center><p><font color ="red">Please enter the first 4 letters of your Post Code!</font></p></center>'; $error = true; } $ts = $_POST['team_supported']; if (!$error) { //-query the database table $query = "SELECT first_name, last_name, email, post_code, team_supported FROM users WHERE post_code LIKE '%" . $pc . "%' AND team_supported LIKE '%" . $ts . "%'"; //-run the query against the mysql query function $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); ?> <table width="600" height="100" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Last Name</th> <th scope="col">E Mail</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> <th scope="col">Attending</th> </tr> </thead> <tfoot> <tr> <th scope="row">Total</th> <td colspan="4">85 designs</td> </tr> </tfoot> <?php //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $fn = $row['first_name']; $ln = $row['last_name']; $em = $row['email']; $pc = $row['post_code']; $ts = $row['team_supported']; ?> <tbody> <tr> <td><?php echo "$fn"; ?></td> <td><?php echo "$ln"; ?></td> <td><?php echo "<a href=mailto:" . $em . ">" . $em . ""; ?></td> <td><?php echo "$pc"; ?></td> <td><?php echo "$ts"; ?></td> <td></td> </tr> </tbody> <?php } } else { echo '<center><p><font color="red">Please enter a search query</font></p></center>'; } } } ?> </div> <?php include ('./includes/footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/ Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 You want pagination. Use the forum search feature for pagination. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051312 Share on other sites More sharing options...
Smee Posted May 1, 2010 Author Share Posted May 1, 2010 Thanks for the reply! I found a good tutorial in the PHP Freaks tut section and followed in but cant see why im getting the error: Parse error: syntax error, unexpected T_IF in search.php on line 72 IF statement on line 72 is: if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } The rest of the code now looks like this.. Any suggestions? Thanks <?php // This script allows us to search our database require_once ('./includes/config.inc.php'); $page_title = 'Search'; include_once ('./includes/header.html'); ?> <div id="search"> <h1> Search </h1> <form action="search.php?go" method="post" /> <p class="post_code"><label> Post Code: </label> <input type="text" name="post_code" maxlength="40" /> <p class="team_supported"><label> Team Supported: </label> <input type="text" name="team_supported" maxlength="40" /> <p class="submit"> <input type="submit" name="submit" value="Search" /> </form> </div> <div id="searchconfirmation"> <?php $error = false; if (isset($_POST['submit'])) { if(isset($_GET['go'])){ require_once ('../mysql_connect.php'); $error = false; if (preg_match ('/^[[:alnum:]]{4,20}$/i', stripslashes(trim($_POST['post_code'])))) { $pc = $_POST['post_code']; } else { echo '<center><p><font color ="red">Please enter the first 4 letters of your Post Code!</font></p></center>'; $error = true; } $ts = $_POST['team_supported']; if (!$error) { $query = "SELECT COUNT(*) FROM users WHERE post_code LIKE '%" . $pc . "%' AND team_supported LIKE '%" . $ts . "%'"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; $rowsperpage = 5; $totalpages = ceil($numrows / $rowsperpage) if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $query = "SELECT SELECT first_name, last_name, email, post_code, team_supported FROM users LIMIT $offset, $rowsperpage"; $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); ?> <table width="600" height="100" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Last Name</th> <th scope="col">E Mail</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> <th scope="col">Attending</th> </tr> </thead> <tfoot> <tr> <th scope="row">Total</th> <td colspan="4">85 designs</td> </tr> </tfoot> <?php //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $fn = $row['first_name']; $ln = $row['last_name']; $em = $row['email']; $pc = $row['post_code']; $ts = $row['team_supported']; ?> <tbody> <tr> <td><?php echo "$fn"; ?></td> <td><?php echo "$ln"; ?></td> <td><?php echo "<a href=mailto:" . $em . ">" . $em . ""; ?></td> <td><?php echo "$pc"; ?></td> <td><?php echo "$ts"; ?></td> <td></td> </tr> </tbody> <?php $range = 3; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } } } else { echo '<center><p><font color="red">Please enter a search query</font></p></center>'; } } } ?> </div> <?php include ('./includes/footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051336 Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 Common syntax errors 'unexpected ...' are simple, for exampe, in this case you missed an end-line operator: $totalpages = ceil($numrows / $rowsperpage) should be: $totalpages = ceil($numrows / $rowsperpage); -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051337 Share on other sites More sharing options...
mikesta707 Posted May 1, 2010 Share Posted May 1, 2010 missing a semi colon on this line $totalpages = ceil($numrows / $rowsperpage) should be $totalpages = ceil($numrows / $rowsperpage); just a tip, when you have parse errors and you just cant see whats wrong with the line it specifies, look to the line(s) above Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051340 Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 Mikesta do you make a habit of copying responses? Just wondering since i see no reason why you would do it other than to steal credit? strange.. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051343 Share on other sites More sharing options...
Smee Posted May 1, 2010 Author Share Posted May 1, 2010 Awesome it worked a treat! made a similar mistake further down as well! Well it works but i understand that because no results show on my second page they are not being passed over. Do i need to pass them through a URL or something? Sorry new to this Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051347 Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 Personally, to save mysql data calls, i would store the initial result of the search query in a session variable array. Then you dont have to worry about passing or searching the db for each page. First, when the the form is submitted, do a while loop like so: $result = mysql_query(searchquery...); $_SESSION['search_result'] = array(); // this is so if they search again the results overwrite instead of get added. while($row = mysql_fetch_array($result)){ $_SESSION['search_result'][] = $row; } Then on the page you would do pagination normally, except where you grab the rows from, do something like: $rowsperpage = 5; $currentpage = 3; // just to show you an example $totalrows = count($_SESSION['search_result']; // say for example 20 rows (index starts at 0). $offset = ($currentpage - 1) * $rowsperpage; // would equal: (3 - 1) * 5 = 10; for($i=$offset;$i<($offset+$rowsperpage);$i++){ echo('Search result: '.$_SESSION['search_result'][$i]['name'].'<br />'); } this is a small example. Will save you effort/time and money in the long run. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051350 Share on other sites More sharing options...
mikesta707 Posted May 1, 2010 Share Posted May 1, 2010 sorry, didn't even realize you posted before I did. that reply certainly wasn't there before I posted. Must have missed the "replys were made while you were posting" error while posting. No need to be so touchy, an honest mistake of course, not to mention double posts are common on most help forums Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051351 Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 Not touchy, thought more time had passed since my answer and yours, thought you would of had to read my question before replying o in my eyes, you already knew i posted. anyway; Having been pm'd this: Hi mate, Hope you dont mind me PM'ing you but im reallyt struggerling with this and its driving me round the bend You seem to really know what your talking about where as i really don't! Thanks for your latest reply but its really not making a lot of sense to me. $query = "SELECT first_name, last_name, email, post_code, team_supported FROM users LIMIT $offset, $rowsperpage"; $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); $_SESSION['search_result'] = array(); // this is so if they search again the results overwrite instead of get added. while($row = mysql_fetch_array($result)){ $_SESSION['search_result'][] = $row; } This is what i have added and have tried adding something to the URL link like: echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&find={$_SESSION['search_result']}'></a> "; i know you mentioned putting it into a session to make it easier in the long run but with my lack of knowledge i was wondering if you could give me an example of how my code could work in the simplist way possible? The website im making is nothing special an only need the basics doing. If you dont have tim or simply can't be bothered then i totally understand! Anyway here is the script i have: <?php // This script allows us to search our database require_once ('./includes/config.inc.php'); $page_title = 'Search'; include_once ('./includes/header.html'); ?> <div id="search"> <h1> Search </h1> <form action="search.php?go" method="post" /> <p class="post_code"><label> Post Code: </label> <input type="text" name="post_code" maxlength="40" /> <p class="team_supported"><label> Team Supported: </label> <input type="text" name="team_supported" maxlength="40" /> <p class="submit"> <input type="submit" name="submit" value="Search" /> </form> </div> <div id="searchconfirmation"> <?php $error = false; if (isset($_POST['submit'])) { if(isset($_GET['go'])){ require_once ('../mysql_connect.php'); $error = false; if (preg_match ('/^[[:alnum:]]{4,20}$/i', stripslashes(trim($_POST['post_code'])))) { $pc = $_POST['post_code']; } else { echo '<center><p><font color ="red">Please enter the first 4 letters of your Post Code!</font></p></center>'; $error = true; } $ts = $_POST['team_supported']; if (!$error) { $query = "SELECT COUNT(*) FROM users WHERE post_code LIKE '%" . $pc . "%' AND team_supported LIKE '%" . $ts . "%'"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 5; $totalpages = ceil($numrows / $rowsperpage); if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $query = "SELECT first_name, last_name, email, post_code, team_supported FROM users LIMIT $offset, $rowsperpage"; $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); $_SESSION['search_result'] = array(); // this is so if they search again the results overwrite instead of get added. while($row = mysql_fetch_array($result)){ $_SESSION['search_result'][] = $row; } ?> <table width="600" height="100" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Last Name</th> <th scope="col">E Mail</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> <th scope="col">Attending</th> </tr> </thead> <tfoot> <tr> <th scope="row">Total</th> <td colspan="4">85 designs</td> </tr> </tfoot> <?php while($row=mysql_fetch_array($result)){ $fn = $row['first_name']; $ln = $row['last_name']; $em = $row['email']; $pc = $row['post_code']; $ts = $row['team_supported']; ?> <tbody> <tr> <td><?php echo "$fn"; ?></td> <td><?php echo "$ln"; ?></td> <td><?php echo "<a href=mailto:" . $em . ">" . $em . ""; ?></td> <td><?php echo "$pc"; ?></td> <td><?php echo "$ts"; ?></td> <td></td> </tr> </tbody> <?php $range = 3; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&find={$_SESSION['search_result']}'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&find={$_SESSION['search_result']}'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&find={$_SESSION['search_result']}'></a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&find={$_SESSION['search_result']}'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&find={$_SESSION['search_result']}'>>></a> "; } } } else { echo '<center><p><font color="red">Please enter a search query</font></p></center>'; } } } ?> </div> <?php include ('./includes/footer.html'); ?> Cheers for everything Smee I will reply with this, here: <?php session_start(); // Tell php we're using sessions. // This script allows us to search our database require_once ('./includes/config.inc.php'); $page_title = 'Search'; include_once ('./includes/header.html'); ?> <div id="search"> <h1> Search </h1> <form action="search.php?go" method="post" /> <p class="post_code"><label> Post Code: </label> <input type="text" name="post_code" maxlength="40" /> <p class="team_supported"><label> Team Supported: </label> <input type="text" name="team_supported" maxlength="40" /> <p class="submit"> <input type="submit" name="submit" value="Search" /> </form> </div> <div id="searchconfirmation"> <?php if(isset($_POST['submit'])){ // Search form // Do the search query here first: require_once ('../mysql_connect.php'); // Santize Post code and team supported. (Santitize both, never use trim or other similar functions when santizing input.) $error = null; if ( preg_match ('/^[[:alnum:]]{4,20}$/i', $_POST['post_code']) && preg_match ('/^[[:alnum:] ]{4,30}$/i', $_POST['team_supported']) ) { // Select every column from the table where conditions match: $query = "SELECT * FROM users WHERE post_code LIKE '%" . $_POST['post_code'] . "%' AND team_supported LIKE '%" . $_POST['team_supported'] . "%'"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: ".mysql_error()); // Do the while loop to add each row to the SESSION array: $_SESSION['search_result'] = array(); while($row = mysql_fetch_array($result)){ $_SESSION['search_result'][] = $row; } } else { // Save error as string, so we can use it how we wish later. $error = '<center><p><font color ="red">Post code or Team Supported contains either; one or more invalid characters, or too many/too little characters.</font></p></center>'; } } if(!isset($_SESSION['search_result']) || $error != null){ // No Search or errors echo(($error != null)? $error : "Please submit a Search query"); }else{ // Display the search results. $rowsperpage = 5; $totalrows = count($_SESSION['search_result']); // Count number of array items in the session variable array $totalpages = ceil($totalrows / $rowsperpage); // Get total number of pages in result $thispage = (isset($_GET['page']) && is_numeric($_GET['page']))? $_GET['page'] : 0; // Set the page number (minimum 0) $thispage = ($thispage > $totalpages)? $totalpages : $thispage; // Make sure the page number isnt over the totalpages. $offset = ($thispage <= 0)? 0 : (($thispage - 1) * $rowsperpage); // Get the row offset (starting row) echo(' <table width="600" height="100" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Last Name</th> <th scope="col">E Mail</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> <th scope="col">Attending</th> </tr> </thead>'); // End echoing header. // Loop through each result and echo it. for($i=$offset;$i<$totalrows;$i++){ echo(' <tbody> <tr> <td>'.$_SESSION['search_result'][$i]['first_name'].'</td> <td>'.$_SESSION['search_result'][$i]['last_name'].'</td> <td><a href="mailto:'.$_SESSION['search_result'][$i]['email'].'">'.$_SESSION['search_result'][$i]['email'].'</a></td> <td>'.$_SESSION['search_result'][$i]['post_code'].'</td> <td>'.$_SESSION['search_result'][$i]['team_supported'].'</td> <td></td> </tr> </tbody>'); // End echoing result. } echo(' <tfoot> <tr> <th scope="row">Total</th> <td colspan="4">85 designs</td> </tr> </tfoot></table>'); // End echoing footer. } ?> </div> <?php // Previosu and Next links echo( '<a href="'.$_SERVER['PHP_SELF'].'?page='.((($thispage-1) <= 0)? 0 : $thispage-1).'">Previous Page</a> - <a href="'.$_SERVER['PHP_SELF'].'?page='.((($thispage+1) <= 0)? 0 : $thispage+1).'">Next Page</a>' ); // Footer include ('./includes/footer.html'); ?> Take note of the comments, you had a lot of code you didnt need and some of your logic was superfluous. Try to get a more streamlined understanding of how php works, sessions can be simple once you have used them a few times. Really read through the code and understand it, anything you dont understand feel free to ask. -cb- EDIT: Modified the error handling of incorrect characters, most simplest form, you can make it more accurate quite easily. Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051376 Share on other sites More sharing options...
Smee Posted May 1, 2010 Author Share Posted May 1, 2010 Your an absolute legend, i cant thank you enough! Im going to go over it tomorrow as its very late. All i will say is that it comes up with an error of: an error occured in the script '/home/footba33/public_html/search.php' on 78 Undefined variable: error if(!isset($_SESSION['search_result']) || $error != null){ // No Search or errors echo(($error != null)? $error : "Please submit a Search query"); } Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051394 Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 oops, nothing serious - to fix that error just move $error = null; above the isset condition: if(isset($_POST['submit'])){ // Search form // Do the search query here first: require_once ('../mysql_connect.php'); // Santize Post code and team supported. (Santitize both, never use trim or other similar functions when santizing input.) $error = null; should be: $error = null; if(isset($_POST['submit'])){ // Search form // Do the search query here first: require_once ('../mysql_connect.php'); // Santize Post code and team supported. (Santitize both, never use trim or other similar functions when santizing input.) -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051397 Share on other sites More sharing options...
Smee Posted May 1, 2010 Author Share Posted May 1, 2010 Another one bites the dust Well it all works! Just when you click search with no content: an error occured in the script '/home/footba33/public_html/search.php' on 143: Undefined variable: thispage which is the two at the bottom of the script. echo( '<a href="'.$_SERVER['PHP_SELF'].'?page='.((($thispage-1) <= 0)? 0 : $thispage-1).'">Previous Page</a> - <a href="'.$_SERVER['PHP_SELF'].'?page='.((($thispage+1) <= 0)? 0 : $thispage+1).'">Next Page</a>' ); Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051400 Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 I'm glad its working! Undefined variable just means that the variable doesnt exist. To get rid of them, jsut do a if condition - if there is no results, dot display the links. eg; if(isset($thispage)){ // show links } -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200328-search-next-page/#findComment-1051403 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.