In all Entertainment sites, Star rating is a common phenomena. We need to use star rating for rating different resources like videos, articles, thesis, posts even sometimes persons. JQuery has an excellent Star Rating plugin, which I recently use with PHP CodeIgniter. This is very simple to integrate with controller and show high quality rating options.

Jquery Call:


$(function() {
       $('.auto-submit-star').rating({
            required: true,
            callback: function(value, link) {

            $.ajax({
                     type: "post",
                     url: site_url + "user/view/star",
                     dataType: "json",
                     data: "&video=" + $('#video_url').val() + "&rate_val=" + value,

              success: function(e) {
                   $.jGrowl(e.code + "<br>" + e.msg);
               }
         });
     }
});

Controller Method:

public function star()
    {
        $rate = $this->input->post("rate_val", true);
        $video_url = $this->input->post("video", true);
        $this->load->model("Model Name");
        $video_id = $this->model_name->get_video_id($video_url);
        unset($this->layout); //Block template Layout
        if (get_user_id())   //get_user_id() return login user id
        {
            if (!$this->model_name->is_video_rated(get_user_id(), $video_id))
            {
                $data = array("video_id" => $video_id,
                    "user_id" => get_user_id(),
                    "videos_rating_value" => $rate,
                    "videos_rating_date" => "" . date("Y-m-d H:i:s") . ""
                    );
                if ($this->model_name->insert_rating($data, $video_id, $rate))
                {
                    echo json_encode(array("code" => "Success", "msg" => "Your Video Rating has been posted"));
                }
                else
                {
                    echo json_encode(array("code" => "Error", "msg" => "There was a problem rating your video"));
                }
            }
            else
            {
                echo json_encode(array("code" => "Error", "msg" => "You have already rated this video"));
            }
        }
        else
        {
            echo json_encode(array("code" => "Error", "msg" => "You have to login to rate the video"));
        }
        exit(0);
    }

Presentation View:

    if (!get_user_id()) //Check if user logged in
    {
        $radio_level = "disabled";
    }
    else
    {
        $radio_level = " ";
    }

    for($i = 1;$i <= 5;$i++)
    {
        if ($i == round($row["total_rate"]))
        {
       ?>
       <input class="auto-submit-star" type="radio" name="rating" <?php echo "$radio_level";

            ?> value="<?php echo $i;

            ?>" checked="checked"/>
	<?php
        }
        else
        {
         ?>
	<input class="auto-submit-star" type="radio" name="rating" <?php echo "$radio_level";

            ?> value="<?php echo $i;

            ?>"/>
   <?php
        }
    } //end of for
?>
    

Its a very cool 🙂 rating JQuery plugins.
For demo click

Advertisements