svgmx5 Posted October 31, 2009 Share Posted October 31, 2009 Having some trouble trying to get the results to show on more than just one page. What is happening is that when i input a subject or zip code, i get results and links on the bottom to show the number of pages, but when i click on them it gives me a broken link error Here is the code that i have at the top of my html code before the <html> tag begins if(!isset($_GET['resultsPage'])){ $resultsPage = 1; } else { $resultsPage = $_GET['resultsPage']; } // Define the number of results per page $max_results = 9; // Figure out the limit for the query based $from = (($resultsPage * $max_results) - $max_results); that is whats supposed to get the pages, and the number of search results here is t he code i have inside my <body> tag that echoes the results and builds the links for the pages // perform the search $subject = $_POST['subject']; $location = $_POST['location']; $sql = "SELECT * FROM tutorProfiles WHERE MATCH(subject, location) AGAINST('$subject*' '$location*' IN BOOLEAN MODE) LIMIT $from, $max_results"; $result = mysql_query($sql) or die("Problem, with Query:".mysql_error()); // Get number of articles, assign value to a variable $count = mysql_num_rows($result); echo "<h3>Search Results</h3>"; echo "<p class='resultspara'>There is a total of $count results found on our database.</p>"; $TableRows = mysql_num_rows($result); $i = 0; while ($i <$TableRows) { //Add resulting tablerow to relvant variable $tutorId = mysql_result($result, $i, "id"); $first = mysql_result($result, $i, "first"); $school = mysql_result($result, $i, "school"); $degree = mysql_result($result, $i, "degree"); $class = mysql_result($result, $i, "class"); echo" <div id='profileBox'> <div id='badgeImg'> <img src='images/imgBG.png' name='imgBadge' width='76' height='70' align='left' id='imgBadge' /> <h2>$first</h2> <p>$school<br /> $degree - $class </p> </div> <div id='profleBtn'> <a href='profile.php?id=$tutorId'><img src='images/linkBtn.png' alt='view Profile' width='150' height='30' border='0' /></a> </div> </div>"; // loop through all results and indent number on each loop $i ++; } // close while loop //Beggin Pagination // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tutorProfiles"),0); // Figure out the total number of pages. Always round up using ceil() $total_newspage = ceil($total_results / $max_results); echo "<p style=\"clear:both;\" align=\"center\">\n"; // Build Previous Link if($resultsPage > 1) { $prev = ($resultsPage - 1); echo "<a href=\"results.php.php?resultsPage=$prev\">Previous</a>\n "; } for($p = 1; $p <= $total_newspage; $p++) { if(($resultsPage) == $p) { echo "$p\n "; } else { echo "<a href=\"result.php?resultsPage=$i\">$p</a>\n "; } } // Build Next Link if($resultsPage < $total_newspage) { $next = ($resultsPage + 1); echo "<a href=\"results.php?resultsPage=$next\">Next</a>\n"; } echo "</p>\n"; //End pagination Script Anyway if someone could take a look at the code when i would gladly appreciate it Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/ Share on other sites More sharing options...
ArizonaJohn Posted October 31, 2009 Share Posted October 31, 2009 Hi, I had the same problem, and the solution was easy. I needed to add this to the URL of each link: &searching=yes&search=search This is because my search results are generated by an HTML form that has this: <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="search" /> So each URL needed &searching=yes&search=search at the end of it to retrieve results from my database. If you are using a similar HTML form to search for your results, maybe you need to make sure something similar is at the end of each page's URL. -John Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948202 Share on other sites More sharing options...
svgmx5 Posted October 31, 2009 Author Share Posted October 31, 2009 Hey ArizonaJohn THanks for the reply this is my html form that in which you input the info <form action="results.php" method="post"> <div id="inputTxt"> Find a Tutor In<br /> <input name="subject" type="text" id="subject" value="Subject" class="txtBoxes"/> </div> <div id="inputTxt">That lives near <br /> <input name="location" type="text" id="location" value="Location" class="txtBoxes" /> </div> <div id="buttonBg"> <input name="search2" type="submit" id="search2" value="Search" /> </div> </form> I'll add a hidden value and see if that works BTW the search script and form are in two different files Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948204 Share on other sites More sharing options...
ArizonaJohn Posted October 31, 2009 Share Posted October 31, 2009 When you use that form to do a search and it works correctly, do you have this at the end of the URL: &subject=Subject&location=Location&search2=Search ? Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948208 Share on other sites More sharing options...
svgmx5 Posted October 31, 2009 Author Share Posted October 31, 2009 all i get on the url is www.domainname.com/test/results.php results.php been were the script to get the results are in. Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948212 Share on other sites More sharing options...
svgmx5 Posted October 31, 2009 Author Share Posted October 31, 2009 im sorry, so you were saying i should put it in &searching=yes&search=search in the urls that the pagination builds? sorry i'm not really understanding if thats what you were saying Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948215 Share on other sites More sharing options...
ArizonaJohn Posted October 31, 2009 Share Posted October 31, 2009 Try this: $subject = $_GET['subject']; $location = $_GET['location']; and <form action="results.php" method="get"> then make each pagination link like this: echo "<a href=\"results.php.php?resultsPage=$prev&subject=Subject&location=Location&search2=Search\">Previous</a>\n "; Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948216 Share on other sites More sharing options...
ArizonaJohn Posted October 31, 2009 Share Posted October 31, 2009 Maybe try taking off the extra "php" from your pagination links: echo "<a href=\"results.php?resultsPage=$prev&subject=Subject&location=Location&search2=Search\">Previous</a>\n "; That extra php might have been why you were getting the broken link actually. If so, maybe you could leave the form as the "Post" method rather than changing it to the "Get" method as I suggest. Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948217 Share on other sites More sharing options...
svgmx5 Posted October 31, 2009 Author Share Posted October 31, 2009 Okay, so i did that, and while i'm now getting a URL return with the search parameters. I still have the issue in which i get returned 3 pages at the bottom, because i set it to only show 9 results per page, so i have about 20 results in the database with the same parameters. The problem is the url for links 1, 2, 3 show this: test/result.php?resultsPage=9&subject=Subject&location=Location&search2=Search Not only does it say page nine, but it also looses the search parameters. I've been trying to figure this one out or a while. Instead of saying page 2 and page 3 and so on, it says page 9. However the 'next' link doe show the page=2..the only problem is that the results get still lost. Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948220 Share on other sites More sharing options...
svgmx5 Posted October 31, 2009 Author Share Posted October 31, 2009 Okay, so i figured out why the parameters were getting lost when clicking next. I had forgotten to put the variables $subject and $location in the url link. So now when i click next, it shows the results of the next page and so on. However, the number links 1 ,2 , 3 are still not working, they show 'page=9' and also the previous link does not appear when you are on page 2 or 3... Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948227 Share on other sites More sharing options...
svgmx5 Posted October 31, 2009 Author Share Posted October 31, 2009 So i have finally managed to fix it, after looking over a tutorial from here on php freaks, i decided to try that code instead, and walla, it worked!, my only issue still is that even if the results come out to be only 3 it still generates the links as if it was showing everything in the database. Anyway, now i'm getting another error. but i think i'll post it under a new topic instead. I'll leave it open, to see if anyone can figure out why its generating 3 pages even though i search for a query in which only 3 results were retrieved. Here is the new code <?php $subject = $_GET['subject']; $location = $_GET['location']; // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM tutorProfiles"; $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 9; // 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; //retrieve the info from DB $sql = "SELECT * FROM tutorProfiles WHERE WHERE MATCH(subject, location) AGAINST('$subject*' '$location*' IN BOOLEAN MODE) LIMIT $offset, $rowsperpage "; $result = mysql_query($sql) or die("Problem, with Query:".mysql_error()); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo ' <div id="profileBox"> <div id="badgeImg"> <img src="images/imgBG.png" name="imgBadge" width="76" height="70" align="left" id="imgBadge" /> <h2>'.$list['first'].'</h2> <p>'.$list['school'].'<br /> '.$list['degree'] . $list['class'].' </p> </div> <div id="profleBtn"> <a href="profile.php?id='.$list['tutorId'].'"><img src="images/linkBtn.png" alt="view Profile" width="150" height="30" border="0" /></a> </div> </div>'; } // end while /*********** Start the pagination links ********/ echo "<p style=\"clear:both;\" align=\"center\">\n"; // 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&subject=$subject&location=$location&search2=Search'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&subject=$subject&location=$location&search2=Search'><</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&subject=$subject&location=$location&search2=Search'>$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&subject=$subject&location=$location&search2=Search'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&subject=$subject&location=$location&search2=Search'>>></a> "; } // end if echo "</p>\n"; /****** end build pagination links ******/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/179709-help-retrieiving-results-and-doing-pagination/#findComment-948239 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.