doudao9896 2014-06-19 14:26
浏览 62
已采纳

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 2014-06-19 14:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料