Jump to content

[SOLVED] There has to be an easier way then this


networkthis

Recommended Posts

Heres what I am trying to do... I am trying to simplify my script.  I have several search options on my page.  I am trying to get the variables if passed and add them into a mysql query...right now I have many single and multiple queries... example if a user selects only one field it will search only that field, if they search two fields it will search them both, three fields it will search all three etc......  and display the results.... here is what I have so far

 

<?php

if ($city) {
$data_p = mysql_query("SELECT * FROM list WHERE city LIKE '%$city%' AND status LIKE '%active%'") or die(mysql_error());
$total_rows = mysql_num_rows($data_p); 
}

if ($city && $country) {
$data_p = mysql_query("SELECT * FROM list WHERE city LIKE '%$city%' AND country LIKE '%$country%' AND status LIKE '%active%'") or die(mysql_error());
$total_rows = mysql_num_rows($data_p); 
}


if ($city && $country && $postion_type) {
$data_p = mysql_query("SELECT * FROM list WHERE city LIKE '%$city%' AND country LIKE '%$country%' AND postion_type LIKE '%$postion_type%' AND status LIKE '%active%'") or die(mysql_error());
$total_rows = mysql_num_rows($data_p); 
}

?>

 

I guess my question is, is there a way to simplify this process...because this has become extremely long very quickly.  All of my results are being displayed by GET variables and I am using pagination in order to display the results.  Any help, guidance or direction is greatly appreciated.  Thank you!

Hi networkthis,

 

Absolutely. Its really easy! I'm just going to post a sample, if you have any questions just let me know!

 

<?php

$query = "SELECT * FROM list WHERE ";

if ($city) 
$query .= "city LIKE '%$city%' AND ";

if ($country)
$query .= "country LIKE '%$country%' AND ";

if ($postion_type)
$query .= "postion_type LIKE '%$postion_type%' AND ";

$query .= "status LIKE '%active%'";

$data_p = mysql_query($query) or die(mysql_error());
$total_rows = mysql_num_rows($data_p); 

?>

 

Its not the only way one can do it, but this way is pretty intuitive.

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.