Jump to content


Photo

PHP search mySQL


  • Please log in to reply
4 replies to this topic

#1 valleydr

valleydr
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 28 May 2006 - 08:36 AM

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:

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

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

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.

#2 wild_dog

wild_dog
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 28 May 2006 - 09:11 AM

[!--quoteo(post=377774:date=May 28 2006, 03:36 AM:name=valleydr)--][div class=\'quotetop\']QUOTE(valleydr @ May 28 2006, 03:36 AM) View Post[/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:

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

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

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




#3 valleydr

valleydr
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 28 May 2006 - 05:06 PM

I am using mySQL. There isn't any way to just somehow do smething like:

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

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.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 May 2006 - 12:22 AM

AND ((c.features LIKE '%$keyword%')
  OR  (c.engine LIKE '%$keyword%')
  OR (c.drive LIKE '%$keyword%' ))

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 valleydr

valleydr
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 29 May 2006 - 05:50 AM

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.


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'='')";






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users