FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Mysql seach for 4 argument or 1 or any conbination of them??





flatliner
Ok I got a database and I have 4 drop down select box for a user seach query, they need to be able to seach for all 4 arguments or just 1 argument or 2 or 3 , any combination of them. I have tryed a few ways of doing this but I cant get it 2 work , I have found other ways to do it but its a very long process. is there any shorter way? Here is somthing I was working on. but cant get it to work probly with the large if() statment.


Code:
mysql_connect("localhost", "$user", "$pass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$property_type     = $_POST[property_type];
$price_range       = $_POST[price_range];
$location          = $_POST[location];
$number_of_room    = $_POST[number_of_room];
$empty = "";

if($property_type != $empty)
$argument1 = "($property_type == $row[property_type])";

if($price_range != $empty)
$argument2 = "&($price_range == $row[price_range])";

if($location  != $empty)
$argument3 = "&($location  == $row[location])";

if($number_of_room != $empty)
$argument4 = "&($number_of_room == $row[number_of_room])";


$result = mysql_query("SELECT * FROM property_database")
or die(mysql_error());

echo "<table id=\"table\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\"><tr>\n";
// fetching from query 1
while($row = mysql_fetch_array( $result )) {

if($argument1 $argument2 $argument3 $argument4)
{echo "
<td style=\"padding:25px; background-color:#D0DDF0;\"  style=\"-moz-opacity:0.4;filter:alpha(opacity=40)\"
onmouseover=\"this.style.MozOpacity=1;this.filters.alpha.opacity=100\"
onmouseout=\"this.style.MozOpacity=0.4;this.filters.alpha.opacity=40\">
<a class=\"thumbnaillinks\" href=\"$row[url]\"> <img id=\"thumbnail\" src=\"$row[picture]\" border=\"0\">
<br>Price:&nbsp;&nbsp;$row[price]
<br>location:&nbsp;&nbsp;$row[location]
<br>No.of Bedrooms:&nbsp;&nbsp;$row[number_of_room]</a></div></td>";}
else
{};

};
?>
flatliner
I also tryed this , but found it to be way to long and stopped.

Code:

if($property_type == $empty)
{}
else
{$result = mysql_query("SELECT * FROM property_database WHERE property_type = '$property_type'")
or die(mysql_error());};

// above query is for argument of property type only and nothing else


if($price_range == $empty)
{}
else
{$result = mysql_query("SELECT * FROM property_database WHERE price_range = '$price_range'")
or die(mysql_error());};

// above query is for argument of price range only and nothing else


if($location == $empty)
{}
else
{$result = mysql_query("SELECT * FROM property_database WHERE location = '$location'")
or die(mysql_error());};

// above query is for argument of location only and nothing else


if($number_of_room == $empty)
{}
else
{$result = mysql_query("SELECT * FROM property_database WHERE number_of_room = '$number_of_room'")
or die(mysql_error());};

// above query is for argument of number_of_room only and nothing else

// END of single arguments

if(($property_type == $empty) && ($number_of_room == $empty) )
{}
else
{
$result = mysql_query("SELECT * FROM  property_database WHERE property_type = '$property_type' AND number_of_room = '$number_of_room'")
or die(mysql_error());};


if(($property_type == $empty) && ($location == $empty) )
{}
else
{
$result = mysql_query("SELECT * FROM  property_database WHERE property_type = '$property_type' AND location = '$location'")
or die(mysql_error());};




flatliner
ok well after a good few hours of trying to solve this probelm with very complex methods I have found nothing, lol , but what it boiled down to in the end was a very easy solution which was straighting me right in the face lol. hate when its so easy like that and you send like 2 days trying to work it out, although I have happy I got it Very Happy. here is my solution code below

Code:

 
  <?php
 
$user = "flatline_user";
$pass = "co2dryice";
$db   = "redstallion";

 
mysql_connect("localhost", "$user", "$pass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$property_type     = $_POST[property_type];
$price_range       = $_POST[price_range];
$location          = $_POST[location];
$number_of_room    = $_POST[number_of_room];
$empty = "";

$result2 = mysql_query("SELECT * FROM property_database")
or die(mysql_error());

$row2 = mysql_fetch_array( $result2 );

$argument1 = "property_type LIKE '%$property_type%'";
$argument2 = "price_range LIKE '%$price_range%' ";
$argument3 = "location LIKE '%$location%' ";
$argument4 = "number_of_room LIKE '%$number_of_room%'";


$result = mysql_query("SELECT * FROM property_database WHERE
$argument1 AND
$argument2 AND
$argument3 AND
$argument4 ")
or die(mysql_error());



echo "<table id=\"table\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\"><tr>\n";
// fetching from query 1
while($row = mysql_fetch_array( $result )) {


echo "
<td style=\"padding:25px; background-color:#D0DDF0;\"  style=\"-moz-opacity:0.4;filter:alpha(opacity=40)\"
onmouseover=\"this.style.MozOpacity=1;this.filters.alpha.opacity=100\"
onmouseout=\"this.style.MozOpacity=0.4;this.filters.alpha.opacity=40\">
<a class=\"thumbnaillinks\" href=\"property_display.php?id=$row[id]\"> <img id=\"thumbnail\" src=\"$row[picture]\" border=\"0\">
<br>Price:&nbsp;&nbsp;$row[price]
<br>location:&nbsp;&nbsp;$row[location]
<br>No.of Bedrooms:&nbsp;&nbsp;$row[number_of_room]</a></div></td>";


};
?>







Related topics
PHP, MySQL...
What are the versions of PhP and MySQL on this server...
info
php can't connect to mysql
Version info?
How does one install Joomla onto the server
PHP and SEO
Setup php,mysql and others...
MySQL and PHP problem in the future
how do i set up a free host?
help me i'm stupid
Error connecting to mysql ; please help
Starting with PHP and going to place with no internet.
MySQL Set-up Structure - Help!!!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.