CodeIgniter Forums

Full Version: Why this error appears stdClass::$pic_item?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
site/views/index.php


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


controllers/Cspages.php

  1.     public function index()    {                $this->load->helper('url');                $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);    }
models/Gallery_model.php


  1. public function get_picture($pic_unique_id)    {                      $query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));        return $query->result();         }
Hello,

How to handle this error message?


A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$pic_item
Filename: views/index.php
Line Number: 214
Backtrace:
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\views\index.php Line: 214 Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\controllers\Cspages.php Line: 31 Function: view
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\index.php Line: 315 Function: require_once
http://localhost/masterlinkci2/assets1/images/slider/"> }


views/index.php

Line Number: 214


<img src="<?php echo base_url('assets1/images/slider/'.$pic_item->pic_item );?>">
READ the error message! You are looping through a query result, assigning each to $pic_item. Then, in your view, you attempt to reference the pic_item property of $pic_item, and you are being told that $pic_item is not an object. Do a var_dump of $pic_item to see what it is.
You would think by now he would know how to use the CODE tags!
I try to do the var_dump, the error remains the same as if nothing happens:


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


A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$pic_item

Filename: views/index.php

Line Number: 214
READ the error message! You are looping through a query result, assigning each to $pic_item. Then, in your view, you attempt to reference the pic_item property of $pic_item, and you are being told that $pic_item is not an object. Do a var_dump of $pic_item to see what it is.

You'll need to do the var_dump before referencing $pic_item->pic_item. Your var_dump is not reached beecause of the error message.
var_dump is string(2) "18"
Annnnnnnd ... there's your answer. $pic_item is a string, not an object with a nested pic_item property.

The code inside your loop only needs to be
Code:
<img src="<?php echo base_url('assets1/images/slider/'.$pic_item);?>">
A PHP Error was encountered

    Severity: 4096

    Message: Object of class stdClass could not be converted to string

    Filename: views/index.php

    Line Number: 213

    Backtrace:

    File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\views\index.php
    Line: 213
    Function: _error_handler

    File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\controllers\Cspages.php
    Line: 32
    Function: view

    File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\index.php
    Line: 315
    Function: require_once


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


TABLE

galleries_pictures

1  picture_unique_id   int(11)

2  gallery_id  int(3)

3  galleries_picture_name   varchar(30)
I finally get the answer but still cannot explain my own answer if anyone can help me explain this codes out:

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

First, do I really need the foreach loop? Second, why $pic_item->galleries_picture_name works?
When you ask question on here if you have code you can use the buttons in the editor one is call php and code


[Image: 3F0c2dFBacMA.png]

Also might be best to watch some tutorials on you tube might learn some more stuff about codeigniter that way.