[Solved] How To Remove Duplicates Entry From Dropdownlist In Cgridview To Filter Model?

Hi All,

I’m a newbie to Yii and PHP. I want to use a dropDownList in CGridView to filter database (MySQL). I had no problem in columns with foreignKey or relations, it displays dropDownList options and filtered the correct data. However the other columns (w/out foreignKey), although it displays the dropDown button and list down all options but after you select one the result is always “No results found”. Is it possible to remove duplicates from dropDownList options and why the filter in this column is not working?

Thanks in Advance.

CONTROLLER:

/**

 * Manages all models.


 */


public function actionAdmin()


{


	$model=new Scaffold('search');


	$model->unsetAttributes();  // clear any default values


	if(isset($_GET['Scaffold']))


		$model->attributes=$_GET['Scaffold'];





	$this->render('admin',array(


		'model'=>$model,


	));


}

MODEL:

/**

 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('id, department_id, facility_id, location, type_id, duty_id, status_id, date_erected', 'required'),


		array('id, department_id, facility_id, type_id, duty_id, status_id, work_order, deluge_id, los_id, deboard_id', 'numerical', 'integerOnly'=>true),


                    array('duty_id', 'in', 'range'=>self::getAllowedDutyRange()),


                    array('status_id', 'in', 'range'=>self::getAllowedStatusRange()),


                    array('deluge_id', 'in', 'range'=>self::getAllowedDelugeRange()),


                    array('los_id', 'in', 'range'=>self::getAllowedLosRange()),


                    array('deboard_id', 'in', 'range'=>self::getAllowedDeboardRange()), 


		array('weight, erect_manhrs, dismantle_manhrs', 'length', 'max'=>11),


		array('requested_by, built_by', 'length', 'max'=>64),


                    array('id', 'unique'),


		array('inspection_date, date_dismantled, other_details, material_list', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


                    array('id, department_id, facility_id, location, type_id, duty_id, status_id, weight, work_order, erect_manhrs, dismantle_manhrs, date_erected, inspection_date, date_dismantled, requested_by, built_by, other_details, deluge_id, los_id, deboard_id, material_list, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),			


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'inspections' => array(self::HAS_MANY, 'Inspection', 'scaffold_id'),


		'type' => array(self::BELONGS_TO, 'Type', 'type_id'),


		'department' => array(self::BELONGS_TO, 'Department', 'department_id'),


		'facility' => array(self::BELONGS_TO, 'Facility', 'facility_id'),


	);


}





/**


 * @return array customized attribute labels (name=>label)


 */


public function attributeLabels()


