我真的需要帮助。我不知道如何解决未定义的变量:图表
错误
遇到PHP错误
严重性:注意
消息:未定义的变量:chart
文件名:shop / header.php
行号:242
我的控制器
function mychart($id_user)
{
$where = array('id_user' => $this->session->userdata('id_user'));
$datachart['chart'] = $this->m_product->tampil_chart($where,'chart')->result();
$this->load->view('shop/header',$datachart);
}
我的模特
function tampil_chart($where,$table)
{
$datachart = $this->db->get_where($table,$where);
return $datachart->result();
}
查看(店/报头)
<div class="header-cart-content flex-w js-pscroll">
<?php foreach($chart as $a){
?>
<ul class="header-cart-wrapitem w-full">
<li class="header-cart-item flex-w flex-t m-b-12">
<div class="header-cart-item-img">
<img src="<?php echo base_url() ?>assets/user/images/item-cart-01.jpg" alt="IMG">
</div>
<div class="header-cart-item-txt p-t-8">
<a href="#" class="header-cart-item-name m-b-18 hov-cl1 trans-04">
<?php echo $a->id_product ?>
</a>
<span class="header-cart-item-info">
<?php echo $a->jumlah ?>
</span>
</div>
</li>
</ul> <?php } ?>
答案 0 :(得分:0)
你必须检查&#34;图表&#34;在迭代之前,在视图文件中定义变量。
对于这个例子,你必须这样做241 line of shop / header.php:
<div class="header-cart-content flex-w js-pscroll">
<?php
if (isset($chart)):
foreach($chart as $a){?>
<ul class="header-cart-wrapitem w-full">
<li class="header-cart-item flex-w flex-t m-b-12">
<div class="header-cart-item-img">
<img src="<?php echo base_url() ?>assets/user/images/item-cart-01.jpg" alt="IMG">
</div>
<div class="header-cart-item-txt p-t-8">
<a href="#" class="header-cart-item-name m-b-18 hov-cl1 trans-04">
<?php echo $a->id_product ?>
</a>
<span class="header-cart-item-info">
<?php echo $a->jumlah ?>
</span>
</div>
</li>
</ul> <?php } endif; ?>
答案 1 :(得分:0)
从->result()
控制器移除mychart
,因为您已经拥有模型tampil_chart
;
替换它:
$datachart['chart'] = $this->m_product->tampil_chart($where,'chart')->result();
:
$datachart['chart'] = $this->m_product->tampil_chart($where,'chart');
你的控制器应该是这样的:
function mychart($id_user)
{
$where = array('id_user' => $this->session->userdata('id_user'));
$datachart['chart'] = $this->m_product->tampil_chart($where,'chart');
$this->load->view('shop/header',$datachart);
}