PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
am am still very new to mysql and php coding. I am making an estate agent site with a property search feature. i hate it all working how i want it to but now i have thought of another option for them to choose but dont know how to go about it.
as you can see i have 4 'option' drop down boxes and they can choose which options they want and it will search mysql but now i want it to so they can have one box as 'NO Pref' so it will not search that dropdown box but will still use the other 3 dropdown boxes to search my sql. i think i need and if statment but not sure how to code.
<?php
echo "<table border='0' cellpadding='0' align='center' width='600'>";
echo "<tr>";
echo "<td width=100' align='center'></td>";
echo "<td width='120' align='left'>Address</td>";
echo "<td width='60' align='center'>Bedrooms</td>";
echo "<td width='60' align='center'>Type</td>";
echo "<td width='70' align='center'>Area</td>";
echo "<td width='80' align='center'>Price</td>";
echo "<td width='80' align='center'></td>";
echo "</tr>";
echo "</table>";
$type=$_GET['type'];
$bedrooms=$_GET['bedrooms'];
$maxprice=$_GET['maxprice'];
$area=$_GET['area'];
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("***","***","***");
//select which database you want to edit
mysql_select_db("djjamiegee1");
//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * from properties WHERE type = '$type' AND bedrooms >= '$bedrooms' AND maxprice < '$maxprice' AND area LIKE '$area' ORDER BY bedrooms
;");
//grab all the content
while($r=mysql_fetch_array($result))
{
$type=$r["type"];
$bedrooms=$r["bedrooms"];
$maxprice=$r["maxprice"];
$area=$r["area"];
$first_address=$r["first_address"];
$link=$r["link"];
$image=$r["image"];
$postcode=$r["postcode"];
//display the row
echo "<table border='0' cellpadding='0' align='center' width='600' class='search'>";
echo "<tr'>";
echo "<td width='100'><img src=\"" . $r["image"] . "\"></td>";
echo "<td width='120'>$first_address<br>$postcode</td>";
echo "<td width='60' align='center'>$bedrooms</td>";
echo "<td width='60' align='center'>$type</td>";
echo "<td width='70' align='center'>$area</td>";
echo "<td width='80' align='center'>£$maxprice</td>";
echo "<td width='80' align='center'><a href=\"" . $r["link"] . "\">
<img src=\"info.png" . "\" border=0 alt=\"" . "\">
</a></td>" ;
echo "</tr>";
echo "</table>";
echo "<br>";
}
?>
many thanks in advance sorry if i havent explained very well.
$sql = "
SELECT *
FROM properties
WHERE 1
";
if (!empty($type)) {
$sql .= " AND type = '". mysql_real_escape_string($type) ."'";
}
//... other vars
$sql .= "
ORDER BY bedrooms
";
$result = mysql_query($sql) or die(mysql_error());
In this example, if the $type variable is empty (assuming your "not specified" value is blank), it will not be included in the query.
thank for your quick reply and erasing the credentials didnt even realise. i have added the code and the first bit works great just want i want it to do but when it comes to the bedrooms bit it doesnt filter them. i.e if i put 3 or more bedrooms it still displays the 1 and 2 bedroom properties.
$sql = "
SELECT *
FROM properties
WHERE 1
";
if (!empty($type)) {
$sql .= " AND type = '". mysql_real_escape_string($type) ."'";
}
//... other vars
$sql .= "
and bedrooms >= '$bedrooms' ORDER BY bedrooms
";
$result = mysql_query($sql) or die(mysql_error());
if i want to do that for the other search boxes do i just create another query the same but change the varibles??
<?php
echo "<table border='0' cellpadding='0' align='center' width='600'>";
echo "<tr>";
echo "<td width=100' align='center'></td>";
echo "<td width='120' align='left'>Address</td>";
echo "<td width='60' align='center'>Bedrooms</td>";
echo "<td width='60' align='center'>Type</td>";
echo "<td width='70' align='center'>Area</td>";
echo "<td width='80' align='center'>Price</td>";
echo "<td width='80' align='center'></td>";
echo "</tr>";
echo "</table>";
$type=$_GET['type'];
$bedrooms=$_GET['bedrooms'];
$maxprice=$_GET['maxprice'];
$area=$_GET['area'];
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("*****","***","***");
//select which database you want to edit
mysql_select_db("*****");
//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * from properties WHERE type = '$type' AND bedrooms >= '$bedrooms' AND maxprice < '$maxprice' AND area LIKE '$area' ORDER BY bedrooms
;");
$sql = "
SELECT *
FROM properties
WHERE 1
";
if (!empty($type)) {
$sql .= " AND type = '". mysql_real_escape_string($type) ."'";
}
//... other vars
$sql .= "
AND bedrooms >= '$bedrooms' ORDER BY bedrooms
";
$result = mysql_query($sql) or die(mysql_error());
//grab all the content
while($r=mysql_fetch_array($result))
{
$type=$r["type"];
$bedrooms=$r["bedrooms"];
$maxprice=$r["maxprice"];
$area=$r["area"];
$first_address=$r["first_address"];
$link=$r["link"];
$image=$r["image"];
$postcode=$r["postcode"];
//display the row
echo "<table border='0' cellpadding='0' align='center' width='600' class='search'>";
echo "<tr'>";
echo "<td width='100'><img src=\"" . $r["image"] . "\"></td>";
echo "<td width='120'>$first_address<br>$postcode</td>";
echo "<td width='60' align='center'>$bedrooms</td>";
echo "<td width='60' align='center'>$type</td>";
echo "<td width='70' align='center'>$area</td>";
echo "<td width='80' align='center'>£$maxprice</td>";
echo "<td width='80' align='center'><a href=\"" . $r["link"] . "\">
<img src=\"info.png" . "\" border=0 alt=\"" . "\">
</a></td>" ;
echo "</tr>";
echo "</table>";
echo "<br>";
}
?>
hey sorry for all the confusing i am very new to php and have only just joint this forum, i have posted my php code with the bit of code you have already done i was now just wondering how i can apply the same rule to all the option boxes. i.e if type blnak and bedrooms blank search maxprice and area.
i have tried a number of differnet ways using else and else if but still cant work it out, i know i am missing something obvious but just cant put me finger on it
$sql = "
SELECT *
FROM properties
WHERE 1
";
if (!empty($type)) {
$sql .= " AND type = '". mysql_real_escape_string($type) ."'";
}
//... other vars
$sql .= "
AND bedrooms >= 'bedrooms'
";
$result = mysql_query($sql) or die(mysql_error());
or would i add more circumstances to the one above.
$sql = "
SELECT *
FROM properties
WHERE 1
";
if (!empty($type)) {
$sql .= " AND type = '". mysql_real_escape_string($type) ."'";
}
if (!empty($bedrooms)) {
$sql .= " AND bedrooms >= ". (int)$bedrooms;
}