Nova Design
Nova Design

Reputation: 106

Codeigniter View, how to pass two variables

My question is a little bit unprofessional but I'm a newbie in CodeIgniter looked so far in documentation and didn't found an answer.

Let say I have 2 table one for products and the other for special offers.

I want to view both table records in one page and the same view file.

I used to do it with smarty by assigning each array and loop through it in the template.

But in CodeIgniter

if I used the built-in template engine it's just like this

$this->parser->parse('template', $data);

And without template engine is like this

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

In both cases, it is only passing one variable to the view file.

How to pass multiple variables.

thank you.

Upvotes: 0

Views: 3155

Answers (2)

pah
pah

Reputation: 4778

Take a look into this.

Quoting:

$data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );

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

In the view side, you'll be able to access the following variables:

echo($title);            // 'My Title'
echo($heading);          // 'My Heading'
echo($message);          // 'My Message'

Hope this gives you some clues for further research.

Edit #1 (reply to comment):

$array1 = array('a', 'b', 'c');
$array2 = array('x', 'y', 'z');

$data['view_array1'] = $array1;
$data['view_array2'] = $array2;

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

This will give you the following variables in the view:

$view_array1;    //  array('a', 'b', 'c')
$view_array2;    //  array('x', 'y', 'z')

Upvotes: 4

Moyed Ansari
Moyed Ansari

Reputation: 8461

In Controller you can make associative array like this

$data['Product'] = $product_array;
$data['Special_Offer'] = $special_offer_array;

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

In view your variables would be $Product and $Special_Offer

Upvotes: 3

Related Questions