从数据库中获取数据并在 yii 中创建列表框 [英] Fetch data from database and create listbox in yii

查看:24
本文介绍了从数据库中获取数据并在 yii 中创建列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表 'tbl_department' 有 2 个字段 'dpt_id' n 'dpt_name'我想获取所有dpt_name"作为页面上的列表框输出.

i have a table 'tbl_department' with 2 fields 'dpt_id' n 'dpt_name' i want to fetch all 'dpt_name' as listbox output on a page.

推荐答案

遵循this 来自优秀 Yii 文档的示例,我已经从 tbl_user 算出用户名.您可以使用最小的 sql 脚本 protected/data/schema.mysql.sql 获取该表,该脚本在您使用 yiic 创建空应用程序时安装.

following this sample from excellent Yii documentation, i've worked out the username from tbl_user. You can get that table, using the minimal sql script protected/data/schema.mysql.sql that get installed when you create the empty application with yiic.

<?php
    $records = User::model()->findAll();
    $list = CHtml::listData($records, 'id', 'username');
    echo CHtml::dropDownList('names', null, $list, array('empty' => '(Select a name)'));
?>

这篇关于从数据库中获取数据并在 yii 中创建列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