PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] How to add dropdown menu in a CGridView Yii Framework?

 January 09, 2022     mysql, php, yii     No comments   

Issue

I want to change the CGridView text field to dropdown menu and change the numbers to categories name.

category grid view

$model = new Products('search');
        $model->unsetAttributes();  
        if(isset($_GET['Products'])){
           $model->attributes=$_GET['Products'];
        }

And in the view.

<?php
       $this->widget('zii.widgets.grid.CGridView', array(
       'dataProvider'=>$model->search(),                               
       'columns'=>array(                                                                                                                                                       'code',
            'category_id',
            'quantity',
            ),
           'filter'=>$model,
        ));
    ?>

Solution

Simply write in place of 'category_id',

array(
           // 'name' => 'category_id',
             'header'=>'Category Name',
            'value' => '$data->categoryname($data->category_id)',
            'filter' => $categoryArray
        ),


categoryname() should be written in your `Product Model`.  



  Product Model
public function categoryname($category_id){
//fetch your category name with this category id and return it

return categoryName;
}

and $categoryArray should be a array with keys as category id and value as category_name

$categoryArray = CHtml::listData(Category::model()->findAll() 'category_id', 'category_name')


Answered By - Passionate Coder
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing