1

让我的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
4

1 回答 1

1

我认为您对循环变量的看法有误,应该是this->country_zones

<?php foreach($this->country_zones as $country):?>
    <li><?php echo $country->id;?></a></li>
<?php endforeach;?>

此外,您的模型不返回数组,而只返回最后一个 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;
}
于 2013-09-30T20:40:44.027 回答