Jump to content

Stummped, can't figure out how to page through search results...


loztdogs

Recommended Posts

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>&nbsp ";

 

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>&nbsp ";
  }

// 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>&nbsp ";
  }

$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.

Link to comment
Share on other sites

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>&nbsp ";

 

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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>&nbsp ";
  }

$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>&nbsp ";
  }

$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>";
  }
  
?>




Link to comment
Share on other sites

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>&nbsp ";
  }

$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>&nbsp ";
  }

$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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!!!  :D

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.