Académique Documents
Professionnel Documents
Culture Documents
demos guide class reference wiki extensions forum live chat login
DOCUMENTATION Take the Tour Tutorials Class Reference Wiki Screencasts Resources
S E A RCH WI K I
CHtml::link() method
CHtml::button() method Find
CHtml::textField() method
CHtml::listData() method View current article
CHtml::dropDownList() method Update this article
Links View history
View revision
Russian
CHtml::link()
Related Articles
CHtml::button()
Change buttons on our CGridView
CHtml::textField()
extending yii
CHtml::listData()
using multiple radio button
CHtml::dropDownList()
CButtonColumn: Use special
variable $data for the 'id' in the
'options' of a button
CHtml::link() method
A simple go back button widget
htmlOptions explained for various
public static string link(string $text, mixed $url='#', array $htmlOptions=array ( )) controls.
HTML Output:
HTML Output:
www.yiiframework.com/wiki/48/by-example-chtml/ 1/7
11/10/12 By Example: CHtml | Wiki | Yii Framework
'param1'=>'value1',
'param2'=>'value2',
'param3'=>'value3')); ?>
HTML Output:
HTML Output:
Example 5: Linking to a controller action inside the actual controller (Suppose you are in the
PostController/view and wants to link to PostController/create)
If you are linking to an action from another controller, use the syntax of the former examples.
Example 6: Linking to a controller action from the site root (Suppose you are inside a module and wants
to make the link from a controller of the root application)
In this case, add an slash "/" at the start of the string url
This is useful when you want to make absolute paths avoiding to use static module names.
Delete actions created using gii require the delete request be sent via POST to help prevent deleting objects
by accident. Below is an example how to create a link that sends the request via POST and also asks for
confirmation. Where you are redirected after the delete depends on your delete action. Note that the idlink
parameter below is a GET type parameter (submit URL will be something like
http://example.com/post/delete/id/100).
www.yiiframework.com/wiki/48/by-example-chtml/ 2/7
11/10/12 By Example: CHtml | Wiki | Yii Framework
If you are using CSRF protection in your application do not forget to add csrf parameter to the htmlOptions
array.
Example 10: Linking to a controller action via POST with POST parameters
If you need to make POST request with arbitary link with additional POST parameters you should use following
code (submit URL will be something like http://example.com/blog/deletePost/param/100).
CHtml::button() method
Generates a button.
HTML Output:
CHtml::textField() method
Generates a textfield.
www.yiiframework.com/wiki/48/by-example-chtml/ 3/7
11/10/12 By Example: CHtml | Wiki | Yii Framework
CHtml::listData() method
Generates data for dropDownList and listBox, using the format $key=>$value.
<?php
/*you can use here any find method you think
proper to return your data from db*/
$models = categories::model()->findAll();
print_r($list);
Example 2: Generating an ordered list data for categories using findAll parameter
<?php
$models = categories::model()->findAll(
array('order' => 'category_name'));
$list = CHtml::listData($models,
'category_id', 'category_name');
print_r($list);
CHtml::dropDownList() method
www.yiiframework.com/wiki/48/by-example-chtml/ 4/7
11/10/12 By Example: CHtml | Wiki | Yii Framework
$name: A name for the dropdownList; $select: selected item from the $data $data: an array of the type $key =>
$value (the possible values of you dropdownlist); $htmlOptions: another options.
This example will avoid the dropdownlist to be blank when no value, outputing some proper information to the
user
It is better to have you gender list definition inside your model definition.
At model:
At view:
In order to create a dropdownList with data from db, you have to combine it with listData method that will format
models from db into array of $key=>$value.
www.yiiframework.com/wiki/48/by-example-chtml/ 5/7
11/10/12 By Example: CHtml | Wiki | Yii Framework
If you need to generate dropdownlist using both optgroupand optiontags use the following code.
<div class="cars-select">
<?php echo CHtml::dropDownList('Cars', 'car_id', array(
'Mazda'=>array(
'mazda-rx7'=>'RX7',
'mazda-rx5'=>'RX5',
),
'Volvo'=>array(
'volvo-b9tl'=>'B9TL',
'volvo-l90e-radlader'=>'L90E Radlader',
),
)); ?>
</div>
<div class="cars-select">
<select name="Cars" id="Cars">
<optgroup label="Mazda">
<option value="mazda-rx7">RX7</option>
<option value="mazda-rx5">RX5</option>
</optgroup>
<optgroup label="Volvo">
<option value="volvo-b9tl">B9TL</option>
<option value="volvo-l90e-radlader">L90E Radlader</option>
</optgroup>
</select>
</div>
Links
Russian
Russian Version dropDownList()
Russian Version listData()
Russian Version textField()
Russian Version link()
Total 3 comments
CHtml::button('Delete',
array('submit' => array('user/delete','id'=>$model->usId),
'name'=>'btnDelete',
'confirm'=>'Are you sure you want to delete?',
'class'=>'btn btn-large btn-danger',
'style'=>'width:160px;'
));
Want more
I think we also need to add link with confirmation, especially in delete condition
www.yiiframework.com/wiki/48/by-example-chtml/ 6/7
11/10/12 By Example: CHtml | Wiki | Yii Framework
Leave a comment
Please login to leave your comment.
Development Community
Contribute to Yii Forum
Latest Updates Live Chat
Report a Bug News
Report a Security Issue Hall of Fame
Badges
Terms
Twof
itter
Service
Facebook
| License
LinkedIn| RSS
Contact Us
Copyright 2012 by Yii Softw are LLC. Feeds
All Rights Reserved.
www.yiiframework.com/wiki/48/by-example-chtml/ 7/7