Welcome Guest, Not a member yet? Register   Sign In
Passing more than one array to View
#1

[eluser]wizzer[/eluser]
Model
Code:
function _getAllUsers()
        {
            $query = $this->db->get('user');
            $userList = array(array());
            $x = 0;
            foreach ($query->result_array() as $row)
            {
                    
                 $userList[$x]['RegDate'] = $row['RegDate'];
                 $userList[$x]['NoOfDocs'] = $row['NoOfDocs'];
                 $userList[$x]['AccessLevel'] = $row['AccessLevel'];
                 $x++;
            }
            return $userList;
        }


Controller
Code:
function index()
    {
        $this->load->model('user');
        $stats = $this->user->_getStats();
        $userList = $this->user->_getAllUsers();
        $this->load->view('manageuser_view',$userList);
    }

As I understand it, in order for me to pass more than one array to the view, I have to join them together? I have a $stats array that needs to get passed too which is not shown here.

My main question is, how do i display this in view, nothing seems to work.

Once it does display, what is the best way to pass another array into this view?
#2

[eluser]sl3dg3hamm3r[/eluser]
Like this:
Code:
$data = array(
                 'userList' => $this->user->_getAllUsers(),
                 'anotherArray' => array(1,2,3),
                 'someMoreData' => 'Hello world',
              );
$this->load->view('manageuser_view', $data);

You can now access your data in the view like this:
Code:
echo "User-count: " & count($userList) & "<br />";
echo $someMoreData;  //etc.

P.s.: Nice sample application, where you could learn from: bambooinvoice
#3

[eluser]LuckyFella73[/eluser]
Hi Wizzer,

what you can do is:
Code:
&lt;?php
function index()
{
    $this->load->model('user');
    $stats = $this->user->_getStats();
    $userList = $this->user->_getAllUsers();
    
    $data['stats'] = $stats;
    $data['userList'] = $userList;
    
    $this->load->view('manageuser_view',$data);
}
// or shorter:
function index()
{
    $this->load->model('user');
    $data['stats'] = $this->user->_getStats();
    $data['userList'] = $this->user->_getAllUsers();
        
    $this->load->view('manageuser_view',$data);
}
?&gt;
#4

[eluser]wizzer[/eluser]
many thanks both of you :-)
#5

[eluser]darkhouse[/eluser]
LuckyFella73, the only issue with your solution is that you didn't declare $data before you started setting its values. Most servers won't squawk about it, but it will technically throw a notice. Easily fixed by just adding a $data = array(); beforehand. However, I prefer sl3dg3hamm3r's method as it keeps it all in one step really.
#6

[eluser]LuckyFella73[/eluser]
Hi Darkhouse,

I found so many code examples in the CI user guide where $data
was not declared - that's why I thought $data is declared as
an array by CI automaticly.

Thanks for the hint.
#7

[eluser]wiredesignz[/eluser]
[quote author="darkhouse" date="1249098573"]... you didn't declare $data before you started setting its values. Most servers won't squawk about it, but it will technically throw a notice. Easily fixed by just adding a $data = array(); beforehand...[/quote]

That is not actually correct, PHP being loosely typed does not require that you declare a data type for a variable before using it.

If it were true then the CI config and database settings arrays should trigger errors.
#8

[eluser]darkhouse[/eluser]
You're right, it's not a requirement, but some servers will throw an error (unless you've turned errors off) if you don't declare it first. I guess loose programming is more of a pet peeve of mine than anything.




Theme © iAndrew 2016 - Forum software by © MyBB