显示产品信息php

时间:2017-08-22 14:29:14

标签: php mysql model-view-controller

您好我是php的初学者并且正在学习开发电子商务页面。 我面临的问题是我可以使用我的产品的产品网格。现在我想点击产品图片或名为" info"显示信息而不是为每个项目创建页面。它确实转到页面,但它只显示页眉和页脚,而不是 栏中的链接确实获得了产品的ID。

我正在使用MVC,所以这里是代码:

Model: ProductDataSet.php

public function fetchProductInformation($productID) {
  $sqlQuery = 'SELECT * FROM product WHERE (productID = :productID)';
  $statement = $this->_dbHandle->prepare($sqlQuery); // prepare a PDO statement
  $statement->execute(); // execute the PDO statement
    $dataSet = [];
    while ($row = $statement->fetch()) {
       $dataSet[] = new ProductData($row);
    }

    return $dataSet;
}

View: product
<?php
session_start();
if(!isset($_SESSION['emailaddress'])){

}
?>
<?php require('template/header.phtml') ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <br>

    <?php foreach ($view->productDataSet  as $productData) {
        if (count($view->productDataSet)>0) {
         echo '<div class = "col-xs-6 .col-sm-3">';
        echo '<a href="product.php?id=' . $productData->getProductID(). '"><img src="images/'. $productData->getPhoto(). ' "alt="no picture" height = ""  width = ""/> </a>' ."\n";
        echo "<p>" . $productData->getTitle(). "</p>\n";
          echo "<p>" . $productData->getDescription(). "</p>\n";
        echo "<p>£" . $productData->getPrice(). ".000</p>\n";
        //  echo '<a class="btn btn-primary" type="button" href =productDetails.php?product='.$product->getProduct().'>view</a>';
        echo '<a href="productDetails.php?ProductID='. $productData->getProductID().'><button type="button" class="btn btn-info">Info</button></a>';
        echo '<a href="product.php"><input type="submit" class = "btn btn-primary " value="Add to basket"></a>';
      //  echo '<a href="/productDetails.php?id='.$productData->getProductID().'">View details</a>';
        //  echo '<a class="btn btn-primary" type="button" href =productDetails.php?product='.$product->getProductID().'>view</a>';
        echo "</div>";

    }
  }
    ?>

</form>

<?php require('template/footer.phtml') ?>

VIEW: ProductDetails
<?php require('template/header.phtml') ?>

<h3>Welcome to Page1</h3>
<?php
  echo "<p>" . $productData->getTitle(). "</p>\n";
  ?>
<?php require('template/footer.phtml') ?>

Controller : product

<?php

require_once('Models/ProductDataSet.php');

$view = new stdClass();
$view->pageTitle = 'Products';
$view->productDataSet = null;

    $productDataSet = new productDataSet();
    $view->productDataSet = $productDataSet->fetchAllProducts();

  //  $view->watchDataSet = $watchDataSet->fetchWatch($_GET['id'])[0];

require_once('Views/product.phtml');

Controller Product Details 
<?php

$view = new stdClass();
$view->pageTitle = 'Page1';
require_once('Models/ProductDataSet.php');
$productDataSet = new productDataSet();
if(isset($_GET['productID']))
{
  $productID = $_GET['productID'];
  $view->productDataSet =$productDataSet->fetchProductInformation($productID);
}

require_once('Views/product.phtml');?>

1 个答案:

答案 0 :(得分:0)

您似乎错过了将参数实际绑定到您准备的查询的部分。在准备查询时,:productID只是查询中的占位符,您需要使用查询显式定义:productID

请注意以下对您的功能的更改:

public function fetchProductInformation($productID) {
  $sqlQuery = 'SELECT * FROM product WHERE (productID = :productID)';
  $statement = $this->_dbHandle->prepare($sqlQuery); // prepare a PDO statement
  ////////////////////////////////////////////////
  $statement->bindParam(':productID', $productID);
  ////////////////////////////////////////////////
  $statement->execute(); // execute the PDO statement
  $dataSet = [];
  while ($row = $statement->fetch()) {
    $dataSet[] = new ProductData($row);
  }

  return $dataSet;
}

此外,button元素的回显缺少href属性的结束双引号。以下是固定版本:

echo '<a href="productDetails.php?ProductID='. $productData->getProductID().'"><button type="button" class="btn btn-info">Info</button></a>';