为什么这个错误出现stdClass :: $ pic_item?

时间:2017-01-21 11:44:25

标签: codeigniter

站点/视图/ index.php的

<?php foreach($pic as $pic_item): ?> { 
                            <img src="<?php echo base_url('assets1/images/slider/'.$pic_item->pic_item );?>">                   
                    } 
                    <?php endforeach;
                    ?>

控制器/ Cspages.php

    public function index()
{


    $this->load->model('gallery_model');

    $pic_unique_id = 18;  // slider

    $data['pic'] = $this->gallery_model->get_picture($pic_unique_id);

    $this->load->view('index', $data);
}

模型/ Gallery_model.php

public function get_picture($pic_unique_id)
{

    $query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
    return $query->result();

}

如何解决以下错误?

遇到PHP错误

严重性:注意

消息:未定义属性:stdClass :: $ pic_item

文件名:views / index.php

行号:214

回溯:

文件:C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC9 \ data \ localweb \ masterlinkci2 \ application \ site \ views \ index.php     行:214     功能:_error_handler

文件:C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC9 \ data \ localweb \ masterlinkci2 \ application \ site \ controllers \ Cspages.php     行:31     功能:查看

文件:C:\ Program Files(x86)\ EasyPHP-DevServer-14.1VC9 \ data \ localweb \ masterlinkci2 \ index.php 行:315 功能:require_once

http://localhost/masterlinkci2/assets1/images/slider/“&gt;}

1 个答案:

答案 0 :(得分:0)

在视图中更改您的foreach循环,如下所示:

<?php foreach($pic as $pic_item): ?>     
<img src="<?php echo base_url('assets1/images/slider/').$pic_item->pic_item;?>">               
<?php endforeach;?>

OR

<?php foreach($pic as $pic_item): ?> 
    <img src="<?php echo base_url('assets1/images/slider/'.$pic_item->pic_item );?>">                   
<?php endforeach;?>

<强>更新

模型

<?php
    public function get_picture($pic_unique_id)
    {

        $this->db->where('picture_unique_id',$pic_unique_id);
        $query = $this->db->get_where('galleries_pictures');
        return $query->row;

    }

控制器

public function index()
{

    $this->load->helper('url');//load url helper
    $this->load->model('gallery_model');

    $pic_unique_id = 18;  // slider

    $data['pic'] = $this->gallery_model->get_picture($pic_unique_id);

    $this->load->view('index', $data);
}

视图

<img src="<?php echo base_url('assets1/images/slider/'.$pic->pic_item );?>">