TeeJaay
TeeJaay

Reputation: 11

How to send multiple variables to codeigniter's view?

I am trying to the array variable $popular_data in Codeigniter’s view, it is installed with Modesty script.

I tried to append this variable to the one already passed to view by array_merge, $data + $variable

in Model:

public function get_popular_products(){
    $popular_data = $this->db->query(‘SELECT... ’)->result_array();
    return $popular_data;
}

in Controller:

$this->load->model("Popularproduct_model");
$popularproducts = $this->Popularproduct_model->get_popular_products();

var_dump($popularproducts) here(in controller) shows the queried popular products on top of view as

array(3) {
  [0]=>
  array(34) {
    ["id"]=>
    string(1) "3"
    ["title"]=>
    string(13) "Patchouli Oil"
    ["slug"]=>
    string(15) "patchouli-oil-3"
...

other variable passed to view as:

$this->load->view('index', $data);

So how to send the $popularproducts to the view and use the values?

Thanks.

Upvotes: 1

Views: 2856

Answers (5)

curiosity
curiosity

Reputation: 844

store it in $data

$data['popularproducts'] = $this->Popularproduct_model->get_popular_products();

then make foreach inside your view.

foreach($popularproducts as $data){
  echo $data['title'].'<br>'.
       $data['slug'];

}

Upvotes: 2

M.Hemant
M.Hemant

Reputation: 2355

To Pass multiple values from controller to view you should use an array as below given example

Each variable are as examples, you can convert it as per your requirement

Controller:

$data['variable'] =  $variable;//from db or hardcoded

$data['arrayofObject'] =  $arrayofObject;//from db or hardcoded

$data['arrayofArray'] =  $arrayofArray;//from db or hardcoded

$this->load->view('index',$data) // add data array here in 2nd parameter

View:

i.e:
<div>
     <span><?php echo $variable; ?></span>
</div>
<div>
     <ul>
     <?php if(!empty($arrayofObject)) { ?>
          <?php for($arrayofObject as $obj) { ?>
              <li><?php echo $obj->key; ?></li>
          <?php } ?>
     <?php } ?>
     </ul>
</div>
<div>
     <ul>
     <?php if(!empty($arrayofArray)) { ?>
          <?php for($arrayofArray as $ary) { ?>
              <li><?php echo $ary['key']; ?></li>
          <?php } ?>
     <?php } ?>
     </ul>
</div>

Upvotes: 0

Pupil
Pupil

Reputation: 23948

In CodeIgniter, we can send data from Controller to view as an associative array.

The function call $this->load->view() has two parameters:

1) View (template) file (path if its not in the default /application/views folder.

2) The associative data array to be passed.

$data['popularproducts'] = $popularproducts;
$this->load->view('index', $data);

If you pass $data in above scenario, your template index.php has a variable $popularproducts.

$popularproducts in the template/view is same as you passed from Controller.

Upvotes: 0

Gulshan S
Gulshan S

Reputation: 1094

Make an array with each of variables:

$data['variable1'] =  $variable1;

$data['variable2'] =  $variable2;

$data['variable3'] =  $variable3;

$this->load->view('template_name',$data) // add data array here in 2nd parameter

Upvotes: 1

SM Imtiaz Hussain
SM Imtiaz Hussain

Reputation: 447

at first your can add the data in $data['popularproducts'] and then pass it into the view.

$data['popularproducts'] = $popularproducts;

$this->load->view('index', $data);

Upvotes: 1

Related Questions