Jump to content

SIMPLE PHP SEARCH HELP


mat3000000

Recommended Posts

OK, I am new to PHP

I am wanting to create a property search using drop down boxes.

I have a database with 4 main fields to search: Bedrooms (beds), Area (area), Price Range (price) and Type (type)

Here is the code so far, can someone please correct it. Thanks...

******************************

When I run it it says:

 

Warning: trim() expects at most 2 parameters, 4 given in C:\wamp\www\search222.php on line 8

 

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\search222.php on line 45

 

Couldn't execute query

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

<?php

 

  // Get the search variable from URL

  $var1 = @$_GET['beds'] ;

  $var2 = @$_GET['area'] ;

  $var3 = @$_GET['price'] ;

  $var4 = @$_GET['type'] ;

  $trimmed = trim($var1, $var2, $var3, $var4);

 

// rows to return

$limit=10;

 

// check for an empty string and display a message.

if ($var1 == "")

  {

  echo "<p>Please enter a search...</p>";

  exit;

  }

 

// check for a search parameter

if (!isset($var1))

  {

  echo "<p>We dont seem to have a search parameter!</p>";

  exit;

  }

 

$host = "localhost";

$username = "root";

$password = "";

//connect to your database ** EDIT REQUIRED HERE **

$link = mysql_connect($host, $username, $password) or die("Unable to connect to SQL Server"); //(host, username, password)

 

$db = "property_db";

//specify database ** EDIT REQUIRED HERE **

mysql_select_db($db, $link) or die("Unable to select database"); //select which database we're using

 

// Build SQL Query

 

$results = mysql_query("SELECT * FROM house_src", $link);

$numresults = mysql_num_rows($results);

 

 

 

// get results

  $result = mysql_query($results) or die("Couldn't execute query");

 

// display what the person searched for

echo "<p>You searched for: ". $var4 . "property, with ". $var1 . " bedrooms, in the " . $var2 . "area, with a price range of " . $var3 ."";

 

// begin to show results set

echo "Results";

$count = 1 + $s ;

 

// now you can display the results returned

  while ($row= mysql_fetch_array($result)) {

  $title = $row["1st_field"];

 

  echo "$count.) $title" ;

  $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
https://forums.phpfreaks.com/topic/211323-simple-php-search-help/
Share on other sites

You need to call trim on each $var, not all of them together.  As well, on line 39 you executed a query, then you used the resource to try and execute it again.  THAT is a big no no, and you will get the response you received from PHP parser.

 

Try this:

<?php

  // Get the search variable from URL
  $var1 = trim(@$_GET['beds']);
  $var2 = trim(@$_GET['area']) ;
  $var3 = trim(@$_GET['price']) ;
  $var4 = trim(@$_GET['type']) ;


// rows to return
$limit=10;

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

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

$host = "localhost";
$username = "root";
$password = "";
//connect to your database ** EDIT REQUIRED HERE **
$link = mysql_connect($host, $username, $password) or die("Unable to connect to SQL Server"); //(host, username, password)

$db = "property_db";
//specify database ** EDIT REQUIRED HERE **
mysql_select_db($db, $link) or die("Unable to select database"); //select which database we're using

// Build SQL Query

$results = "SELECT * FROM house_src";

// get results
  $result = mysql_query($results) or die("Couldn't execute query");
$numresults = mysql_num_rows($result);
// display what the person searched for
echo "<p>You searched for: ". $var4 . "property, with ". $var1 . " bedrooms, in the " . $var2 . "area, with a price range of " . $var3 ."";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["1st_field"];

  echo "$count.) $title" ;
  $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>";

?>

OK, now I have that a LOT of new things come up...

Firstly my results are strange. (BTW my fields are: beds(int), area(text), price range(text) and type(text)) Is this right?

Secondly when I run this script I get a lot of errors. I need to know what to put in the 's' variable?...

 

You searched for: Detached property, with 2 bedrooms, in the Calderdale area, with a price range of £50 - £100

 

Results:

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 30

1.) 22.) 1

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 32

 

 

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 34

 

Notice: Undefined variable: numrows in C:\wamp\www\search222.php on line 37

 

Notice: Undefined variable: numrows in C:\wamp\www\search222.php on line 39

 

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 43

 

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 45

 

Notice: Undefined variable: PHP_SELF in C:\wamp\www\search222.php on line 45

 

Notice: Undefined variable: var in C:\wamp\www\search222.php on line 45

Next 10 >>

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 45

 

Notice: Undefined variable: numrows in C:\wamp\www\search222.php on line 45

 

Notice: Undefined variable: numrows in C:\wamp\www\search222.php on line 45

 

Notice: Undefined variable: s in C:\wamp\www\search222.php on line 45

 

Notice: Undefined variable: numrows in C:\wamp\www\search222.php on line 45

 

 

Showing results 1 to of

 

 

 

Would be greatful for any help, cheers!

 

PS: Thanks for your previous answer it did sort a lot of the problems out! :)

 

<?php  // Get the search variable from URL  
$var1 = trim(@$_GET['beds']);  
$var2 = trim(@$_GET['area']) ;  
$var3 = trim(@$_GET['price']) ;  
$var4 = trim(@$_GET['type']) ; 

// rows to return
$limit=10;
// check for an empty string and display a message.
if ($var1 == "")  {  echo "<p>Please enter a search...</p>";  exit;  }
// check for a search parameter
if (!isset($var1))  {  echo "<p>We dont seem to have a search parameter!</p>";  exit;  }

$host = "localhost";
$username = "root";
$password = "";
$link = mysql_connect($host, $username, $password) or die("Unable to connect to SQL Server"); 

mysql_select_db("property_db", $link) or die("Unable to select database"); 
//select which database we're using// Build SQL Query
$results = "SELECT * FROM house_src";
// get results  
$result = mysql_query($results) or die("Couldn't execute query");
$numresults = mysql_num_rows($result);

// display what the person searched for
echo "<p>You searched for: ". $var4 . " property, with ". $var1 . " bedrooms, in the " . $var2 . " area, with a price range of " . $var3 ."";

// begin to show results set
echo "<br><br>Results:";$count = 1 + $s ;
// now you can display the results returned  
while ($row= mysql_fetch_array($result)) {  $title = $row["beds"];  echo "$count.) $title" ;  $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>"; ?>

 

1. $s is not defined, it is used on line 30 for addition purposes, but nowhere is it's initial value set.

2. I'm thinking that $numrows is suppose to point back to Line 24, but that is set to $numresults.  Change this name to $numrows as that variable is used more than once.

3.$PHP_SELF is only if Globals are on, which in later releases of PHP they are not.  Change to $_SERVER['PHP_SELF'].

4. There is no $var in your script, they are $var1,$var2,$var3,$var4.

 

 

Correct those 4 things, should get you well on your way.

If you want an example that works there is a tutorial called Simple SQL Search that will be a good read.

 

But most of your errors seemed like you learned PHP from a bad source (surpressing $_GET errors and register_globals assuming to be on). I would highly suggest reading through that tutorial above.

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.