问题描述
限时送ChatGPT账号..我试图从一个简单的 mySql 表 tbl_u_type
中检索数据,该表只有两列,'tid' 和 'type'.
I am trying to retrieve data from a simple mySql table tbl_u_type
which has just two columns, 'tid' and 'type'.
我想使用直接 SQL 查询而不是模型逻辑.我用过:
I want to use a direct SQL query instead of the Model logic. I used:
$command = Yii::app()->db->createCommand();
$userArray = $command->select('type')->from('tbl_u_type')->queryAll();
return $userArray;
但在下拉列表中,它会自动显示一个索引号以及所需的条目.有什么办法可以避免索引号吗?
But in the dropdown list it automatically shows an index number along with the required entry. Is there any way I can avoid the index number?
推荐答案
要在下拉列表中使用数据数组,请使用 CHtml::listData() 方法.如果我理解这个问题是正确的,这应该会让你继续前进.像这样:
To make an array of data usable in a dropdown, use the CHtml::listData() method. If I understand the question right, this should get you going. Something like this:
$command = Yii::app()->db->createCommand();
$userArray = $command->select('tid, type')->from('tbl_u_type')->queryAll();
echo CHtml::dropdownlist('my_dropdown','',CHtml::listData($userArray,'tid','type'));
如果您为 tbl_u_type
表设置了一个模型,您也可以使用模型执行此操作:
You can also do this with the Model if you have one set up for the tbl_u_type
table:
$users = UType::model()->findall();
echo CHtml::dropdownlist('my_dropdown','',CHtml::listData($users ,'tid','type'));
我希望这能让你走上正轨.我没有像往常一样在这里测试我的代码,所以要注意这一点.;) 祝你好运!
I hope that gets you on the right track. I didn't test my code here, as usual, so watch out for that. ;) Good luck!
这篇关于yii 中的 CDbcommand 方法 queryAll() 是否仅返回索引条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论