hakmir Posted October 16, 2008 Share Posted October 16, 2008 Hi guys, I have two files "searchrecipe.php" and "reciperesults.php" (with the help of MadTechie and Andy17) Both files are working perfect (almost) (almost): I don't want all the results in one page. I want 10 results (just and example) per page. I've been trying to do and I was so close but I couldn't. Can anybody help me? Thanks in advance. Here are the files searchrecipe.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $dbcnx = @mysql_connect('localhost', 'root', ''); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('recipetime')) { exit('<p>Unable to locate the recipe ' . 'database at this time.</p>'); } ?> <form id="form1" name="form1" method="post" action="reciperesults.php"> <label><strong>SEARCH RECIPE</strong><br /> <br /> </label> <p>Country<br /> <label> <select name="country" id="country"> <option>USA</option> <option>CANADA</option> <option>ENGLAND</option> </select> </label> </p> <p>Type of Food <br /> <label> <select name="typeoffood" id="typeoffood"> <option>DESERT</option> <option>ENTREE</option> <option>SPICY</option> </select> </label> </p> <p> <label> <input type="submit" name="Search" id="Search" value="Search" /> </label> </p> </form> </body> </html> reciperesults.php <?php if (!isset($_POST['Search'])) die("FAILED: nothing selected"); $dbcnx = @mysql_connect('localhost', 'root', ''); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('recipetime')) { exit('<p>Unable to locate the recipe' . 'database at this time.</p>'); } $typeoffood = $_POST['typeoffood']; $country = $_POST['country']; $sql = "SELECT * FROM recipeform WHERE country='$country' AND typeoffood='$typeoffood'"; $result = mysql_query($sql); echo "HERE IS ALL THE $typeoffood RECIPES FROM $country" . "<br>" . "<br>"; while ($rec = mysql_fetch_array($result)) { $recname = htmlspecialchars($rec['name']); $reccountry = htmlspecialchars($rec['country']); $rectypeoffood = htmlspecialchars($rec['typeoffood']); $recyourrecipe = htmlspecialchars($rec['yourrecipe']); echo "Name: <td>$recname</td>" . "<br>"; echo "Country: <td>$reccountry</td>" . "<br>"; echo "Typeoffood: <td>$rectypeoffood</td>" . "<br>"; echo "Your Recipe: <td>$recyourrecipe</td>" . "<br>" . "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/ Share on other sites More sharing options...
Andy-H Posted October 16, 2008 Share Posted October 16, 2008 I see this alot on here, maybe there should be a sticky topic about it xP Anyhow, http://www.phpfreaks.com/tutorial/basic-pagination Thats the tutorial for it... Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667282 Share on other sites More sharing options...
hakmir Posted October 16, 2008 Author Share Posted October 16, 2008 I checked the tutorial. It is working well (almost) When I use this code there is no problem. It shows everything in my database page by page. $sql = "SELECT * FROM recipeform LIMIT $offset, $rowsperpage "; But when I use this code (which I want to use) It shows first page with no problem. But when I click on page 2, 3, or other pages there is no data showing then when I click page 1 again it doesn't show anything either.. $sql = "SELECT * FROM recipeform WHERE typeoffood='$typeoffood' and country='$country' LIMIT $offset, $rowsperpage "; ? This is what I came up with <?php // database connection info $conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('recipetime',$conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM recipeform"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 3; // 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 $typeoffood = $_POST['typeoffood']; $country = $_POST['country']; $sql = "SELECT * FROM recipeform WHERE typeoffood='$typeoffood' and country='$country' LIMIT $offset, $rowsperpage "; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { $recname = htmlspecialchars($list['name']); $reccountry = htmlspecialchars($list['country']); $rectypeoffood = htmlspecialchars($list['typeoffood']); $recyourrecipe = htmlspecialchars($list['yourrecipe']); // echo data echo "Name: <td>$recname</td>" . "<br>"; echo "Country: <td>$reccountry</td>" . "<br>"; echo "Typeoffood: <td>$rectypeoffood</td>" . "<br>"; echo "Your Recipe: <td>$recyourrecipe</td>" . "<br>" . "<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) - 1); $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 ******/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667448 Share on other sites More sharing options...
hakmir Posted October 16, 2008 Author Share Posted October 16, 2008 I saw somebody (biciodark )has the same problem too at http://www.phpfreaks.com/tutorial/basic-pagination#comment-172 can you help? Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667502 Share on other sites More sharing options...
hakmir Posted October 16, 2008 Author Share Posted October 16, 2008 I think When it updates the page (2. or later pages) it doesn't get the POST values (id=typeoffood and id=country) from searchrecipe.php file. But I don't know how to fix. Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667521 Share on other sites More sharing options...
GingerRobot Posted October 16, 2008 Share Posted October 16, 2008 You're correct. The values are only in the POST array on the first request. You will have to pass them around in the URL once they've been set. Don't forget to encode first though. Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667523 Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Yes problem is that the POSTED value is not persisting. You need to make your values session variables or else add them to your links to be passed as GET vars and check for them if there are not posted values. Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667524 Share on other sites More sharing options...
hakmir Posted October 16, 2008 Author Share Posted October 16, 2008 I am new to php guys. I could understand or know how to do what you are saying. I am a copy and paste guy. Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667557 Share on other sites More sharing options...
hakmir Posted October 16, 2008 Author Share Posted October 16, 2008 sorry I wanted to say: I am new to php guys. I WISH I could understand or know how to do what you are saying. I am a copy and paste guy. Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667560 Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Just look at the script. There's already vars in the script that do the same thing... Look how the links are made in the script. Look how they are received in the script. Just do your copy and paste thing and change the name to what you want and there you have it. Don't really know what else we can tell you beyond that, except to learn to be more than a cut and paste guy....or hire someone to do it for you. Quote Link to comment https://forums.phpfreaks.com/topic/128749-x-number-of-results-per-page/#findComment-667587 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.