简体   繁体   中英

I can't put more than 2 query model result in Codeigniter controller

OK guys.. My problem is I want to create a loop on my model in a bootstrap HTML table. The looped data should look like this in HTML table:

在此处输入图片说明

Here's my model:

<?php class Dash_model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                // Loading second db and running query.
            $CI = &get_instance();
            //setting the second parameter to TRUE (Boolean) the function will return the database object.
            $this->db2 = $CI->load->database('db2', TRUE);
        }

        public function itemboughttotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as itembought
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE rsp_code = '11'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result(); 

        }

        public function itemsoldtotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as itemsold
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE rsp_code = '22'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result(); 
        }

        public function transsuccesstotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as TransSuccess
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE trans_code = '100'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result(); 
        }

        public function transfailedtotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as TransFail
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE trans_code = '200'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result(); 
        }

}

It's not the actual query, but it looks just like my query and it works perfectly when I test it on my SQL Server. So, there's nothing wrong with my query. Next I pass the query result into my controller like this:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Dash_control extends CI_Controller {

    public function __construct()
       {
            parent::__construct();
            $this->load->model('dash_model');
            $this->load->library('table');
       }

    public function index()
    {
        $tmpl = array (
        'row_start'           => '<tr>',
        'row_end'             => '</tr>',
        'cell_start'          => '<td>',
        'cell_end'            => '</td>',
        );

        $this->table->set_template($tmpl); 

        $data['resultitembought'] = $this->dash_model->itemboughttotal();
        $data['resultitemsold'] = $this->dash_model->itemsoldtotal();
        $data['resulttranssuccess'] = $this->dash_model->transsuccesstotal();
        $data['resulttransfailed'] = $this->dash_model->transfailedtotal();

        $this->load->view('dashboard', $data);
    }


}

And then I pass th $data into my VIEW like this:

<tbody>
<?php foreach (array_merge($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed) as $row)

{ ;
?>

<tr>

<td><?php echo $row->Name; ?></td>
<td><?php echo $row->itembought; ?></td>
<td><?php echo $row->itemsold; ?></td>
<td><?php echo $row->TransSuccess; ?></td>
<td><?php echo $row->TransFail; ?></td>

</tr>
<?php } ?>
</tbody>

After that, I can only loop the 'Name' and the 'Item Bought' data. Anything else is just a complete error. And here's the error I got:

ERROR ON ITEM SOLD:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$itemsold

Filename: views/dashboard.php

Line Number: 302

Backtrace:

File: C:\xampp\htdocs\application\views\dashboard.php
Line: 302
Function: _error_handler

File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view

File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once

ERROR ON TRANSACTION SUCCESS:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$TransSuccess

Filename: views/dashboard.php

Line Number: 303

Backtrace:

File: C:\xampp\htdocs\application\views\dashboard.php
Line: 303
Function: _error_handler

File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view

File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once

ERROR ON TRANSACTION FAILURE

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$TransFail

Filename: views/dashboard.php

Line Number: 304

Backtrace:

File: C:\xampp\htdocs\application\views\dashboard.php
Line: 304
Function: _error_handler

File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view

File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once

I got a tip from someone that the error was happened because I failed to call the $value of other function in my controller.

I tried to remove the $resultitemsold, $resulttranssuccess, and $resulttransfail on my controller, leaving only the name and the itembought, and it works just fine.

Does this means that Codeigniter won't allow me to put more than 2 value to put in the View on the index function? I don't know about this, please help me, guys...

Thank you for your help...

. .

.


UPDATE

Thank for all of your answers, but I still got the error. Let me show what the error look like:

在此处输入图片说明

So, as you can see. It actually looped, but why I still got the error.

When the value of the 'itemsold' looped, it looped the array result, but error on the 'namecust' and the 'itembought'.

A PHP Error was encountered

Severity: Notice

Message: Undefined index: itemsold

Filename: views/dashboard.php

Line Number: 302

Backtrace:

File: C:\xampp\htdocs\application\views\dashboard.php
Line: 302
Function: _error_handler

File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 30
Function: view

File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once

I would have just make a comment but I don't have enough reputation to do so, so I decided place this here.

The error messages must have showed up because you are trying to echo an array property that doesn't have a value assigned to it after your array_merge() . You should make sure that after the array_merge() , all properties of each array in the resulting multidimensional array have values assigned to them. You can do so by printing the merge result and check out the associative arrays for each key/value pairs.

I would have done this just to check:

$result = array_merge($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed);
print_r($result);

You can also checkout php's manual on array_merge function to have a better understanding of how it is used.

From the result you've displayed, using array_merge() , actually appends each subsequent array to the first array, which is not what you want to achieve. Use array_merge_recursive() to make all values of each arrays with the same key to be merged into an array. I expect this to work as long as all the arrays you are attempting to merge have common keys. You can print the result to check out the resulting array. Like this:

$result = array_merge_recursive($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed);
print_r($result);

Try using result_array() in model.

<?php 

 class Dash_model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                // Loading second db and running query.
            $CI = &get_instance();

            //setting the second parameter to TRUE (Boolean) the function will return the database object.

            $this->db2 = $CI->load->database('db2', TRUE);
        }

        public function itemboughttotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as itembought
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE rsp_code = '11'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result_array(); 

        }

        public function itemsoldtotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as itemsold
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE rsp_code = '22'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result_array(); 
        }

        public function transsuccesstotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as TransSuccess
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE trans_code = '100'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result_array(); 
        }

        public function transfailedtotal()
        {
            $query = $this->db->query("
            SELECT namecust as Name, COUNT(namecust) as TransFail
            FROM db1..Table_1
            INNER JOIN db2..Table_2
            ON db2..Table_2.ID = db1.Table_1.CustID
            INNER JOIN db2..Table_3
            ON Table_3.TransID = Table_2.TransID
            WHERE trans_code = '200'
            GROUP BY namecust
            ORDER BY namecust
            ");

            return $query->result_array(); 
        }

}

And on View

<tbody>
<?php foreach (array_merge($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed) as $row)

{ 
?>

<tr>

<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['itembought']; ?></td>
<td><?php echo $row['itemsold']; ?></td>
<td><?php echo $row['TransSuccess']; ?></td>
<td><?php echo $row['TransFail']; ?></td>

</tr>
<?php } ?>

</tbody>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM