如何显示结果

时间:2014-12-17 21:07:03

标签: php mysql pdo

我正在使用PDO,同时遵循MVC模式,我正在尝试创建多个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是查询所依赖的,就像我在buy.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来显示结果。然而,它没有显示任何东西,我不知道我做错了什么,有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

你有这条线,我认为它在你的控制器中:

  $SearchCar = $CarData->SearchCar($query2);

在您看来,您正试图获得$view->CarData2

的价值

然后我认为您需要在控制器中设置CarData2值,我不知道您的结构是什么,但它可能是这样的:

  $view->CarData2 = $CarData->SearchCar($query2);