Jump to content

[SOLVED] PHP Search engine troubles


alianated

Recommended Posts

Hello Everyone,

 

Firstly great place here alot of usefull things linked and posted great job.

 

Secondly i have a small simple script that searches through my Database and returns the results of usernames that are searched, this all works perfectly my problem is with making thepage link to a new page after x amount of results are shown on page 1, i have what i assumed would have worked but im apparantly wrong, it shows that i have more than x amount of results, and then gives me the link to the next page but i cannot click to view the next page, Any help or thought's on this is greatly appretiated.

 

~Code~

 





<form name="form" action="search.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>



<?php

  $var = @$_GET['q'] ;
  $trimmed = trim($var); 

  $limit=10; 

  if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

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

  mysql_connect("localhost","******","******");

  mysql_select_db("login") or die("Unable to select database"); 

  $query = "select * from users where username like \"%$trimmed%\" 
  order by username"; 

  $numresults=mysql_query($query);
  $numrows=mysql_num_rows($numresults);

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

  echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }
  
  if (empty($s)) {
  $s=0;
  }

  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

  echo "<p>You searched for: "" . $var . ""</p>";

  $count = 1 + $s ;

  while ($row= mysql_fetch_array($result)) {
  $title = $row["username"];

  echo "<br> $title" ;
  $count++ ;
  }
  echo "<br>" ;
  $currPage = (($s/$limit) + 1);

  echo "<br />";

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

  $pages=intval($numrows/$limit);

  if ($numrows%$limit) {
  $pages++;
  }

  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

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






 

 

Many Thanks Dave.

Link to comment
https://forums.phpfreaks.com/topic/141809-solved-php-search-engine-troubles/
Share on other sites

You pass the variable 's' through the address bar, but you never GET it on the new page like you do with the variable 'q'.

 

I'd also recommend using better variable names, it helps others know what a variable is for without looking at when it is initialized.

You have this :

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

 

Which pass the variables 's' and 'q' to the new page.

On the page you GET the variable 'q' using this statement:

$var = @$_GET['q'] ;

 

But you don't have a similar statement for $s/$_GET['s'].

To be quite honest im not sure how i would need to adjust my code then to make it actually show the next set of results forgive me it's been a long day.. is there any chance you could maybe show me a example, as i really thought i had everything coverd.

 

thanks again

Sorry for the lengthy time to respond, a bit busy on my end.

 

If really have the process down from what I can see, you're just missing a component.

When you put a variable in the address bar, for instance you have a link that goes to:

page.php?var=blah

 

When it goes to that page, you don't automatically have the variable $var, it's $_GET['var'].

You understood this with:

$var = @$_GET['q'];

But then the variable $s is passed up, and you don't get it before you check if it's empty, you need:

$s = $_GET['s'];

Hello Everyone,

 

Firstly great place here alot of usefull things linked and posted great job.

 

Secondly i have a small simple script that searches through my Database and returns the results of usernames that are searched, this all works perfectly my problem is with making thepage link to a new page after x amount of results are shown on page 1, i have what i assumed would have worked but im apparantly wrong, it shows that i have more than x amount of results, and then gives me the link to the next page but i cannot click to view the next page, Any help or thought's on this is greatly appretiated.

Many Thanks Dave.

 

What you're looking to do is called pagination.

 

I knew nothing about it and this tutorial helped me learn about it and create it.  Its a fantastic tutorial.

http://www.phpfreaks.com/tutorial/basic-pagination

Zhadus, Thank you for taking the time to point out my mistake, that is actually quite obvious now i come to look at it, i guess i just needed a break and a new set of eyes to help point out what i'd missed, its now working flawlessly thank you so much your time is appretiated,

 

Dave

No problem, sometimes all it takes is another set of eyes. If you have any more questions, feel free to post here, also welcome to contact me directly through AIM or MSN etc.

 

A quick pointer: Use some "debugging" skills. You noticed it wasn't changing the results, therefore it isn't understanding something or it isn't getting all the information. What I normally do then, are "echo" out variables after if statements to see if they passed and if the variables look as they should. Hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.