1

I want to pass an array to a view from a controller. I tried to use the code below. I know it is wrong, but cannot think of what to use. find() function gets all rows from table, and then i want to pass those rows as array to the view. How can i do so?

<?php
    class Blog extends CI_Controller{

        public function __construct(){
            parent::__construct();
            $this->load->model('blog_model');
        }

        public function index(){

             $data = $this->blog_model->find(); //which gets all entries from table
            $this->load->view('template/header');
            $this->load->view('template/content', $data);
            $this->load->view('template/footer');

        }

        public function create(){
            $this->blog_model->create();


        }

        public function delete(){


        }
    }
    ?>

1 Answer 1

1
$data = $this->blog_model->find(); //which gets all entries from table
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');

Should be:

$data = array('myvar' => $this->blog_model->find());
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');

Then access it in your view with:

$myvar

Please see this for a thorough explanation.

Sign up to request clarification or add additional context in comments.

Comments

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.