这里我有一个搜索表单和PHP搜索代码,我似乎无法解决问题。我遇到的问题是我有一个For Sale Page和一个For Rent Page,当我使用搜索表单时,它会显示属性,但它会显示For Sale Page和For Rent Page的所有属性。 如果我选择For Sale选项,那么它需要显示For Sale的所有属性,而For Rent也是如此。
<div class="col-lg-4 col-md-4 col-sm-12">
<div class="searchmodule clearfix" data-effect="slide-right">
<form id="advanced_search" action="search.php" class="clearfix" name="advanced_search" method="post">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="type"><b>Type</b></label>
<select name="type">
<option value=""></option>
<option value="House">House</option>
<option value="Apartment">Apartment</option>
<option value="Flat">Flat</option>
<option value="Townhouse">Townhouse</option>
<option value="Plot">Plot</option>
<option value="Farm">Farm</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="status"><b>Status</b></label>
<select name="status">
<option value=""></option>
<option value="For Sale">For Sale</option>
<option value="For Rent">For Rent</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="bedrooms"><b>Bedrooms</b></label>
<select name="bedrooms">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="baths"><b>Bathrooms</b></label>
<select name="baths">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="min_price"><b>Min Price</b></label>
<select name="min_price">
<option value=""></option>
<option value="1000">1000</option>
<option value="2000">2000</option>
<option value="3000">3000</option>
<option value="4000">4000</option>
<option value="5000">5000</option>
<option value="6000">6000</option>
<option value="7000">7000</option>
<option value="8000">8000</option>
<option value="9000">9000</option>
<option value="10000">10000</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="max_price"><b>Max Price</b></label>
<select name="max_price">
<option value=""></option>
<option value="100000">100000</option>
<option value="200000">200000</option>
<option value="300000">300000</option>
<option value="400000">400000</option>
<option value="500000">500000</option>
<option value="600000">600000</option>
<option value="700000">700000</option>
<option value="800000">800000</option>
<option value="900000">900000</option>
<option value="1000000">1000000</option>
</select>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<button name="search" type="submit" class="btn btn-success">SEARCH PROPERTY</button>
我仍然是整个PHP和MySQL的新手,所以请帮助我。
这里我有搜索的PHP编码,问题就在这里,我只是不知道:
<?php
include 'database.php';
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM properties INNER JOIN images ON images.propertyid = properties.propertyid WHERE Status ='For Sale' OR Status = 'For Rent' GROUP BY images.propertyid";
foreach ($pdo->query($sql) as $row) {
$propertyid = $row['PropertyId'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql1 = "SELECT * FROM images where PropertyId = ?";
$q1 = $pdo->prepare($sql1);
$q1->execute(array($propertyid));
$data = $q1->fetch(PDO::FETCH_ASSOC);
$type = $_POST['type'];
$status = $_POST['status'];
$minprice = $_POST['min_price'];
$maxprice = $_POST['max_price'];
$bedrooms = $_POST['bedrooms'];
$baths = $_POST['baths'];
if ($status != ''){
$qStatus = " AND status = '$status' ";
$sql = $sql . $qStatus;
}
if ($type != ''){
$qType = " AND type = '$type' ";
$sql = $sql . $qType;
}
if ($baths != ''){
$qBaths = " AND baths = '$baths' ";
$sql = $sql . $qBaths;
}
if ($bedrooms != ''){
$qBedrooms = " AND bedrooms = '$bedrooms' ";
$sql = $sql . $qBedrooms;
}
if ($minprice != ''){
$qMinprice = " AND min_price = '$minprice' ";
$sql = $sql . $qMinprice;
}
if ($maxprice != ''){
$qMaxprice = " AND max_price = '$maxprice' ";
$sql = $sql . $qMaxprice;
}
$sql = $sql." GROUP BY PropertyId";
if ($status == 'For Sale'){
echo '<li class="col-md-4">';
echo '<div class="proj_block">';
echo '<img alt="" style="opacity: 1" src="'. $row['ImageUrl'].'"></div>';
echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';
echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';
echo '<a href="readmore.php?PropertyId='.$row['PropertyId'].'">Read More</a>';
echo '</li>';
}
else if ($status == 'For Rent'){
echo '<li class="col-md-4">';
echo '<div class="proj_block">';
echo '<img alt="" style="opacity: 1" src="'. $row['ImageUrl'].'"></div>';
echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';
echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';
echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';
echo '<a href="readmore.php?PropertyId='.$row['PropertyId'].'">Read More</a>';
echo '</li>';
}
}
Database::disconnect();
?>
答案 0 :(得分:0)
你可以像这样使用LEFT JOIN:
SELECT * FROM PROPERTIES as Prop
LEFT JOIN Images as Img
ON Img.PropertyId = Prop.PropertyId
WHERE **** Your condition ****
ORDER BY ** (DESC or ASC)