doudao9896
doudao9896
2014-06-19 14:26

PHP错误“试图获取非对象的属性”

OK, I have been having trouble for the week with this error, and what I've found i think i understand but when i change it it doesn't work. So if someone could help me understand a little better and tell me why this is not working.

The RunDown- I am trying to Display a list of the different Product families on my table.

Ok. So here is my Class

 <?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
", $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
", $db->error);
        exit();
    }

    //return array
    return $categories;
} //End of get_categories
}

?>

Now here is how i am calling it for the 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&ensp;&raquo;</span></p></a></div></div>';
                     //   echo '<a href="/' . '_products/' . '"><p>' . $category_description . '<span>Learn More&ensp;&raquo;</span></p></a></div></div>';
                    }
            ?>

Then I get This error on the page:

An error occurred in script '/websites/hoffmansales.local/views/mclean/content.php' on line 29: Trying to get property of non-object An error occurred in script '/websites/hoffmansales.local/views/mclean/content.php' on line 30: Trying to get property of non-object

Lines 29: and 30 is this:

 $Product_Family = $Caterogry->Product_Family;
 $Product_Sub_Family = ($Caterogry->Product_Sub_Family);

Any Help or guidance will be appreciated , I have also tried

var_dump($Product_Family);
var_dump($Caterogry->Product_Family);
exit();

and for some reason it brings back NULL? But why?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dtamho6404 dtamho6404 7年前

    Your foreach is incorrect:

                    foreach($menu as $Caterogry => $Caterogry){
    

    Your key and value variables are the same, which screws up the loop:

    $arr = array('a' => 'b', 'c' => 'd');
    
    foreach($arr as $key => $key) {
        echo $key;
    }
    

    output: ac

    So in your case, you're working on the array KEY, which is not an object. It's just a string.

    点赞 评论 复制链接分享

为你推荐