donglu5047 2014-08-12 06:18
浏览 57

如何在选择框中显示城市列表,城市列表将来自cakephp中的数据库

I have a cities table in my database that contain the list of city and each city has their own id(Unique).

Now I want to show the list of city in a select box in my view page so that user can select the city from them.

I am using the below code in my controller:

$sql = mysql_query("select * from cities ORDER BY city_name");
if(mysql_num_rows($sql)>0){

    while($row=mysql_fetch_array($sql))
    {
        echo '<option value="'.$row['city_name'].'">'.$row['city_name'].'</option>';
    }
}
else
{
    echo '<option selected="selected">No data</option>';
}

What code should I use in view to show the list if city in the select box?

  • 写回答

1条回答 默认 最新

  • douxuanou2787 2014-08-12 08:53
    关注

    Why not use a City model?

    Than you can use the find('list') option in your controller:

    $this->set('cities', $this->City->find('list',array('fields' => array('id','city_name'))));
    

    Then in your view use this:

    echo $this->Form->input('City');
    

    More on the find list option: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

    More on the form helper: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?