好的,这个星期我一直在遇到这个错误,我发现我想我明白了,但是当我更改它时,它不起作用。因此,如果有人可以帮助我更好地理解并告诉我为什么它不起作用。
摘要-我正尝试在表上显示不同产品系列的列表。
好的。这是我的课
<?php
class mclean {
public $Product_id, $Catalog_Number, $Model_Code, $Caterogry, $Product_Family, $Product_Sub_Family, $Product_Name, $img;
function __construct($Caterogry){
// $db is defined in config.inc.php
global $db;
// sanitize input
$Caterogry = $db->real_escape_string($Caterogry);
// sql query
$sql = "SELECT * FROM Mclean_Products_info
WHERE Caterogry = '". $Caterogry . "'";
// if there is a result...
if($result = $db->query($sql)){
// $row contains an object
$row = $result->fetch_object();
// Set class attributes
$this->Product_id = htmlentities($row->Product_id);
$this->Catalog_Number = htmlentities($row->Catalog_Number);
$this->Model_Code = htmlentities($row->Model_Code);
$this->Caterogry = htmlentities($row->Caterogry);
$this->Product_Family = htmlentities($row->Product_Family);
$this->Product_Sub_Family = htmlentities($row->Product_Sub_Family);
$this->Product_Name = htmlentities($row->Product_Name);
$this->img = htmlentities($row->AllProductImages);
// Free result set
$result->close();
// if there are no results...
}else{
// there is probably an error
printf("Query failed: %s\n", $db->error);
exit();
}
}
// Returns an array of all categories
public static function get_categories(){
//$db is defined in config.inc.php
global $db;
global $site_brand_ids;
//sql query
$sql = "SELECT DISTINCT product_family_id FROM mclean
LEFT JOIN product_families USING(product_family_id)
LEFT JOIN categories USING(category_id)
WHERE category_id = '91'";
//if there is a result
$categories = array();
if ($result = $db->query($sql)) {
while ($row = $result->fetch_object()){
$categories[] = new mclean($row->Caterogry);
}
//Free result set
$result->close();
}else{ // if there are no results
// There is a possible error
printf("Query failws: %s\n", $db->error);
exit();
}
//return array
return $categories;
} //End of get_categories
}
?>
现在,这就是我为Display调用的方式。
<?php
$categories = array('Accessories', 'Thermal Management', 'Busbar Systems');
$menu = array();
foreach($categories as $Caterogry){
$Caterogry = new mclean($Caterogry);
$menu[$Caterogry->Product_Family] = $Caterogry;
}
foreach($menu as $Caterogry => $Caterogry){
$Product_Family = $Caterogry->Product_Family;
$Product_Sub_Family = ($Caterogry->Product_Sub_Family);
echo '<div class="large-4 columns"><div class="product_category"><h4>' . $Product_Sub_Family . '</h4>';
$Product_Sub_Family= toAscii($Product_Sub_Family);
$category_link = $Product_Family;
$category_link = strtolower($category_link);
// $category_link = str_replace("-", "_", $category_link);
echo '<a href="/' . $category_link . '"><p>' . $Product_Sub_Family . '<span>Learn More »</span></p></a></div></div>';
// echo '<a href="/' . '_products/' . '"><p>' . $category_description . '<span>Learn More »</span></p></a></div></div>';
}
?>
然后我在页面上收到此错误:
第29行的脚本'/websites/hoffmansales.local/views/mclean/content.php'中发生错误:试图获取非对象的属性脚本'/websites/hoffmansales.local/views/mclean/中发生了错误第30行的content.php”:尝试获取非对象的属性
第29行和第30行是这样的:
$Product_Family = $Caterogry->Product_Family;
$Product_Sub_Family = ($Caterogry->Product_Sub_Family);
任何帮助或指导将不胜感激,我也尝试过
var_dump($Product_Family);
var_dump($Caterogry->Product_Family);
exit();
并出于某种原因将其带回NULL?但为什么?
您的foreach错误:
foreach($menu as $Caterogry => $Caterogry){
您的key和value变量是相同的,这会弄乱循环:
$arr = array('a' => 'b', 'c' => 'd');
foreach($arr as $key => $key) {
echo $key;
}
输出: ac
因此,在您的情况下,您正在处理数组KEY,它不是对象。这只是一个字符串。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句