Jump to content


Photo

I need help with this search engine


  • Please log in to reply
6 replies to this topic

#1 GoodGuy201

GoodGuy201
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 August 2006 - 02:59 AM

Hi ,  This is my first time here and I really was thrilled to see there is help.  I need help with this , i have been stuck on it for the past 3 days.  I am trying to create a search engine which will display all the records that are like the name they enter or  in a given state if the user just selected a state, or all the records of the selected category if a user selected category, etc.  When I select State and Category the code works, but it doesn't work for Name, and it doesnt work when I try to do searches on individual selections. I keep modifying my code but I still cant get it to work properly.  Can someone please point me in the right direction.  I really would appreciate any help I can get. Thank you sooooooo much in advance.

<!---Forum Code above-->
--------------------------------------------
<?php


require_once('./mysql_connect.php'); //connect to the db.


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

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

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

$limit=10;

// Handle the form.

// Create an empty new variable.

if (isset($var)) {
$business_name = TRUE;
}

// Check for a category.
if (isset($var2)) {
$category = TRUE;
}

if (isset($var3)) {
$state = TRUE;
}

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

  }

if ($business_name && $category && $state)
{
$query = "SELECT * FROM Users WHERE (Business_Name LIKE \"%$var%\" AND State =\"$var3\" AND Category =\"$var2\") ORDER BY Business_Name, City";
}
elseif ($business_name && $category)
{
$query = "SELECT * FROM Users WHERE (Business_Name LIKE \"%$var%\" AND Category =\"$var2\") ORDER BY Business_Name, City";
}
elseif ($business_name && $state)
{
$query = "SELECT * FROM Users WHERE (Business_Name LIKE \"%$var%\" AND State =\"$var3\") ORDER BY Business_Name, City";
}
elseif ($category && $state)
{
$query = "SELECT * FROM Users WHERE (Category = \"$var2\" AND State =\"$var3\") ORDER BY Business_Name, City";
}
elseif ($business_name)
{
$query = "SELECT * FROM Users WHERE Business_Name LIKE \"%$var%\" ORDER BY Business_Name, City";
}
elseif ($category)
{
$query = "SELECT * FROM Users WHERE Category = \"$var2\" ORDER BY Business_Name, City";
}
else//if ($state)
{
$query = "SELECT * FROM Users WHERE State = \"$var3\" ORDER BY Business_Name, City";
}


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


if(empty($s))
{
$s=0;
}

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

echo "<p>You searched for:&quot;" .$var . ",&nbsp;". $var2 . ",&nbsp;". $var3 . "&quot;</p>";

echo "Results <br />";
$count = 1 + $s;

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

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

echo "<br />";

if ($s >= 1){
$prevs = ($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&business_name=$var\">&lt;&lt;
Prev 10 </a>&nbsp;&nbsp;";
}
$pages=intval($numrows/$limit);

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

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

echo "&nbsp;<a href = \"$PHP_SELF?s=$news&business_name=$var&category=$var2&state=$var3\">Next 10 &gt;&gt;</a>";
}


$a = $s + ($limit);

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


?>

</body>
</html>

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 03:27 AM

OK well firsat lets get you query string generation a bit more efficient...

<?php

$qry = "SELECT * FROM Users WHERE "; // start of string

$sub = NULL; //initialize substring

if (isset($_GET['business_name']) && strlen($_GET['business_name']) > 0)
{
 $sub = "Business_Name LIKE '%" . $_GET['business_name'] . "%'";
}

if (isset($_GET['category']) && strlen($_GET['category']) > 0)
{
 $sub .= is_null($sub) ? " Category = '" . $_GET['category'] . "'" : " AND Category = '" . $_GET['category'] . "'";
}

if (isset($_GET['state']) && strlen($_GET['state']) > 0)
{
 $sub .= is_null($sub) ? " State ='" . $_GET['state'] . "'" : " AND State ='" . $_GET['state'] . "'";
}

$limit = 10;

$s = empty($s) ? 0 : $s;

$qry .=  $sub . "ORDER BY Business_Name, City LIMIT $s, $limit"; // end of string

?>

Now that may look a little daunting but read it a couple of times and it will make sense....

try that and see how you get on
follow me on twitter @PHPsycho

#3 GoodGuy201

GoodGuy201
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 August 2006 - 03:58 AM

