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

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);
}

models/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();

}

How to fix the following error?

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/"> }

Davy, you are so totally not getting PHP. It is C++ for server-side web development. Treat it as such (you do know C++, right?) and it will quickly become a breeze to work with! I had to remediate about 10K lines of PHP/HTML/JavaScript code at Nokia as a principal engineer there. When I was done, we had about 1K lines of code, it was 10x faster, and no errors!

Gee, I still do not understand the error message eventhough I know C++ (It was only back in college).

Perhaps if anyone can help me out:

Line 214:

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;
                ?>
Member Avatar for Mark_k

So, you're iterating over $pic and you're expecting that $pic is a list (array) or map (associative array). In this $pic list/map you're expecting that each value set here as $pic_item is an object that contains a property called $pic_item.

$pic_item->pic_item

I assume that you simply need $pic_item and that pic_item is the name of the image. Maybe instead of "pic_item" go with "filename".

If this is indeed a list of pictures call it $data['pics'] or even better $data['picList'], $data['pictureList'], $data['galleryList'] or something to that affect. Make it explicit that it is a collection of things so that it's not as confusing to look at down the line.

So in this case I would probably write this:

<?php foreach ($galleryList as $picture): ?> // <- no curly brace required here
    <img src="<?= base_url('assets1/images/slider/' . $picture['filename'] ); ?>">                   
//  <- no curly brace required here
<?php endforeach; ?>

Give that a shot and hit me back.

TIP: If you're not sure what the data structure is of the variable that you're dealing with try

var_dump($yourVariableHere)

Can be very helpful.

Member Avatar for Mark_k

Sorry. I should've gotten more into the CI side of things on my above post. you're only problem is that you need to call ->result() on your db result object.

The result is then iterable and each row is an object of stdClass.

So:

<?php if ($galleryList->num_rows()): ?>

<?php foreach ($galleryList->result() as $picture): ?> // <- no curly brace required here
    <img src="<?= base_url('assets1/images/slider/' . $picture->filename ); ?>">                   
//  <- no curly brace required here
<?php endforeach; ?>

<?php endif; ?>

Or in your case if you don't want to change the names of any of your variables:

<?php if ($pic->num_rows()): ?>
<?php foreach($pic->result() as $row): ?> 
    <img src="<?php echo base_url('assets1/images/slider/'.$row->pic_item );?>">
<?php endforeach; ?>
<?php endif; ?>
Member Avatar for Mark_k

BTW, if you're getting 0 back from $pic->num_rows(), check that your criteria is correct. picture_unique_id seems like an odd name for an id column to me. If that's what you named it, cool, but simply id would suffice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.