Yii2 DropDownList Simple Usage Example _php instance

Source: Internet
Author: User
Tags smarty template yii
The examples in this paper describe the simple usage of DropDownList in YII2. Share to everyone for your reference, as follows:

Here, the practical application is used as an example to explain Yii2 DropDownList usage.

There is a classification table, infinite pole classification of the kind, table structure as follows, PID is the parent category ID
Here we want to achieve:

When creating a new category, the parent category can be selected from any of the categories or not selected

When editing a category, the parent category cannot select the currently edited category ... If you choose yourself, the parent category is your own, must be wrong!

The implementation code is as follows, I'll put all the code of the form

<?phpuse common\models\category;use yii\helpers\arrayhelper;use yii\helpers\html;use yii\widgets\ActiveForm;/* @ var $this yii\web\view *//* @var $model common\models\category *//* @var $form yii\widgets\activeform */?> <?ph P if (! $model->isnewrecord) {//If it is an edit category $cate = Arrayhelper::map (Category::find ()->andwhere (' id! =: id ', [': I    d ' = $model->id])->all (), ' id ', ' title ');    } else {//If it is a new classification $cate = Arrayhelper::map (Category::find ()->all (), ' id ', ' title '); }?> <?php $form = Activeform::begin ()?> <?= $form->field ($model, ' title ')->textinput ([' maxlength ' = +])->label ("category title")?> <?= $form->field ($model, ' name ')->textinput ([' maxlength ' = >])->label ("classification alias")?> <?= $form->field ($model, ' pid ')->dropdownlist ($cate, [' prompt ' = ' = ' Please select Parent category '])->label ("parent category")?> <?= $form->field ($model, ' keywords ')->textarea ([' maxlength ' = = 255])->l Abel("categorical keywords")?> <?= $form->field ($model, ' description ')->textarea ([' maxlength ' = = 255])->label ("Category description" )?> <?= Html::submitbutton ($model->isnewrecord? ' Create ': ' Update ', [' class ' = $model->isnewrecord?  ' Btn btn-block btn-success ': ' btn btn-block btn-primary ']?> <?php activeform::end ();       ?>

For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article is helpful to the PHP program design based on YII framework.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.