好的伙计..我的问题是我想在我的模型中在bootstrap 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>
之后,我只能循环播放名称&#39;并且&#39;项目购买&#39;数据。其他任何事情都只是一个完整的错误。这是我得到的错误:
出售物品的错误:
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
我从某人那里得到了一个错误,因为我没有在我的控制器中调用其他函数的$值。
我试图在我的控制器上删除$ resultitemsold,$ resulttranssuccess和$ resulttransfail,只留下名称和itembought,它运行正常。
这是否意味着Codeigniter不允许我在索引函数的View中输入2个以上的值?我不知道这件事,请帮助我,伙计......
感谢您的帮助......
。
更新
感谢您的所有答案,但我仍然收到错误消息。让我说明错误的样子:
所以,正如你所看到的那样。它实际上是循环的,但为什么我仍然得到错误。
当物品的价值为&#39;循环,它循环数组结果,但错误的&#39; namecust&#39;并且“反复”#39;
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
答案 0 :(得分:1)
我会发表评论,但我没有足够的声誉,所以我决定把它放在这里。
错误消息必须已显示,因为您尝试echo
在array_merge()
之后没有为其分配值的数组属性。您应确保在array_merge()
之后,生成的多维数组中每个数组的所有属性都分配了值。您可以通过打印合并结果并检查每个键/值对的关联数组来完成此操作。
我会这样做只是为了检查:
$result = array_merge($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed);
print_r($result);
您还可以查看array_merge function上的php手册,以便更好地了解它的使用方法。
从您显示的结果中,使用array_merge()
实际上将每个后续数组附加到第一个数组,这不是您想要实现的。使用array_merge_recursive()
使每个数组的所有值都使用相同的键合并到一个数组中。只要您尝试合并的所有阵列都具有公共密钥,我希望它能够正常工作。
您可以打印结果以检查生成的数组。像这样:
$result = array_merge_recursive($resultitembought, $resultitemsold, $resulttranssuccess, $resulttransfailed);
print_r($result);
答案 1 :(得分:0)
尝试在模型中使用result_array()。
<?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();
}
}
在视图
上<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>