Yii Star Rating

Is Rating or Voting concept available in yii?

Yes It Is

available in yii framework.
CStarRating class show the star rating widget in yii framework. It is used to get the rating details of user. It is based on jQuery Star Rating Plugin. We can show this star and assign the value to this stars. We can get the value of rated star value in controller.

CStarRating

1 <?php
2 $this->widget('CStarRating',array('name'=>'rating'));
3 ?>

We can get this value in controller.

1 $rating=$_POST['rating'];
2 [OR]
3 $rating=$_GET['rating'];

 

Star Rating: Properties

We can assign the minimum and maximum value for star list. “minRating”, “maxRating” properties are used for this. ‘value’ property used to set default value of selected star value. ‘starCount’ used to show the number of star.

1 <?php
2 $this->widget('CStarRating',array(
3             'name'=>'rating_1',
4             'value'=>'3',
5             'minRating'=>1,
6             'maxRating'=>10,
7             'starCount'=>10,
8             ));
9 ?>

 

Star Rating: Read Only

‘readOnly’ is a boolean property. The default value is false. So User can give the rating. But if you set ‘true’ We can only see the rating cannot give rating.

1 <?php
2 $this->widget('CStarRating',array(
3             'name'=>'rating_2',
4             'value'=>'3',
5             'readOnly'=>true,
6             ));
7 ?>

 

Star Rating With Model

We can assing the model and model attribute to CStarRating widget using ‘attribute’,’model’ parameter.

1 <?php
2 $this->widget('CStarRating',array(
3             'attribute'=>'rememberMe',
4             'model'=>$model,
5             ));
6 ?>

 

Star Rating: Ajax

Using callback property we can call function using ajax and pass the value of rating via ajax.

01 <?php
02 $this->widget('CStarRating',array(
03     'name'=>'star_rating_ajax',
04     'callback'=>'
05         function(){
06                 $.ajax({
07                     type: "POST",
08                     url: "'.Yii::app()->createUrl('cstarrating/ajax').'",
09                     data: "star_rating=" + $(this).val(),
10                     success: function(data){
11                                 $("#mystar_voting").html(data);
12                         }})}'
13   ));
14 echo "<br/>";
15 echo "<div id='mystar_voting'></div>";
16 ?>

CStarRating: Titles

The default value of stars are value of star like 1, 2, 3, 4 etc. we can change the title of star using ‘titles’ property. It contains array of values.Ex 1->Normal, 2->Average

01 <?php
02 $this->widget('CStarRating',array(
03             'name'=>'rating_4',
04             'value'=>'3',
05             'minRating'=>1,
06             'maxRating'=>5,
07             'titles'=>array(
08                 '1'=>'Normal',
09                 '2'=>'Average',
10                 '3'=>'OK',
11                 '4'=>'Good',
12                 '5'=>'Excellent'
13             ),
14             ));
15 ?>

Leave a Reply

Your email address will not be published. Required fields are marked *