down100009 2016-02-23 04:24
浏览 57

我不能在Codeigniter控制器中放入超过2个查询模型的结果

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:

enter image description here

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:

enter image description here

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
  • 写回答

2条回答 默认 最新

  • donglipi4495 2016-02-23 05:08
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号