Jump to content

Archived

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

GoodGuy201

I need help with this search engine

Recommended Posts

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>

Share this post


Link to post
Share on other sites
OK well firsat lets get you query string generation a bit more efficient...

[code]<?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

?>[/code]

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

Share this post


Link to post
Share on other sites
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.  :)

Share this post


Link to post
Share on other sites
read this

[url=http://www.phpfreaks.com/tutorials/43/0.php]http://www.phpfreaks.com/tutorials/43/0.php[/url]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
This is the code that i finally got after modifying it, but it is still not working.  Something is wrong with this statement

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

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.

[code]

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


?>
[/code]

Share this post


Link to post
Share on other sites

×

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.