Difference between #2 and #1 of How to filter data using a dropdown list?

unchanged
Title
How to filter data using a dropdown list?
unchanged
Category
Tutorials
unchanged
Tags
changed
Content
A dropdown list can be used to select a value, based on which we can filter the
data to be displayed. For example, the [extension page](/extensions/) has a
dropdown list to filter the displayed extensions according to the selected
category.

The following is the view code used for the category filter in the extension
page:

~~~
[php]
<?php echo CHtml::form('','get'); ?>
Category: 
<?php echo CHtml::dropDownList('cat',
    isset($_GET['cat'])?(int)$_GET['cat']:0,
    CHtml::listData($categories,'id','name'),
    array('empty'=>'All categories', 'submit'=>'')); ?>
</form>
~~~

We first create a form which uses GET as the submission method. We then use
[CHtml::dropDownList] to create the dropdown list. We specify two HTML options
for the dropdown list:
 
 * empty: this displays an extra dropdown list item at the beginning because
this item is not provided by the `$categories` array.
 * submit: this makes the dropdown list to be able to submit the form. If you
change the current selection, the form enclosing the dropdown list will be
submitted to the current page (because the submit value is empty).

Once the form is submitted, we will obtain a `cat` GET variable which contains
the current category selection. We use this value to filter the extensions and
display the corresponding results.

### Links
[Chinese version](http://dreamneverfall.cn/node/110)
Write new article
  • Written by: qiang
  • Updated by: Yang He
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +6
  • Viewed: 22,739 times
  • Created on: Feb 16, 2009
  • Last updated: Jun 29, 2012