![](/img/trans.png)
[英]How can I grab values from a database, sum them, and display them in codeigniter?
[英]Grab DB values and display For Each - Codeigniter
讓我的For Each
函數在視圖中顯示數據庫中的所有值時遇到麻煩。 我要去哪里錯了? 不是后端專業人士,我也不寫。 數據庫表非常簡單,一列名為id
。 我想為我顯示每個行。
我有一個自定義的MY_Controller.php
文件,在_construct
有這個文件,它可以在整個站點中全局填充變量。
$this->country_zones = $this->Studios_model->get_studios();
模型:
function get_studios()
{
//this will alphabetize them
$this->db->order_by('id', 'ASC');
$query = $this->db->get('gc_studio_zones');
foreach ($query->result() as $row)
{
$countries = $row->id;
}
return $countries;
}
在我看來:
<?php foreach($this->$country_zones as $country):?>
<li><?php echo $country['countries']->id;?></li>
<?php endforeach;?>
錯誤:
Cannot access empty property...etc
我認為您在循環變量方面有錯字,應該是this->country_zones
:
<?php foreach($this->country_zones as $country):?>
<li><?php echo $country->id;?></a></li>
<?php endforeach;?>
此外,您的Model不返回數組,而僅返回最后一個id。 您需要將值推送到結果數組:
function get_studios()
{
//this will alphabetize them
$this->db->order_by('id', 'ASC');
$query = $this->db->get('gc_studio_zones');
$countries = array();
foreach ($query->result() as $row) {
$countries[] = $row;
}
return $countries;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.