开发者

Returning two separate query results within a model function

My model has a function 开发者_JS百科that should run 2 different queries.

Ideally I would like it to return 2 different vars (1 from each query) that would be available to my controller.

The code below obviously doesn't work b/c the first return ends execution right there. But how could I do something that would generate these two results within the same function?

Thanks for any help / pointer you can provide.

MODEL -- Hypothetical code

function ABC() 
{
    $query1 = $this->db->query( ... MySQL code #1 here ... );

    $data1 = array();
    foreach (query1->result() as $row){
        $data1[$row->k] = $row->value;

    return $data1;    

    $query2 = $this->db->query( ... MySQL code #2 here ... );

    $data2 = array();
    foreach (query2->result() as $row){
        $data2[$row->k] = $row->value;

    return $data2;
}


function ABC() 
{
    $query1 = $this->db->query( ... MySQL code #1 here ... );

    $data = array();
    foreach (query1->result() as $row){
        $data['query1'][$row->k] = $row->value;


    $query2 = $this->db->query( ... MySQL code #2 here ... );

    $data2 = array();
    foreach (query2->result() as $row){
        $data['query2'][$row->k] = $row->value;

    return $data;
}

also see danip's answer


Got anything against return array($data1, $data2); ?

Then you could use list($foo, $bar) = $baz->ABC(); in your controller of you want to.


You can do this like fazo suggested but your class/method design seems to have a problem. You would be better of refactoring this code and return the results in two different functions.


Use the following Code Tested And Works fine

function Submit() 
{
    $query1 = $this->db->query( INSERT INTO... MySQL code #1 here ... );

    $query2 = $this->db->query( INSERT INTO... MySQL code #2 here ... );

    return array( $query1, $query2);
}

or

function Submit() 
{
    $query1 = $this->db->query( INSERT INTO... MySQL code #1 here ... );

    $query2 = $this->db->query( INSERT INTO... MySQL code #2 here ... );

    return $query1;
    return $query2;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