CodeIgniter:尝试获取非对象的属性

时间:2015-12-12 03:46:18

标签: php codeigniter

我正在尝试在我的数据库中显示特定类别的所有产品,并在链接列表上显示这样的

<?=anchor('home/display_category/' . $category='Business', 'Business',[
                    'class' => 'list-group-item'                    
                ]) ?>

带控制器上的功能

public function display_category($category)
{
    $data['products'] = $this->model_products->find_category($category);    
    $this->load->view('homepage', $data);       
}

和在model_products中

public function find_category($category){
    //Search Record based on category
    $hasil = $this->db->where('category', $category)                          
                      ->get('products');
    if($hasil->num_rows() > 0){
        return $hasil->row();
    } else {
        return array();
    }
}

这是在主页上显示产品的代码

<?php foreach ($products as $product):?>                    
    <div class="col-md-3">
        <div class="thumbnail">
            <?=img([
                'src'   =>  'uploads/' .$product->image,
                'style' =>  'max-width: 100%; max-height:100%; height:150px; width:100px;'
            ])?>
            <div class="caption" style="height:420px;">
                <h4 align="center"><?=$product->product_name?></h4>
                <hr>
                <p align="justify" style="height:180px;">   <?=$product->misc?></p>
                <hr>
                <h4 align="center">Rp.  <?=number_format($product->price,0,',','.')?></h3>
                <hr>
                <p align="center">                                  
                    <?=anchor('home/add_to_cart/' . $product->id_product, 'Add to Cart',[
                        'class' => 'btn btn-primary',
                        'style' => 'align="center"',
                        'role'  => 'button'
                    ]) ?>
                    <?=anchor('home/product_details/' . $product->id_product, 'See Details',[
                        'class' => 'btn btn-default',
                        'style' => 'align="center"',
                        'role'  => 'button'
                    ]) ?>
                </p>
            </div>
        </div>
    </div>
<?php endforeach; ?>

我有print_r display_category功能,我就这样做了

Array ( [products] => stdClass Object ( [id_product] => 1 [product_name] => Samsung Galaxy J7 [price] => 4500000 [category] => Business [brand] => Samsung [date_added] => 2015-12-12 [os_type] => Android OS [os_version] => 5.1 [network] => GSM / HSPA / LTE [display_desc] => Super AMOLED capacitive touchscreen, 16M colors [display_size] => 5.5 [wifi] => yes [bluetooth] => yes [gps] => yes [keyboard] => QWERTY [int_memory] => 16GB [ram] => 1.5GB [battery_desc] => Li-Ion battery [battery_size] => 3000 [color] => White, Black, Gold [camera_primary] => 13 [camera_secondary] => 5 [cpu] => Quad-core 1.4 GHz Cortex-A53; quad-core 1.0 GHz Cortex-A53 Octa-core 1.5 GHz [gpu] => Adreno 405 Mali-T720MP2 [resolution] => 720 x 1280 pixels (~267 ppi pixel density) [misc] => The smartphone jungle can be confusing, but the Samsung Galaxy J7 is surprisingly easy to decipher: J for "affordable price" and 7 for "big screen". This is the lite version of the Galaxy A7, offering semi-premium features but confined [image] => samsung-galaxy-j7.jpg ) )

但是当我到达主页时,我收到错误“试图获取非对象的属性” 提前谢谢!

1 个答案:

答案 0 :(得分:0)

$ products是一行,类型为std对象。您尝试像数组一样访问它。删除foreach,因为它不是数组。因此,在视图中,您可以访问:

$products->image

而不是

$product->image

如果您需要数组格式,可以使用

return $hasil->result_array(); 

而不是

return $hasil->row();

在模型函数中