CodeIgniter的多级列表

时间:2011-11-17 19:17:36

标签: php list codeigniter

我正在使用CodeIgniter和PHP。我正在尝试做一个简单的任务,即显示一个多级菜单。假设我有一个学生和状态表。我想表明哪些学生处于哪种状态(大四,大三等)。但是我收到了这个错误:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/sview.php

Line Number: 31

在第31行,我有

<?php echo $status->statusname;?>

控制器:

// ....
$data['status'] = $this->status_model->get_students();
$this->load->view('sview', $data);

模特:

function get_students(){

    $s = $this->db->get('status');

    foreach ($s->result() as $status){

        $students = $this->db->get_where('student', array('status_id'=>$status->id));
        $status->students = students->result();
    }

    return $s;
}

视图:

<?php foreach($s as $status):?>
    <h4><?php echo $status->statusname;?></h4>
    <?php foreach($status->student as $student):?>
        <?php echo $student->studentname; ?>
    <?php endforeach;?>
<?php endforeach;?>

2 个答案:

答案 0 :(得分:0)

您的控制器正在设置$data['status'],具有所有不同的状态。尝试在视图中更改以下内容:

<?php foreach($status as $s):?>
    <h4><?php echo $s->statusname;?></h4>
    <?php foreach($s->student as $student):?>
        <?php echo $student->studentname; ?>
    <?php endforeach;?>
<?php endforeach;?>

答案 1 :(得分:0)

您没有在$s的循环中修改get_students(),因此它只返回 status 表中所有记录的原始查询。您在循环中的$students变量上也错过了“$”。

试试你的模特:

function get_students()
{
    $s = $this->db->get('status');

    $statuses = array();

    foreach ($s->result() as $status)
    {
        $students = $this->db->get_where('student', array('status_id'=>$status->id));
        $status->students = $students->result();

        $statuses[] = $status;
    }

    return $statuses;
}

这将返回$status数组中已修改的$statuses变量,然后您可以在视图中访问它们。