开发者

Is it possible to return multiple variables/arrays in Codeigniter?

开发者 https://www.devze.com 2022-12-26 01:01 出处:网络
Let\'s say in my model, I have a function that queries two separate tables. I need to pass the results back to my controller to display in my view.

Let's say in my model, I have a function that queries two separate tables. I need to pass the results back to my controller to display in my view.

I'm using MongoDB but it should be the same for any other DB. Normally this would work.

$files = $grid->find(array("username" => $profile_id, 
                     "thumbnail" => array('$ne' => true)) );

$return files;

But, I need to go a step further and check to see if the user has a default photo selected.

$getCount = $grid->count(array("username" => $profile_id, 
            开发者_开发技巧         "default" => array('$ne' => true)) );

If I recall correctly, I would normally do ...

$return array($files, $getCount);

Doesn't work though.


Figured it out ... in my function, I did the following.

$files['data'] = $grid->find(array("username" => $profile_id, 
                 "thumbnail" => array('$ne' => true)) );

$files['count'] = $grid->count(array("username" => $profile_id, 
                  "default" => array('$ne' => true)) );

$return files;

In my view, I would manipulate my data accordingly.

echo $files['count'];

foreach( $files['data'] as $obj) {
    ...
}


To do it the way you wanted to in your original post it'd be something like:

$return array('files' => $files, 'getCount' => $getCount);


I'd split it into two separate model functions. I think its cleaner than fetching and returning the two separate pieces of data from the one function.

0

精彩评论

暂无评论...
验证码 换一张
取 消