Search Form help!

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
templeguy
Posts: 34
Joined: Tue Sep 15, 2009 5:12 pm

Search Form help!

Post by templeguy »

I started with some basic search code.

Whats wrong here?
It say: Notice: Undefined variable: var in C:\wamp\www\work\search\search.php on line 3

index.php

Code: Select all

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

Code: Select all

<?php

  $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","root",""); 


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



 $query = "select * from the_table where 1st_field like \"%$trimmed%\" order by 1st_field";

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

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


 echo "Results";
 $count = 1 + $s ;


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

  echo "$count.) $title" ;
  $count++ ;
  }

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

  $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>";
 
?>
Name: Mads
Age: 17
Contry: Denmark
Skill: Just Started
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: Search Form help!

Post by Falken »

exactly what it says. The variable $var on line 3 in the php file is not defined, it got no value assigned to it, and you are trying to use trim on it.
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
templeguy
Posts: 34
Joined: Tue Sep 15, 2009 5:12 pm

Re: Search Form help!

Post by templeguy »

What can i use insted of trim? Any ideas? (:
Name: Mads
Age: 17
Contry: Denmark
Skill: Just Started
User avatar
Sakar
Posts: 520
Joined: Thu Apr 23, 2009 2:59 am

Re: Search Form help!

Post by Sakar »

The problem is not trim itself, it's that the variable $var is not defined anywhere in the script. You need to setup $var before you can use it.
Post Reply

Return to “Advanced Help and Support”