Jump to content

Archived

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

valleydr

PHP search mySQL

Recommended Posts

Hello. I am somewhat familiar with php, I am learning more as I go. I have come up with an issue I can't figure out. I am setting up a script that uses a search form that searches a mysql db for the keyword and show the results. I have it set up to search a specific field in the db, but I want it to search more than one. what I have so far is:

[code]
if(!isset($HTTP_GET_VARS["keyword"]))
    $keyword = "";
else
    $keyword = $HTTP_GET_VARS["keyword"];

$filterKeyword = "AND (c.features LIKE '%$keyword%' OR '$keyword'='')";
[/code]

keyword is the text box from the form, and c.features is the field it is currently searching from the c table. This works, but what I need is for it to basically search c.features and c.engine and c.drive, etc. For whatever reason, i cannot figure out the correct formatting for this. I'd appreciate any help I can get. Thanks.

Share this post


Link to post
Share on other sites
[!--quoteo(post=377774:date=May 28 2006, 03:36 AM:name=valleydr)--][div class=\'quotetop\']QUOTE(valleydr @ May 28 2006, 03:36 AM) [snapback]377774[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello. I am somewhat familiar with php, I am learning more as I go. I have come up with an issue I can't figure out. I am setting up a script that uses a search form that searches a mysql db for the keyword and show the results. I have it set up to search a specific field in the db, but I want it to search more than one. what I have so far is:

[code]
if(!isset($HTTP_GET_VARS["keyword"]))
    $keyword = "";
else
    $keyword = $HTTP_GET_VARS["keyword"];

$filterKeyword = "AND (c.features LIKE '%$keyword%' OR '$keyword'='')";
[/code]

keyword is the text box from the form, and c.features is the field it is currently searching from the c table. This works, but what I need is for it to basically search c.features and c.engine and c.drive, etc. For whatever reason, i cannot figure out the correct formatting for this. I'd appreciate any help I can get. Thanks.
[/quote]




If you are using MySQL then try use the array mysql_fetch_assoc() function.

You can use it to search several columns, tables etc and then store that in an array


Share this post


Link to post
Share on other sites
I am using mySQL. There isn't any way to just somehow do smething like:

[code]
AND (c.features, c.engine, c.drive LIKE '%$keyword%' OR '$keyword'='')
[/code]

or something like that? (I realize the above is incorrect.) This would just be much easier for to update the pages. Thanks for the input.

Share this post


Link to post
Share on other sites
[code]AND ((c.features LIKE '%$keyword%')
  OR  (c.engine LIKE '%$keyword%')
  OR (c.drive LIKE '%$keyword%' ))[/code]

Share this post


Link to post
Share on other sites
Hello Barand,

I tried that, but it stopped my other search functions from working properly. Below is my full search code I have. You can see what search info I am gathering, and how I am searching the db. I tried what you mention, and it worked great for the keywords field, but none of my other search fields worked properly.

[code]

if(!isset($HTTP_GET_VARS["model"]))
    $model = "";
else
    $model = $HTTP_GET_VARS["model"];
    
if(!isset($HTTP_GET_VARS["min_price"]))
    $min_price = "";
else
    $min_price = $HTTP_GET_VARS["min_price"];
    
if(!isset($HTTP_GET_VARS["max_price"]))
    $max_price = "";
else
    $max_price = $HTTP_GET_VARS["max_price"];
    
if(!isset($HTTP_GET_VARS["min_year"]))
    $min_year = "";
else
    $min_year = $HTTP_GET_VARS["min_year"];
    
if(!isset($HTTP_GET_VARS["max_year"]))
    $max_year = "";
else
    $max_year = $HTTP_GET_VARS["max_year"];


if(!isset($HTTP_GET_VARS["type"]))
    $type = "";
else
    $type = $HTTP_GET_VARS["type"];

$filterType = "AND (c.type LIKE '%$type%' OR '$type'='')";

if(!isset($HTTP_GET_VARS["keyword"]))
    $keyword = "";
else
    $keyword = $HTTP_GET_VARS["keyword"];
    
if(!isset($HTTP_GET_VARS["engine"]))
    $engine = "";
else
    $engine = $HTTP_GET_VARS["engine"];
    
if(!isset($HTTP_GET_VARS["drive"]))
    $drive = "";
else
    $drive = $HTTP_GET_VARS["drive"];

$filterKeyword = "AND (c.features LIKE '%$keyword%' OR '$keyword'='')
                  AND (c.drive LIKE '%$drive%' OR '$drive'='')
                  AND (c.engine LIKE '%$engine%' OR '$engine'='')
                  ";

if(!isset($HTTP_GET_VARS["year"]))
    $year = "";
else
    $year = $HTTP_GET_VARS["year"];

$sqlCnt = "SELECT COUNT(c.id)
           FROM ".$db_prefix."cars AS c
           INNER JOIN ".$db_prefix."dealers AS d ON d.id=c.member_id
           WHERE  
           d.status='Active' AND
           (c.model LIKE '$model%' OR '$model'='')
           AND (c.price >= '$min_price' OR '$min_price'='')
           AND (c.price <= '$max_price' OR '$max_price'='')
           ".$filterType."
           ".$filterKeyword."
           AND (c.year >= '$min_year' OR '$min_year'='')
           AND (c.year <= '$max_year' OR '$max_year'='')
           AND (c.member_id='$dealer_id' OR '$dealer_id'='')";

[/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.