Hi guys, i’m currently creating an application with the star rating widget.
What i’m looking to achieve is that a person should only be allowed to have one rate of a product, but he may update his rating if he chooses. Therefore the system should check if a rating of that user on that product already exist. If i does, the default value of the stars should be the rating he/she submitted last time, and if the user chooses to submit again, this row in the database table should be updated. If no rating on that product has been done by this user before, he/she should be see empty stars, and if any rating is submitted this would create a new row in the database table.
I have some ideas on how some of this would be made, but i can be wrong about the method. I’m particular unsure of how the application should check wether the user already have a rating on that product.
2- when user will rate some post for example data will save in above table .
3- if user will change him rate then you need to check your table :
a- if user had no raw in table insert new one if else go to (
b- call function to return user data where post = current post and user_id= $user_id
c- if old rate != new $rate then update record with new value
That is
rate action will be like that :
public function actionRate(){
/// note user id you can get it from session or any method u saved it befor
if(isset($_GET['post_id'])) {
$post_id=intval($_POST['post_id']);
$new_rate=intval($_POST['new_rate']);
$model=rate::model()->find("user_id=$user_id and post_id=$post_id");
if($model===null){/// So no old recorde
//// insert new value in table
}
else{
$old_rate=$model->rate;
if($old_rate!== $new_rate) {
$model->rate=$new_rate;
$model->save(false);
}
}
}
}