views:

61

answers:

2

I am using codeIgniter and I am trying to pass an array of data. I have written like this:

$data['username']="Dumbo";

I also wrote this:

$data['shouts']=$this->Musers->getShout();  // retrieve data from table

Then I write:

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

In view page, I wrote:

 <?php echo $username;
    foreach ($shouts as $shout)
        {
        echo $shout->shout;
        echo '<br>';
        echo $shout->timeStamp;          
        }
  ?> 

Problem is that while the view did retrieve data from table and display results in view page, an error came up for $data['username'] saying, "Undefined variable: username"

Why is that? The $data['username'] is already defined! Or what did I do wrong?

+1  A: 
<?php echo $data['username']; ?>

If you wrote this, the error will occur.

Correct way is to write like

<?php echo $username; ?>

'username' is the index in the data array, which is passed to the view using the load method

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

If you need to pass an array...

$data['usernames'] = $username_array;
$this->load->view("welcome_message", $data);

Then in the view,

<?php print_r($usernames); ?>
ShiVik
Actually, that's what I did. I updated. Still saying it's undefined.
netrox
A: 

In your view, do this..

$data = array(); 
$data['username'] = "something";
$data['shouts']=$this->Musers->getShout();
Manie
That's what I did. It was undefined.
netrox
Note: don't forget the $data = array();
Manie