【问题标题】:code igniter form_dropdown() ordercodeigniter form_dropdown() 顺序
【发布时间】:2014-08-13 03:53:40
【问题描述】:

我正在尝试按字母顺序排列下拉项目,但我无法这样做。我一定遗漏了一些明显的东西..

我假设ORDER BY type_name 会按字母顺序创建数组

$data['training_types'] = $this->db->query("SELECT * FROM training_types ORDER BY type_name")->result_array();

print_r($training_types);
foreach ($training_types as $type)
{
    $options[$type['id']] = $type['type_name'];
    echo $options[$type['id']]; //test only: this displays the options in alphabetical order just fine
}
print_r($options);
echo form_dropdown('training_type',$options,'0');
//for some reason when the dropdown is created, the order is not alphabetical, it's not even ordered by id... I have no idea what is ordering it this way.

第一个 print_r 返回:

Array ( [0] => Array ( [id] => 6 [type_name] => Independent Study ) [1] => Array ( [id] => 1 [type_name] => Instructor Lead ) [2] => Array ( [id] => 3 [type_name] => Instructor Lead/Virtual ) [3] => Array ( [id] => 7 [type_name] => Job Aid ) [4] => Array ( [id] => 5 [type_name] => Mentoring ) [5] => Array ( [id] => 2 [type_name] => Virtual ) [6] => Array ( [id] => 4 [type_name] => Web ) ) 

第二次 print_r 返回:

Array ( [2] => Virtual [3] => Instructor Lead/Virtual [4] => Web [1] => Instructor Lead [5] => Mentoring [6] => Independent Study [7] => Job Aid )

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您可以在foreach 循环之前print_r($data['training_types']) 和循环之后print_r($options) 发布结果吗?这将有助于深入了解循环中的内容和输出内容,以确保它不是 Form Helper form_dropdown() 没有重新排序任何内容。

    我的建议是在form_dropdown() 之前添加一个简单的asort($options); 以确保它是按字母顺序排列的。

    【讨论】:

    • 好点。看来 form_dropdown() 不是罪魁祸首。打印 $options 表明它在生成下拉列表之前就搞砸了。打印结果见原帖。
    • 可能只是在form_dropdown() 之前加上一个简单的asort($options); 来确认它是按字母顺序排列的。
    【解决方案2】:

    您没有指定是升序还是降序。

    ORDER BY type_name ASCORDER BY type_name DESC

    【讨论】:

    • 如果是MySQL,ORDER BY的默认行为是按升序排列记录。
    • 它是 SQLite。添加 ASC 似乎没有任何区别。相同的订单结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多