Jump to content

[SOLVED] Help with multi-page search


Onloac

Recommended Posts

Alright, its me again. =p I'm trying to put together a very simple search for my site. I've got it working fine, but I'm having trouble displaying the results over multiple pages. I'm using the below code. Right now it displays the first ten results, shows how many results there are, and displays a link to the next page. Only thing is when I click the next page link it nothing happens. Can anyone spot what I'm doing wrong?

 

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  }

// Build SQL Query 
$query = "select * from myarticles where title like \"%$trimmed%\" order by date";

$numresults = $db->query($query);
$numrows = $db->num_rows($numresults);

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";


  }

// 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 = $db->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<br>";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= $db->fetch_array($result)) {
  $title = $row["title"];

  echo "$count.) $title<br>" ;
  $count++ ;
  }

$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 10</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;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";

Link to comment
Share on other sites

Can you also post the HTML output of the page?  Im looking at your $PHP_SELF var and dont think its gonna work in the way you have it set up...  You might try replacing $PHP_SELF with:

 

<a href=\"" . $_SERVER['PHP_SELF'] . "?s=$news&q=var\">Next 10 >></a>

 

Additional Note:  Youre leaving yourself open for a mysql injection attack.  Trimming data is fine and dandy and all but you want to do some error checking on it. 

 

for example also add this:

 

if ( (isset($_GET['q'])) && (is_numeric($_GET['q'])) )

  {

  $var = $_GET['q'];

  }

else

  {

  $var = 10;  //Default

  }

 

 

Which thats fine and dandy for working with NUMBERS but if youre going to deal with text, make DAMN sure you use mysql_real_escape_string($text_var) before you put it in a query.  Do that after you connect to the database tho...

Link to comment
Share on other sites

Yeah, I'm going to secure it a little more once I actually get it working right. I changed the PHP_SELF and I still get the same problem. When I search for something I get the first page of results. When I click the link for the next page it just refreshes the page and shows the first page of results still. The HTML is as followed:

<p>You searched for: "result"</p>
Results<br>
1.) Result one<br>
2.) Result two<br>
3.) Result three<br>
4.) Result four<br>
5.) Result five<br>
6.) Result six<br>
7.) Result seven<br>
8.) Result eight<br>
9.) Result nine<br>
10.) Result ten<br>
<br />
 <a href="/search.php?s=10&q=result">Next 10 >></a>
<p>Showing results 1 to 10 of 38</p>

 

Result # is returned as the article name. I took this from the source and just renamed the results. But this is the results of my search.php file. The results are displayed perfect, I just need to get the multi-page working. =( Any help would be appreciated. by the way, "result" is the term that was searched.

Link to comment
Share on other sites

I search for a term and I'm taken to a page with the first ten results. (?q=mysearchterm in the url)

 

It displays "Showing results 1 - 10 of 30" and right under it has a link for the next ten results.

 

When I click this link the page is reloaded (?s=10&q=mysearchterm in the url now) but it displays the first 10 results I saw on the previous page. Even the "Showing results 1 - 10 of 30" stays the same. I just need some outside eyes on this cause I've been staring at it to much. =(

Link to comment
Share on other sites

Is this all of the code?  I don't see that $s is assigned a value anywhere

// Maybe add this line
$s = $_GET['s'];
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

Your old host may have had regsiter_globals turned on, which IMHO is a bad idea.

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.