Difference between #37 and #49 of
By Example: CHtml

Changes

Title unchanged

By Example: CHtml

Category unchanged

Tutorials

Yii version unchanged

Tags changed

listData, chtml, link, button, ByExample, ajax, ajax link, image

Content changed

[...]
- [CHtml::link()](#hh0)
- [CHtml::ajaxLink()](#hh1)
- [CHtml::button()](#hh2)
- [CHtml::textField()](#hh3)
- [CHtml::listData()](#hh4)
- [CHtml::dropDownList()](#hh5)
 
- [Chtml::image()](#hh6) - Added new tag


## CHtml::link() method
[...]
array('id'=>'idTextField',
'width'=>100,
'maxlength'=>100)
); ?>
```
[...]
```


 
***
 
 
**Example 5: Generating a hidden textfield**
 
 
```php 
<?php echo CHtml::hiddenField('Text', 'some value', 
 
    array('id'=>'idTextField'); ?>
 
```
 
 
## CHtml::listData() method

~~~
[...]
<?php echo CHtml::dropDownList('listname', $select,
$model->genderOptions,
array('empty' => '(Select a gender
')');
```
[...]
```php
<?echo CHtml::dropDownList('categories', $category, 
 
              $list,
 
              array('empty' => '(Select a category)');
 
```
 
 
With condition -
 
 
 
```
php  
echo CHtml::dropDownList('categories', $category, $listCHtml::listData(categories::model()->findAll("Status=1"),
 
              'category_id', 'category_name')
, array('empty' => '(Select a category')'); ```
 
 
 
**Example 5: Generating dropdownlist with option groups.**

If you need to generate dropdownlist using both `optgroup` and `option` tags use the following code.
[...]
```


 
## CHtml::image() method
 
 
 
```php 
public static string image(string $src, string $alt='', array $htmlOptions=array ( ))
 
```
 
 
 
Generates a image tag.
 
 
***
 
 
**Example 1: Image only**
 
 
```php 
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/img/image.png'); ?>
 
```
 
 
**HTML Output:**
 
 
 
```php 
<img src="/img/image.png" />
 
```
 
 
 
***
 
 
**Example 2: Image with alt attribute**
 
 
```php 
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/img/image.png', 
 
'this is alt tag of image'); ?>
 
```
 
 
**HTML Output:**
 
 
 
```php 
<img src="/img/image.png" alt="this is alt tag of image" />
 
```
 
 
 
 
***
 
 
**Example 3: Add width, height and others attribute for image**
 
 
```php 
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/img/image.png',
 
      'this is alt tag of image',
 
      array('width'=>'100px','height'=>'100px','title'=>'image title here')); ?>
 
```
 
 
**HTML Output:**
 
 
 
```php 
<img src="/img/image.png" alt="this is alt tag of image" title="image title here" width="100" height="100" />
 
```
 
 
You can also add another html attributes like class, id etc. for image in the similar way to adding width and height attribute for image above.
 
 
 
***
 
 
**Example 4: Image with link**
 
 
```php 
<?php $image = CHtml::image(Yii::app()->request->baseUrl.'/img/image.png',
 
      'this is alt tag of image');
 
      
 
      echo CHtml::link($image,array('book/view','id'=>'book_id')); ?>
 
```
 
 
**HTML Output:**
 
 
 
```php 
<a href="index.php?r=book/view&id=book_id"><img src="/img/image.png" alt="this is alt tag of image" /></a>
 
```
 
 
 
 
 
***
You can find CHtml class at `yii/framework/web/helpers/CHtml.php` view on [Github](https://github.com/yiisoft/yii/blob/1.1.14/framework/web/helpers/CHtml.php ""), all [CHtml methods here](http://yii.codexamples.com/v1.1/CHtml/ "").

## Links

### Russian
[...]
78 0
74 followers
Viewed: 919 987 times
Version: 1.1
Category: Tutorials
Written by: jonah
Last updated by: glicious
Created on: Sep 25, 2009
Last updated: 3 years ago
Update Article

Revisions

View all history