我正在尝试按字母顺序排列下拉项目,但我无法这样做。我一定错过了一些明显的东西..
我假设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 )
答案 0 :(得分:1)
您可以在print_r($data['training_types'])
循环之前foreach
和循环之后的print_r($options)
,并发布结果吗?这将有助于深入了解循环内容以及出现的内容,以确保不是表单助手form_dropdown()
不重新排序任何内容。
我的建议是在asort($options);
之前添加一个简单的form_dropdown()
,以确保它是按字母顺序排列的。
答案 1 :(得分:0)
您没有指定它是升序还是降序。
ORDER BY type_name ASC
或ORDER BY type_name DESC