我在这里不知所措。我不能为我的生活找出为什么我得到错误。
Notice: Undefined index: price in /home/*****/public_html/newfinal/home_view.php on line 14
Notice: Undefined index: description in /home/****/public_html/newfinal/home_view.php on line 15
以下是相关的代码段:
PHP / SQL
function get_product($product_id) {
global $db;
$query = "SELECT * FROM prjProducts p"
. " INNER JOIN prjCategories c"
. " ON p.categoryID = c.categoryID"
. " WHERE productID = :product_id";
try {
$stmt = $db->prepare($query);
$stmt->bindValue(':product_id', $product_id);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$result = $stmt->fetchAll();
$stmt->closeCursor();
return $result;
} catch (PDOException $ex) {
$error = $ex->getMessage();
echo $error;
}
}
PHP
// Set the featured product Id's in an array
$product_ids = array(3, 5, 10);
// Get an array of featured producst from the database
$products = array();
foreach ($product_ids as $product_id) {
$product = get_product($product_id);
$products[] = $product; // add the product to the array
}
HTML
<?php foreach ($products as $product) :
// Get product data
$price = $product['price'];
$description = $product['description'];
我现在已经在这个问题上敲了几个小时。我试图回应阵列,我可以毫无问题地打印出阵列。我确定这是一些我在某处遗漏的小格式错误。
答案 0 :(得分:0)
首先检查表price
中是否有description
和prjProducts
列。
在foreach ($products as $product)
循环中,为var_dump
执行$product
并检查它是array
还是object
?< / p>
如果它不是数组,您应该$product->price
而不是$product['price']
。