{


	return array(


                    'id' => 'Reference Number',


		'department_id' => 'Department Requestor',


                    'facility_id' => 'Facility',                 


		'location' => 'Location',


		'type_id' => 'Type of Scaffold',


		'duty_id' => 'Design Loading',


		'status_id' => 'Status',


		'weight' => 'Weight (Kg.)',


		'work_order' => 'Work Order',


		'erect_manhrs' => 'Manhours to Build',


		'dismantle_manhrs' => 'Manhours to Dismantle',


		'date_erected' => 'Date Erected',


		'inspection_date' => 'Next Inspection Due',


		'date_dismantled' => 'Date Dismantled',


		'requested_by' => 'Requested By',


		'built_by' => 'Built By',


                    'other_details' => 'Other Details',


		'deluge_id' => 'Blocking Deluge?',


		'los_id' => 'Blocking LOS?',


		'deboard_id' => 'Deboarded?',


                    'material_list' => 'Material List',


		'create_time' => 'Create Time',


		'create_user_id' => 'Create User',


		'update_time' => 'Update Time',


		'update_user_id' => 'Update User',                    


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.





	$criteria=new CDbCriteria;





            $criteria->compare('id',$this->id);


	$criteria->compare('department_id',$this->department_id);


	$criteria->compare('facility_id',$this->facility_id);


	$criteria->compare('location',$this->location,true);


	$criteria->compare('type_id',$this->type_id);


	$criteria->compare('duty_id',$this->duty_id);


	$criteria->compare('status_id',$this->status_id);


	$criteria->compare('weight',$this->weight,true);


	$criteria->compare('work_order',$this->work_order);


	$criteria->compare('erect_manhrs',$this->erect_manhrs,true);


	$criteria->compare('dismantle_manhrs',$this->dismantle_manhrs,true);


	$criteria->compare('date_erected',$this->date_erected,true);


	$criteria->compare('inspection_date',$this->inspection_date,true);


	$criteria->compare('date_dismantled',$this->date_dismantled,true);


	$criteria->compare('requested_by',$this->requested_by,true);


	$criteria->compare('built_by',$this->built_by,true);


            $criteria->compare('other_details',$this->other_details,true);


	$criteria->compare('deluge_id',$this->deluge_id);


	$criteria->compare('los_id',$this->los_id);


	$criteria->compare('deboard_id',$this->deboard_id);


	$criteria->compare('material_list',$this->material_list,true);


	$criteria->compare('create_time',$this->create_time,true);


	$criteria->compare('create_user_id',$this->create_user_id);


	$criteria->compare('update_time',$this->update_time,true);


	$criteria->compare('update_user_id',$this->update_user_id);





	return new CActiveDataProvider($this, array(


		'criteria'=>$criteria,


                    'sort' => array(


                        'defaultOrder' => 'id DESC',


                   ),


                    'pagination' => array(


                        'pageSize' => 10,


                   ),


            ));


}

ADMIN.PHP:

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'scaffold-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


            array(


                'name' =&gt; 'id',


                'htmlOptions' =&gt; array('width'=&gt;60)


            ), 


            array(


                'name' =&gt; 'department_id',


                'value' =&gt; '&#036;data-&gt;department-&gt;name',


                'filter' =&gt; CHtml::listData(Department::model()-&gt;findAll(),'id','name'),


                'htmlOptions' =&gt; array('width'=&gt;95)


            ),


            array(


                'name' =&gt; 'facility_id',                    


                'value' =&gt; '&#036;data-&gt;facility-&gt;name',


                'filter' =&gt; CHtml::listData(Facility::model()-&gt;findAll(),'id','name'),


                'htmlOptions' =&gt; array('width'=&gt;55)


            ),


	'location',


            array(


                'name' =&gt; 'status_id',


                'value' =&gt; '&#036;data-&gt;getStatusText()',


                'filter' =&gt; &#036;model-&gt;getStatusOptions(),


                'htmlOptions' =&gt; array('width'=&gt;90)


            ),


            array(


                'name' =&gt; 'date_erected',


                'filter' =&gt; CHtml::listData(Scaffold::model()-&gt;findAll(array('order'=&gt;'date_erected')),'id','date_erected'),


                'htmlOptions' =&gt; array('width'=&gt;90)


            ),


            array(


                'name' =&gt; 'inspection_date',


                'filter' =&gt; CHtml::listData(Scaffold::model()-&gt;findAll(array('order'=&gt;'inspection_date')),'id','inspection_date'),


                'htmlOptions' =&gt; array('width'=&gt;90)


            ),


        


	array(


		'class'=&gt;'CButtonColumn',


	),


),

)); ?>

Have you checked the Parameters sent in AJAX while you are performing filtering. ?

Thanks for your reply codesutra.

Sorry if this will be a dumb question but where can I find this AJAX and Parameters in my project?

Columns ‘department_id’, ‘facility_id’ and ‘status_id’ are filtering perfectly so I’m thinking that the problem is the codes for ‘date_erected’ and ‘inspection_date’ only. There is no syntax error to display dropDownList on both columns ‘date_erected’ and ‘inspection_date’ so I know that I only missing additional parameters for filtering to work as expected.

And again, is it really possible to remove duplicates from dropDownList options if data are coming from database as shown on attached screenshot.

use array_unique on array that you give to your dropdownlist/filter.


array_unique(Chtml::listData...);

But the id kept will be the first met parsing the array.

and use [ php] and [/ php] to write your code on forum please :).

for the other question, try :


CHtml::listData(Scaffold::model()->findAll(array('order'=>'inspection_date')),'inspection_date','inspection_date')

Indeed in your search function you want to compare for inspection_date $this->inspection_date or if you write


CHtml::listData(Scaffold::model()->findAll(array('order'=>'inspection_date')),'id','inspection_date'),

$this->id will be selected and compared with $this->inspection_date.

Thank You Very Much Zugluk.

Both my questions was answered & solved by only changing ‘id’ to ‘date_erected’ & ‘inspection_date’ for the two columns.


CHtml::listData(Scaffold::model()->findAll(array('order'=>'date_erected')),'date_erected','date_erected')


CHtml::listData(Scaffold::model()->findAll(array('order'=>'inspection_date')),'inspection_date','inspection_date')

Thanks for the tip of using [ php] and [/ php] on writing code, Sorry about my first post.

Thank you, that’s work