duancuan6466 2016-01-30 06:53
浏览 48
已采纳

cakephp:如何在我的下拉列表中显示默认值?

i want to display a default value like 'select category' insted of blanck space

here is my tables

category_tbls

  • id
  • cat_name

subcategory_tbls

  • id
  • category_tbls_id
  • sub_cat

Model Addcat.php

class Addcat extends AppModel
{
    public $name = 'category_tbls';
    public $hashMany = 'subcategory_tbls';
}

Model Subcat.php

class Subcat extends AppModel
{
    public $name = 'subcategory_tbls';
    public $belongsTo = 'category_tbls';
}

AdminController.php

public function subcat()
{
    $this->loadModel("Subcat");

    if($this->Subcat->save($this->request->data))
    {
        $this->Session->setFlash('Successully save your information!');
    }

    $this->loadModel("Addcat");
    $dt=$this->Addcat->find('list',array('fields'=>array('id','cat_name')));
    $this->set('drop',$dt);

view file is as bellow

subcat.ctp

<h2>Add Sub Category!</h2>
                    <form action="subcat" method="post">
                    <?php echo $this->Form->select('category_tbls_id',$drop);?>
                        <input type="text" name="sub_cat" placeholder="sub category name" required/>

                        <button type="submit" class="b1">Add Sub Category</button>
                    </form>
  • 写回答

1条回答 默认 最新

  • donglin5770 2016-01-30 07:04
    关注

    You need to add 3rd parameter in your select function. If you don't want to show empty value in your dropdown:

    <?php echo $this->Form->select('category_tbls_id',$drop,array('empty' => 'Select Category'));?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?