0

I am a beginner in Codeigniter framework and I am also learning from youtube

In my admin.php code is

<?php 
class Admin extends MY_Controller {
    public function dashboard(){
        $this->load->model('articlesmodel');
        $articles = $this->articlesmodel->articles_list();
        $this->load->view('admin/dashboard', ['articles'=> $articles]);
    }
}
?>

And in my dashboard.php code is

<table class="table">
    <thead>
        <th>Sr. No</th>
        <th>Title</th>
        <th>Action</th>
    </thead>
    <tbody>
        <?php if ( count($articles) ):?>
            <?php foreach($articles as $article): ?>
                <tr>
                    <td>1</td>
                    <td>
                        <?= $article->title ?>
                    </td>
                    <td>
                        <a href="" class="btn btn-info">Edit</a>
                        <a href="" class="btn btn-danger">Delete</a>
                    </td>
                </tr>
        <?php endforeach;  ?>
        <?php else:?>
            <tr>
                <td colspan="3">No Records Found</td>
            </tr>
        <?php endif;?>
    </tbody>
</table>

I am fetching the articles from my database in table format, but the following error is presented: Undefined variable: articles in dashboard.php page

2 Answers 2

2

change My_Controller to CI_Controller in Admin controller class definition.

When creating a controller class in Codeigniter, you should always extend CI_Controller class. Because $this->load() function is implemented in CI_Controller class. See the documentation

As a best practice create an array and pass it into view() function instead of initializing an array within view() function parameters. ex:

$data['articles'] = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard',$data);
Sign up to request clarification or add additional context in comments.

2 Comments

My_Controller is extends from CI_Controller
@AhilKhan Then you can use only whatever which are inside in the My_Controller class. In order to use load functions from the CI_Controller, you have to create a function inside the My_Controller which returns load function from the CI_Controller. Remember this kind of inheritance isn't a right way to do
1

controller

change these lines

$articles = $this->articlesmodel->articles_list();

$this->load->view('admin/dashboard', ['articles'=> $articles]);

to this

$data['articles'] = $this->articlesmodel->articles_list();

$this->load->view('admin/dashboard',$data);

Tip: try to look at documentation syntax

https://www.codeigniter.com/user_guide/general/views.html

2 Comments

try this $data['articles'] = $this->articlesmodel->articles_list(); echo "<pre>"; print_r ($data); echo "</pre>"; exit(); post the complete error also
@AhilKhan check above answer well explained

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.