0

我正在尝试使用 codeigniter 表单助手的表单下拉功能。

echo form_dropdown('userCharacters', $userRoster, '', '', 'id="userCharacter"');

如果您注意到这$userRoster是我从控制器传递到视图的数组。

这是我在数组上执行 print_r 时的显示方式。

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [rosterName] => Kid Wonder
    )

[1] => stdClass Object
    (
        [id] => 3
        [rosterName] => Oriel
    )

)

但是我收到这些错误,不知道为什么

A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: helpers/form_helper.php

Line Number: 352
A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: helpers/form_helper.php

Line Number: 352

编辑 :

Array
(
[0] => Array
    (
        [id] => 1
        [rosterName] => Kid Wonder
    )

[1] => Array
    (
        [id] => 3
        [rosterName] => Oriel
    )

)

编辑2: 应该发生的是用户登录后它具有默认字符ID和用户数据数组中保存的用户角色ID。它运行库函数 getRosterList。在该函数内部,它检查用户的角色 ID 是否为 4(管理员)或 5(超级管理员),如果是,那么我想要它做的是获取所有名册成员,其中包括他们的默认角色并拥有它作为选定的选项。如果他们不是这两个角色之一,那么我只希望它获取他们控制的名册成员并将预选选项作为默认角色 ID。如果他们只有一个字符,那么它会显示一个 h1 标签而不是下拉菜单。

控制器:

$this->data['userData'] =   $this->users->getUserByUserID($this->session->userdata('userID'));
$this->data['userRoster'] = $this->kowauth->getRosterList($this->data['userData']->usersRolesID);

图书馆 (kowauth)

 * Get roster list
 *
 * @param   integer
 * @return  object/NULL
 */
function getRosterList($usersRolesID)
{
    // Check args
    if(!is_numeric($usersRolesID)) { throw new Exception('Non-numeric $usersRolesID provided to getRosterList()'); }

    if (($usersRolesID == 4) || ($usersRolesID == 5))
    {
        return $this->ci->users->getAllRoster();
    } 
    else
    {
        return $this->ci->users->getRosterByUserID($this->ci->session->userdata('userID'));
    }
}

模型:

/**
 * Get roster list
 *
 * @return  object/NULL
 */
function getAllRoster()
{       
    $this->db->select('id');
    $this->db->select('rosterName');
    $this->db->select('rosterStatusID');
    $this->db->from('rosterList');
    $this->db->order_by('rosterName');
    $query = $this->db->get();
    if ($query->num_rows() > 0) 
    {
        return $query->result();
    }
    return null;
}

/**
 * Get list of roster by user ID
 *
 * @return  object/NULL
 */
function getRosterByUserID($userID)
{
    // Check args
    if (!is_numeric($userID)) { throw new Exception('Non-numeric $userID provided to getRosterByUserID()'); }

    $this->db->select('id');
    $this->db->select('rosterName');
    $this->db->from('rosterList');
    $this->db->where('userID', $userID);
    $this->db->order_by('rosterName');
    $query = $this->db->get();
    if ($query->num_rows() > 0) 
    {
        return $query->result_array();
    }
    return null;
}

看法:

<?php 
        echo '<pre>';
        print_r($userRoster);
        echo '</pre>';
        if (count($userRoster) == 1)
        {
            echo '<h1>'.$userRoster->rosterName.'</h1>';
        }
        else 
        {
            $options = array (
                $userRoster['id'] => $userRoster->rosterName
            );
            echo form_dropdown('userCharacters', $options, '', 'id="userCharacter"');
        }
        ?>

有人对此有任何想法吗?

4

1 回答 1

3

您当前正在传递一个对象数组。我相信你的 $userRoster 数组应该像这样格式化:

Array
(
     1 => 'Kid Wonder'
     3 => 'Oriel'
)

另外,我相信 form_dropdown 只需要四个参数,而您正在尝试传递五个参数。您可能希望将最后一个参数移到第四个位置:

echo form_dropdown('userCharacters', $userRoster, '', 'id="userCharacter"');

应该产生:

<select name="userCharacters" id="userCharacter">
<option value="1">Kid Wonder</option>
<option value="3">Oriel</option>
</select>

认为这就是你想要的!

http://codeigniter.com/user_guide/helpers/form_helper.html

于 2012-04-06T18:54:18.083 回答