Welcome Guest, Not a member yet? Register   Sign In
return multiple values from a function
#1

[eluser]versatilia[/eluser]
Ok my problem is this i have a function in a model that gets some results from the DB and i return them to the controller by way of return $query-result_array();

I want to run a second query in the same function however that will do;

$resultCount = $this->db->query("SELECT FOUND_ROWS() as total ");


and i want to return that result to the calling controller as well as the results of the first query (the first query has a SQL_CALC_FOUND_ROWS in it)

Is this possible and if so then how?

cheers in advance.
#2

[eluser]narkaT[/eluser]
you could do something like this:

Code:
function foobar() {
    //.....
    return array('result' => $result, 'count' => $resultCount);
}
#3

[eluser]versatilia[/eluser]
will that work eventho $result is a result_array?
#4

[eluser]simshaun[/eluser]
Yes. PHP will simply return a multi-dimensional array.
#5

[eluser]versatilia[/eluser]
sorry for being thick but can you give an example of how id access that multidimentional array once its sent back to the controller?
#6

[eluser]narkaT[/eluser]
for example:
Code:
$ret = foobar();

echo $ret['result'][0]['name'];

foreach($ret['result'] as $row) {
    echo $row['name'];
}

you can use $ret['result'] like you would use $result.
#7

[eluser]m4rw3r[/eluser]
list() can also be used
Code:
function a()
{
    return array('a', 'b');
}

list($foo, $bar) = a();

echo $foo; // echos 'a'
echo $bar; // echos 'b'
#8

[eluser]sophistry[/eluser]
be aware that in constructing models you are building a mini API for your application to access your db tables.

with that in mind, i would caution you against returning an array with the FOUND_ROWS() count and the LIMITed array stuffed together - it's just not a good design IMHO.

better overall API-minded approach would be to:

create another model function that can run FOUND_ROWS() and return the count. that keeps the FOUND_ROWS() call decoupled and available to other model functions should you need to use it down the road.

just have to make sure that you access it in the next line $this->themodel->get_found_rows();

cheers.
#9

[eluser]versatilia[/eluser]
Works perfectly cheers.

Code:
return array('result' => $query->result_array(), 'total' => $resultCount->result_array());

Then in the controller

Code:
$ret = $this->tracks->atoztitlelist($letter,$displaynumber,$start);
$data['mysqlResult'] = $ret['result'];

Super, Smashing, Great.
#10

[eluser]versatilia[/eluser]
[quote author="sophistry" date="1227575933"]better overall API-minded approach would be to:

create another model function that can run FOUND_ROWS() and return the count. that keeps the FOUND_ROWS() call decoupled and available to other model functions should you need to use it down the road.

just have to make sure that you access it in the next line $this->themodel->get_found_rows();

cheers.[/quote]


That was exactly my original plan, however for some reason i could never make FOUND_ROWS() return the correct number. It would only ever return 1. However if i called it from with in the function in the model then it returned the correct number.




Theme © iAndrew 2016 - Forum software by © MyBB