1

so i wanna load image from database using codeigniter. here's my code:
Controller: Pweb

    public function display($id=FALSE)
    {
        if ($id==FALSE){
        $data["home_post"] = $this->M_pweb->displays();
        $this->load->view('header');
        $this->load->view('upload', $data);
        $this->load->view('footer');

        }else{
            $data["post"] = $this->M_pweb->displays($id);
        $this->load->view('header');
        $this->load->view('upload', $data);
        $this->load->view('footer');
        }
    }


Models: M_pweb

    public function displays($id=FALSE)
    {
        if ($id==FALSE){
            return $this->db->get("post")->result_array();

        }else{
            $query = $this->db->get_where("post", array('id'=>$id));
            return $query->row();
        }
        
    }


Views: upload

    <ul class="collection">
    <?php foreach ($home_post as $data ): ?>
    <li class="collection-item avatar">
        <img src="<?=site_url("upload/post/".$data["filename"]) ?>" class="circle">
        <p class="title"><?= $data["name"];?></p>
        <small><?= $data["description"];?></small>
        <a href="<?= site_url("pweb/upload/".$data["id"]) ?>" class="secondary-content">
            <i class="material-icons">visibility</i>
        </a>
    </li>
    <?php endforeach ?>
    </ul>


It give error like this:

A PHP Error was encountered Severity: Notice

Message: Undefined variable: home_post

Filename: views/upload.php

Line Number: 47

Backtrace:

File: C:\xampp\htdocs\pweb\application\views\upload.php Line: 47

Function: _error_handler

File: C:\xampp\htdocs\pweb\application\controllers\Pweb.php Line: 117

Function: view

and

A PHP Error was encountered Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/upload.php

Line Number: 47

Backtrace:

File: C:\xampp\htdocs\pweb\application\views\upload.php Line: 47

Function: _error_handler

File: C:\xampp\htdocs\pweb\application\controllers\Pweb.php Line: 117

Function: view

1 Answer 1

0

Just replace your controller with the below code: ( In your HTML code, foreach loop uses the "home_post" variable which is not defined in the else condition of your controller.

Controller: Pweb

public function display($id=FALSE)
{
    if ($id==FALSE){
    $data["home_post"] = $this->M_pweb->displays();
    $this->load->view('header');
    $this->load->view('upload', $data);
    $this->load->view('footer');

    }else{
        $data["home_post"] = $this->M_pweb->displays($id);
                  ^
        $this->load->view('header');
        $this->load->view('upload', $data);
        $this->load->view('footer');
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, but it has no change and still error. i'm curious why i can't read data with this algorithm
@initialha please update your question with updated code so we will figure out where the issue is. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.