Thank you so much for your help.  Right now, another problem came up which is that only 10 results get displayed no matter what.  I can change the limit on the number of results but I want there to be 10 per page.  For example if there are 16 results that match the search, then 10 of the records get displayed in the first page and 6 in the second page.  Thank you in advance for your help.  :)

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 04:02 AM

read this

http://www.phpfreaks...orials/43/0.php
follow me on twitter @PHPsycho

#5 GoodGuy201

GoodGuy201
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 August 2006 - 04:44 AM

Thanks for the tutorial, I did read all of it.  The other problem is I think is that when the script is executed the second time the values in the form are empty so the query is empty.  Is there a way to save those values so that the next time the scripts runs the parameters of the Query won't be empty?  Thanks again for your help ToonMariner.

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 04:50 AM

yep...

add the variables that were set to the links of your pagination....

so they will say <a href="yourscript.php?s=10&business_name=" . $_GET['business_name'] . "&state=" ..... and so on.
follow me on twitter @PHPsycho

#7 GoodGuy201

GoodGuy201
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 August 2006 - 06:02 AM

This is the code that i finally got after modifying it, but it is still not working.  Something is wrong with this statement

if(!((($s + $limit)/$limit)== $pages) && $pages != 1){ // It's all the way at the bottom, theres a comment.
	$news = $s + $limit;

It's not evaluating to true, so the next link is not being displayed.  I am stuck and don't know where to go from here.  I have received a lot of help from ToonMariner and I appreciate it all.  I dont want to give up, but I've spent few hours just on this one if statement. If anyone can help I will be grateful. Thank you.


<?php


	require_once('./mysql_connect.php'); //connect to the db.


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

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

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

//$limit=10;

// Handle the form.

 /* Create an empty new variable.
	
	if (isset($var)) {
		$business_name = TRUE;
	} 
	
	// Check for a category.
	if (isset($var2)) {
		$category = TRUE;
	}
	
	if (isset($var3)) {
		$state = TRUE;
	} 
	
	if (!isset($var) && !isset($var2) && !isset($var3))
  {
  echo "<p>We dont seem to have a search parameter!</p>";

  }
*/	
  
$query = "SELECT * FROM Users WHERE "; // start of string

$sub = NULL; //initialize substring

if (isset($_GET['business_name']) && strlen($_GET['business_name']) > 0)
{
 $sub = "Business_Name LIKE '%" . $_GET['business_name'] . "%'";
}

if (isset($_GET['category']) && strlen($_GET['category']) > 0)
{
 $sub .= is_null($sub) ? " Category = '" . $_GET['category'] . "'" : " AND Category = '" . $_GET['category'] . "'";
}

if (isset($_GET['state']) && strlen($_GET['state']) > 0)
{
 $sub .= is_null($sub) ? " State ='" . $_GET['state'] . "'" : " AND State ='" . $_GET['state'] . "'";
}

$limit = 10;

$s = empty($s) ? 0 : $s;

$query .=  $sub . "ORDER BY Business_Name, City LIMIT $s, $limit"; // end of string

$numresults = @mysql_query($query);

$numrows=mysql_num_rows($numresults);

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



echo "<p>You searched for:&quot;" .$var . ",&nbsp;". $var2 . ",&nbsp;". $var3 . "&quot;</p>";

echo "Results <br />";
$count = 1 + $s;

while ($row=mysql_fetch_array($result)){
	$title=$row["Business_Name"];
	
	echo "$count &nbsp;$title <br />";
	$count++;
}

$currPage = (($s/$limit)+1);

echo "<br />";

if ($s >= 1){
	$prevs = ($s-$limit);
	print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&business_name={$_GET['business_name']}&category=${$_GET['category']}&state={$_GET['state']}\">&lt;&lt; Prev 10 </a>&nbsp;&nbsp;";
}
$pages=intval($numrows/$limit);

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

if(!((($s + $limit)/$limit)== $pages) && $pages != 1){ //Never Evaluates to true. <<<<------------
	$news = $s + $limit;
	
	echo "&nbsp;<a href = \"$PHP_SELF?s=$news&business_name={$_GET['business_name']}&category={$_GET['category']}&state={$_GET['state']}\">Next 10 &gt;&gt;</a>";
}


$a = $s + ($limit);

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


?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users