表格下拉功能

时间:2012-04-06 18:48:22

标签: php codeigniter

我正在尝试使用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和userData数组中保存的用户的角色id。它运行库函数getRosterList。在该函数内部,它检查用户是否具有角色id为4(admin)或5(superadmin),如果它们是我想要它做的是获取所有包含其默认角色的名册成员并拥有它作为选定的选项。如果他们不是这两个角色中的一个,那么我只想让它获得他们控制的名册成员并将预先选择的选项作为默认角色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"');
        }
        ?>

有人对此有任何想法吗?

1 个答案:

答案 0 :(得分: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