简体   繁体   中英

cakephp: how can i display a default value in my dropdownlist?

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>

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'));?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM