controllers/Cspages.php

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

views/index.php

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

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

    }

Now the result is:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$pic_item

Filename: views/index.php

Line Number: 214

I wonder how to fix this result so that I could see the picture appears on my view.

Member Avatar for diafol

How about var_dumping the object (or stdClass) to see what's in it. The naming of your variables leaves something to be desired. Name them so that you don't have the same object name as a property name.

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

How about changing the method or create a new model method...(simplified - no error trapping included)...

public function get_picture_item( $pic_unique_id )
{
    $query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
    $row =  $query->row();
    return $row->pic_item;
}

I try your model and I still get the same error messages:

Severity: Notice

Message: Undefined property: stdClass::$pic_item

Filename: models/Gallery_model.php

Line Number: 23

gallery_model.php

public function get_picture($pic_unique_id)
    {

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

    }

row 23: return $row->pic_item;

Member Avatar for diafol

Could this mean that pic_item is not the name of your field?

I thought I define it already in

views/index.php

<?php foreach($pic as $pic_item): ?>
Member Avatar for diafol

No I'm talking about the model. You are getting an error in the model AFAIK.

 return $row->pic_item;

This has nothing to do with a foreach loop.

Member Avatar for diafol

Sorry, just thought - if you are preparing a loop in the view - it won't work as the method just returns the single filename, not an array of records.

View

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

Model

public function get_picture_item( $pic_unique_id )
{
    $query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
    $row =  $query->row();
    return $row->pic_item;
}

Controller

$data = [];
...
$data['pic'] = $this->gallery_model->get_picture_item($pic_unique_id);
$this->load->view('index', $data);

controllers/Cspages.php

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

public function get_picture($pic_unique_id)
        {
            $query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
            $row = $query->row();
            return $row->pic_item; 
        }

views/index.php

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

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$pic_item

Filename: models/Gallery_model.php

Line Number: 18

line 18:
return $row->pic_item;

Member Avatar for diafol

This was my point. What are the names of the fields in that table?

Any clue how to fix this error:

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)

It seems like I figure the answer finally yet still cannot comprehand the codes. Can someone help explain this to me:

<?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? Why $pic_item->galleries_picture_name works?

Member Avatar for diafol

This was my point. What are the names of the fields in that table?

Your response to my continued help was...

Well, I post similar question in codeigniter forum:
https://forum.codeigniter.com/thread-67155.html
Perhaps if anyone have an account there or can continue the answer here and help me out.

So you're not just unhappy with my help, you want to drag members to another forum to help you.

I had reason to believe the issue was the fact that the pic_item field did not exist and that was the cause of your errors. You've let it slip, that this field actually DOESN'T exist - it is galleries_picture_name.

Now you ask a totally different question. And repeat the issue with foreach.

FFS - not much makes me annoyed - but this takes the biscuit. Good luck with it.

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.