我在遵循 MVC 模式的同时使用 PDO,并且尝试根据用户搜索汽车时检索到的行数来创建多个 div。我的查询工作完美,但是当用户提交表单时,它不会检索任何结果,有人明白我做错了什么吗?这是到目前为止我的一些代码:
CarData.php 类:
public function SearchCar($query2){
$query = "SELECT make, model, type, colour, price, year, picture
FROM AllCars car
JOIN AllMakes mak ON mak.id = car.makeID
JOIN AllModels model ON model.id = car.modelID
JOIN AllType typ ON typ.id = car.typeID
JOIN AllColour col ON col.id = car.colourID";
$query .= $query2;
// This is where you need to start setting up the query to run it so you prepare it by putting it in a variable
$statement = $this->_dbHandle->prepare($query);
// Then execute the query
$statement->execute();
// This will return the row from the database as an array
$dataSet = [];
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$dataSet[] = new CarData2($row);
}
return $dataSet;
}
参数中的 $query2 是查询所依赖的,就像我在buying.php中一样:
if(isset($_POST["SearchCar"]))
{
if($_POST["MakeOfCar"] == 1){
$query2 = " WHERE car.makeID > 1";
}
else
{
$query2 = " WHERE car.makeID = ".$_POST['MakeOfCar'];
}
$SearchCar = $CarData->SearchCar($query2);
}
现在,当检索查询时,另一个名为 CarData2.php 的类将检索每一行,例如汽车的品牌和型号等:
protected $make, $model, $type, $colour, $price, $year,$picture;
public function __construct($row) {
$this->make = $row['make'];
$this->model = $row['model'];
$this->type = $row['type'];
$this->colour = $row['colour'];
$this->price = $row['price'];
$this->year = $row['year'];
$this->picture = $row['picture'];
}
public function getMake() {
return $this->make;
}
public function getModel() {
return $this->model;
}
public function getType() {
return $this->type;
}
public function getColour() {
return $this->colour;
}
public function getPrice() {
return $this->price;
}
public function getYear() {
return $this->year;
}
public function getPicture(){
return $this->picture;
}
这就是我在 buy.phtml 中显示结果的地方:
<?php if(isset($view->CarData2))
{
if (count($view->CarData2) > 0){
foreach($view->CarData2 as $ResultCarData) {
echo '<div class="row">';
echo '<div class="col-sm-6 col-md-4">';
echo '<div class="thumbnail">';
echo '<img src="" alt=""/>';
echo '<div class="caption">';
echo '<h2></h2>';
echo '<h4 class="colouredTitle">Make: </h4>';
echo '<h4>' .$ResultCarData->getMake(). '</h4>';
echo '<h4 class="colouredTitle">Model: </h4>';
echo '<h4>' .$ResultCarData->getModel(). '</h4>';
echo '<h4 class="colouredTitle">Type: </h4>';
echo '<h4>' .$ResultCarData->getType(). '</h4>';
echo '<h4 class="colouredTitle">Colour: </h4>';
echo '<h4>' .$ResultCarData->getColour(). '</h4>';
echo '<h4 class="colouredTitle">Year: </h4>';
echo '<h4>' .$ResultCarData->getYear(). '</h4>';
echo '<h4 class="colouredTitle">Price: </h4>';
echo '<h4>' .$ResultCarData->getPrice(). '</h4>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
else
{
echo 'No results found';
}
};
?>
正如您所看到的,我尝试在 DIV 中显示结果并循环它,因此根据检索到的行数,它会创建许多 div 来显示结果。但是它没有显示任何内容,我不知道我做错了什么,有人明白我做错了什么吗?
最佳答案
你有这行我认为它在你的 Controller 中:
$SearchCar = $CarData->SearchCar($query2);
在您看来,您正在尝试获取 $view->CarData2
然后我认为您需要在 Controller 中设置 CarData2
值,我不知道您的结构是什么,但可能是这样的:
$view->CarData2 = $CarData->SearchCar($query2);
关于php - 如何显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534869/