【发布时间】:2016-02-23 04:24:35
【问题描述】:
好的,伙计们.. 我的问题是我想在引导 HTML 表中的模型上创建一个循环。循环的数据在 HTML 表格中应该如下所示:
这是我的模型:
<?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();
}
}
这不是实际的查询,但它看起来就像我的查询,当我在我的 SQL Server 上测试它时它运行良好。所以,我的查询没有问题。接下来我将查询结果传递给我的控制器,如下所示:
<?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);
}
}
然后我像这样将 $data 传递到我的 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>
之后,我只能循环“名称”和“购买的物品”数据。其他任何事情都只是一个完全错误。这是我得到的错误:
商品已售出错误:
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
交易成功错误:
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
交易失败错误
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
我从某人那里得到了一个提示,错误是因为我未能在我的控制器中调用其他函数的 $value。
我尝试删除控制器上的 $resultitemsold、$resulttranssuccess 和 $resulttransfail,只留下名称和购买的项目,它工作正常。
这是否意味着 Codeigniter 不允许我将超过 2 个值放入索引函数的视图中?我不知道这个,请帮助我,伙计们......
感谢您的帮助...
。 .
.
更新
感谢您的所有回答,但我仍然收到错误消息。让我展示一下错误的样子:
所以,如您所见。它实际上循环了,但为什么我仍然得到错误。
当“itemsold”的值循环时,它会循环数组结果,但“namecust”和“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
【问题讨论】:
-
模型中无需使用get实例
-
@wolfgang1983 你是什么意思?我不必使用 $query->result() ???
-
签到
var_dump($data);在你的视野中 -
@naseebac 如何在我看来?
-
<?php var_dump($data);?>
标签: php codeigniter model-view-controller