1

我是 codeIgniter 的新手,我的数据库和下拉菜单有点问题。

这是我获取所需信息的功能...

protected $tbl_name = 'club';

public function get($id = 0, $object = TRUE)
    {
        // select * from users where id = 0
        // check if id is provided
        if($id) {
            // id provided - retrieve a specific user
            $query = $this->db->get_where($this->tbl_name, array('id' => $id));
            // check if object type is requested
            if($object) {
                // object is requested
                $data = $query->row();
            }
            else {
                // array is requested
                $data = $query->row_array();
            }
        }
        else {
            // id not provided - retrieve all users
            $query = $this->db->get($this->tbl_name);
            // check if object type is requested
            if($object) {
                // object is requested
                $data = $query->result();
            }
            else {
                // array is requested
                $data = $query->result_array();
            }
        }
        return $data;
    }

这是我在控制器中调用它的地方

$data['clubname'] = $this->club_model->get();

这是我认为的下拉菜单

<tr><td><?php echo form_label('Club Name: ', 'clubname'); ?></td><td><?php echo form_dropdown('clubname', $clubname['name']); ?></td><td><?php echo form_error('clubname'); ?></td></tr>

但我收到这些错误

A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: individual/individual_club.php

Line Number: 7
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

我究竟做错了什么?

4

1 回答 1

1

问题在于你的form_dropdown('clubname', $clubname['name'])电话。第二个参数错误。 form_dropdown需要一个数组。 见文档

根据您的查询结果,您需要构建一个俱乐部数组。类似的东西:

// array is requested
$data = array();
foreach ($query->result_array() as $row)
{
   $data[$row['club_id']] = $row['club_name'];
}

club_id和替换club_name为您的表格的列名称作为俱乐部的名称和 ID。之后,将您的更改form_dropdownform_dropdown('clubname', $clubname).

像这样,$clubname就是一排俱乐部。

希望这可以帮助!

于 2012-11-05T17:52:38.863 回答