arrays - PHP Error "Trying to get property of non-object" -
arrays - PHP Error "Trying to get property of non-object" -
ok, have been having problem week error, , i've found think understand when alter it doesn't work. if help me understand little improve , tell me why not working.
the rundown- trying display list of different product families on table.
ok. here class
<?php class mclean { public $product_id, $catalog_number, $model_code, $caterogry, $product_family, $product_sub_family, $product_name, $img; function __construct($caterogry){ // $db defined in config.inc.php global $db; // sanitize input $caterogry = $db->real_escape_string($caterogry); // sql query $sql = "select * mclean_products_info caterogry = '". $caterogry . "'"; // if there result... if($result = $db->query($sql)){ // $row contains 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 no results... }else{ // there error printf("query failed: %s\n", $db->error); exit(); } } // returns array of categories public static function get_categories(){ //$db defined in config.inc.php global $db; global $site_brand_ids; //sql query $sql = "select distinct product_family_id mclean left bring together product_families using(product_family_id) left bring together categories using(category_id) category_id = '91'"; //if there 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 no results // there possible error printf("query failws: %s\n", $db->error); exit(); } //return array homecoming $categories; } //end of get_categories } ?>
now here how calling display.
<?php $categories = array('accessories', 'thermal management', 'busbar systems'); $menu = array(); foreach($categories $caterogry){ $caterogry = new mclean($caterogry); $menu[$caterogry->product_family] = $caterogry; } foreach($menu $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>'; } ?>
then error on page:
an error occurred in script '/websites/hoffmansales.local/views/mclean/content.php' on line 29: trying property of non-object error occurred in script '/websites/hoffmansales.local/views/mclean/content.php' on line 30: trying property of non-object
lines 29: , 30 this:
$product_family = $caterogry->product_family; $product_sub_family = ($caterogry->product_sub_family);
any help or guidance appreciated , have tried
var_dump($product_family); var_dump($caterogry->product_family); exit();
and reason brings null? why?
your foreach incorrect:
foreach($menu $caterogry => $caterogry){
your key , value variables same, screws loop:
$arr = array('a' => 'b', 'c' => 'd'); foreach($arr $key => $key) { echo $key; }
output: ac
so in case, you're working on array key, not object. it's string.
php arrays foreach runtime-error instance
Comments
Post a Comment