Fuffy
Fuffy

Reputation: 835

How pass parameters between controller and view Codeigniter

My parameter is an array:

Controller:

$data=......;
$this->load->view('a/p/l',$data);

The data vector has like parameter:

  0 => 
    array (size=4)
 'email' => string '' (length=21)
      ...
  1 => 
    array (size=4)
 'email' => string '' (length=21)
     ...
  2 => 
    array (size=4)
      'email' => string '' (length=21)

Anyone can show mem some View that I can read the elements in to the array?

Upvotes: 1

Views: 327

Answers (2)

Oli Soproni B.
Oli Soproni B.

Reputation: 2800

Here is a simple example

Here is my controller named welcome.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {

        $data['default'] = array(
            array(
                'email' => '[email protected]',
                'username' => 'username1'
            ),

            array(
                'email' => '[email protected]',
                'username' => 'username2'
            ),

            array(
                'email' => '[email protected]',
                'username' => 'username3'
            )
        );

        $data['title'] = 'Sample';

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

}

In order to call the pass $data array in the views, we make sure that we have a reference key for the array like

$data['default'] = array

$data['title'] = 'Sample';

In order for me to access those data in my view here is a sample view named

welcome_message.php

<html lang="en">
    <head>
        
    </head>
    <body>
        <div id="container">
            <?php
                foreach ($default as $key => $value) {
            ?>
            <h1><?php echo $value['email'];?></h1>
            <?php
                }
            ?>
            
            <h6><?php echo $title;?></h6>
            
        </div>
    </body>
</html>

To be able to access those data pass, I used the reference key of the pass array

default and title

and from there I can do the processing already

hope It could help you out.

Upvotes: 2

vsogrimen
vsogrimen

Reputation: 456

Hi the data array's index must be an associative index it must be letters first. CI convert the array as variable in view.

Example:

$data=array('value1'=>12,'value2'=>23)
$this->load->view('a/p/l',$data);

you can now access the values of the passed array by treating the indexes as new variable.

in your view you can get the value of value1 index like this

echo $value1;

I think it won't work if you use a number as index, it is a basic php rules in variables.

Upvotes: 0

Related Questions