loztdogs Posted December 1, 2010 Share Posted December 1, 2010 Hi I'm new to the site... and new to PHP, so excuse me if my verbiage isn't quite right. I've been struggling to find a solution to paging through my search results. MY code works for the first five entries that are found via the mysql query but when I click next five results, the code breaks down and sends me back to my index. I believe the problem is here: print " <a href=\"$PHP_SELF?s=$news&poster=$var\">Next 5 >></a>  "; I don't know how to rewrite, to display results based on the number of pages found. Ideally the user should be able to query the db and then page through the results. my entire code: <?php // Get the search variable from URL $var = @$_GET['poster']; $trimmed = trim($var);//trim whitespace from stored variable // rows to return $limit=5; // check for empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; } else { // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database $con = mysql_connect("localhost", "test", "test") or die('Sorry, could not connect to server'); mysql_select_db("recipe", $con) or die('Sorry, could not connect to database'); //Build SQL Query $query = "Select date, poster, title, article FROM bulletin WHERE poster LIKE \"%$trimmed%\" order by articleid desc"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } else // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; echo "<hr>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $date = $row['date']; $poster = $row['poster']; $title = $row['title']; $article = $row['article']; echo "$date"; echo "<br>\n"; echo "<img src='images/marker.png'><font color=\"white\">$poster</font><br><br>\n"; echo "$title"; echo "<br />"; echo "<br />"; echo "<font color=\"white\">$article</font>\n"; echo "<br>\n"; echo "<hr>"; echo "<br>\n"; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 5</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; print " <a href=\"$PHP_SELF?s=$news&poster=$var\">Next 5 >></a>  "; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<br />"; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> If someone could help I would be so great full! Thanks in advance... Mod edit: . . . BBCode tags added. Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/ Share on other sites More sharing options...
loztdogs Posted December 2, 2010 Author Share Posted December 2, 2010 Could someone please help??? Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142245 Share on other sites More sharing options...
Pikachu2000 Posted December 2, 2010 Share Posted December 2, 2010 I haven't gone through all of the code, but first try replacing the line that you think is the problem with this: print " <a href=\"{$_SERVER['PHP_SELF']}?s=$news&poster=$var\">Next 5 >></a>  "; And this line: print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< with this: print " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><< See if that makes a difference. Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142303 Share on other sites More sharing options...
loztdogs Posted December 2, 2010 Author Share Posted December 2, 2010 Thanks Pikachu for replying!!! Unfortunately, when I made the changes, that cause the search results not to display at all. Please let me know if I can provide any more detail... I would love to get this ironed out. Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142313 Share on other sites More sharing options...
Pikachu2000 Posted December 2, 2010 Share Posted December 2, 2010 What do you mean? Are you getting a blank page now? Do you have error reporting on? Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142320 Share on other sites More sharing options...
loztdogs Posted December 2, 2010 Author Share Posted December 2, 2010 Yes, a blank page... No error reporting. I'm testing this live on my host, and they obviously have majority of error reporting turned off. I wont be able to test on my local host until late this evening. Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142322 Share on other sites More sharing options...
Pikachu2000 Posted December 2, 2010 Share Posted December 2, 2010 Sounds like a typo made it's way into the code. When I make the same changes to the code you originally posted, there are no parse errors. You can try adding these couple lines to the top of the script, but of it's a fatal parse error, it won't help. If all else fails, repost the code you have in its current form. ini_set('display_errors', 1); error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142330 Share on other sites More sharing options...
loztdogs Posted December 2, 2010 Author Share Posted December 2, 2010 It didn't display any errors. Here is the code from off my host as you requested. <?php ini_set('display_errors', 1); error_reporting(-1); // Get the search variable from URL $var = @$_GET['poster']; $trimmed = trim($var);//trim whitespace from stored variable // rows to return $limit=5; // check for empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; } else { // check for a search parameter if (!isset($var)) { echo "<p>We don't seem to have a search parameter!</p>"; exit; } //connect to your database $link = mysql_connect('myhost', 'my_db', 'db_pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo ''; mysql_select_db(table); //Build SQL Query $query = "Select date, poster, title, article FROM bulletin WHERE poster LIKE \"%$trimmed%\" order by articleid desc"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } else // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; echo "<hr>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $date = $row['date']; $poster = $row['poster']; $title = $row['title']; $article = $row['article']; echo "$date"; echo "<br>\n"; echo "<img src='images/marker.png'><font color=\"white\">$poster</font><br><br>\n"; echo "$title"; echo "<br />"; echo "<br />"; echo "<font color=\"white\">$article</font>\n"; echo "<br>\n"; echo "<hr>"; echo "<br>\n"; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><< } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; print " <a href=\"{$_SERVER['PHP_SELF']}?s=$news&poster=$var\">Next 5 >></a>  "; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<br />"; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> "; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> <?php // Get the search variable from URL $var = @$_GET['poster']; // $self = $_SERVER['PHP_SELF']; $trimmed = trim($var);//trim whitespace from stored variable // rows to return $limit=5; // check for empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; } else { // check for a search parameter if (!isset($var)) { echo "<p>We don't seem to have a search parameter!</p>"; exit; } //connect to your database $link = mysql_connect('hostname', 'user', 'pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo ''; mysql_select_db(recipe); //Build SQL Query $query = "Select date, poster, title, article FROM bulletin WHERE poster LIKE \"%$trimmed%\" order by articleid desc"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } else // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; echo "<hr>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $date = $row['date']; $poster = $row['poster']; $title = $row['title']; $article = $row['article']; echo "$date"; echo "<br>\n"; echo "<img src='images/marker.png'><font color=\"white\">$poster</font><br><br>\n"; echo "$title"; echo "<br />"; echo "<br />"; echo "<font color=\"white\">$article</font>\n"; echo "<br>\n"; echo "<hr>"; echo "<br>\n"; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><< } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; print " <a href=\"{$_SERVER['PHP_SELF']}?s=$news&poster=$var\">Next 5 >></a>  "; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<br />"; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142336 Share on other sites More sharing options...
Pikachu2000 Posted December 2, 2010 Share Posted December 2, 2010 There were a couple missing quotes and semicolons. Give this a shot. <?php ini_set('display_errors', 1); error_reporting(-1); // Get the search variable from URL $var = @$_GET['poster']; $trimmed = trim($var);//trim whitespace from stored variable // rows to return $limit=5; // check for empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; } else { // check for a search parameter if (!isset($var)) { echo "<p>We don't seem to have a search parameter!</p>"; exit; } //connect to your database $link = mysql_connect('myhost', 'my_db', 'db_pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo ''; mysql_select_db(table); //Build SQL Query $query = "Select date, poster, title, article FROM bulletin WHERE poster LIKE \"%$trimmed%\" order by articleid desc"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } else // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; echo "<hr>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $date = $row['date']; $poster = $row['poster']; $title = $row['title']; $article = $row['article']; echo "$date"; echo "<br>\n"; echo "<img src='images/marker.png'><font color=\"white\">$poster</font><br><br>\n"; echo "$title"; echo "<br />"; echo "<br />"; echo "<font color=\"white\">$article</font>\n"; echo "<br>\n"; echo "<hr>"; echo "<br>\n"; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><<"; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows % $limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; print " <a href=\"{$_SERVER['PHP_SELF']}?s=$news&poster=$var\">Next 5 >></a>  "; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<br />"; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> "; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> <?php // Get the search variable from URL $var = @$_GET['poster']; // $self = $_SERVER['PHP_SELF']; $trimmed = trim($var);//trim whitespace from stored variable // rows to return $limit=5; // check for empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; } else { // check for a search parameter if (!isset($var)) { echo "<p>We don't seem to have a search parameter!</p>"; exit; } //connect to your database $link = mysql_connect('hostname', 'user', 'pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo ''; mysql_select_db(recipe); //Build SQL Query $query = "Select date, poster, title, article FROM bulletin WHERE poster LIKE \"%$trimmed%\" order by articleid desc"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } else // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; echo "<hr>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $date = $row['date']; $poster = $row['poster']; $title = $row['title']; $article = $row['article']; echo "$date"; echo "<br>\n"; echo "<img src='images/marker.png'><font color=\"white\">$poster</font><br><br>\n"; echo "$title"; echo "<br />"; echo "<br />"; echo "<font color=\"white\">$article</font>\n"; echo "<br>\n"; echo "<hr>"; echo "<br>\n"; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><<"; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; print " <a href=\"{$_SERVER['PHP_SELF']}?s=$news&poster=$var\">Next 5 >></a>  "; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<br />"; echo "<br />"; echo "<p>Showing results $b to $a of $numrows</p>"; } ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142347 Share on other sites More sharing options...
loztdogs Posted December 2, 2010 Author Share Posted December 2, 2010 I copied the upper portion of the code, as it looks like it is running together. Were back to the beginning, where the first 5 articles are displayed but when next 5 is clicked it takes me back to my main page and doesnt actually display the next 5. There were a couple of errors... Warning: Unknown: open(/var/php_sessions/sess_57113a36e34db739d0bbc8a8db5ebace, O_RDWR) failed: No such file or directory (2) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0 Notice: Use of undefined constant recipe - assumed 'recipe' in /hermes/web06b/b339/moo.collectiverecipescom/showbulletin.php on line 34 Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142355 Share on other sites More sharing options...
loztdogs Posted December 2, 2010 Author Share Posted December 2, 2010 Ok I think I solved the last couple of errors... My search form was as follows: <form name="form" action="index.php" method="get"> <input type="text" name="poster" /> <input name="goButton" type="submit" value="find" /> <input name="content" type="hidden" value="showbulletin" /> </form> After your help getting me back on track I realized that the form action was calling on index.php, bringing me back to my main. So I changed the form to the following: <form name="form" action="showbulletin.php" method="get"> <input type="text" name="poster" /> <input name="goButton" type="submit" value="find" /> </form> That seems to have fixed the issue as I'm able to page through all of the results. YAY!!!! Lastly is there a way to bring the results back into my main instead of a seperate page???? I think it looks cleaner that way. Quote Link to comment https://forums.phpfreaks.com/topic/220362-stummped-cant-figure-out-how-to-page-through-search-results/#findComment-1142396 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.