Welcome Guest, Not a member yet? Register   Sign In
Ajax star rating bar
#3

[eluser]wiredesignz[/eluser]
Create application/models/ratings_model.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ratings_model extends Model
{
    function Ratings_model()
    {
        parent::Model();
    }
    
    function bar($id, $units = '', $static = '')
    {
        //set some variables
        ($units)  OR $units = 10;
        ($static) OR $static = FALSE;
        
        $ip = $this->input->ip_address();
        $rating_unitwidth = $this->config->item('rating_unitwidth');
        
        //default values
        $count = 0;
        $current_rating = 0;
        $tense = "votes"; // 0 votes
        
        // get votes, values, ips for the current rating bar
        if (!$numbers = $this->findBy_id($id))
        {
            // insert the id in the DB if it doesn't exist already
            $data = array(
                'id' => $id,
                'total_votes' => $count,
                'total_value' => $current_rating,
                'used_ips' => '',
            );
            $this->insert($data);
        }
        else
        {
            $count = $numbers['total_votes']; //how many votes total        
            $current_rating = $numbers['total_value']; //total number of rating
            $tense = ($count == 1) ? "vote" : "votes"; //plural form votes/vote
        }
        
        $voted = (bool)$this->countBy_ip($ip, $id);

        //more default values
        $rating_width = 0;
        $rating1 = 0;
        $rating2 = 0;
        
        // now draw the rating bar
        if ($count > 0)
        {
            $rating_width = number_format($current_rating/$count,2) * $rating_unitwidth;
            $rating1 = number_format($current_rating/$count,1);
            $rating2 = number_format($current_rating/$count,2);
        }
        
        $data = array(
            'id' => $id,
            'current_rating' => $current_rating,
            'count' => $count,
            'units' => $units,
            'tense' => $tense,
            'voted' => $voted,
            'rating_width' => $rating_width,
            'rating1' => $rating1,
            'rating2' => $rating2,
            'rating_unitwidth' => $rating_unitwidth,
            'ip' => $ip,
        );
        
        if ($static == 'static')
        {
            return $this->load->view('static_rating_view', $data, TRUE);
        }
        else
        {
            return $this->load->view('dynamic_rating_view', $data, TRUE);
        }
    }
    
    function findBy_id($id)
    {
        $this->db->select('total_votes, total_value, used_ips');
        $query = $this->db->getwhere('ratings', "id = '{$id}'");
        return $query->row_array();
    }
    
    function countBy_ip($ip, $id)
    {
        $this->db->select('used_ips');
        $query = $this->db->getwhere('ratings', "used_ips LIKE '%{$ip}%' AND id = '{$id}'");
        return count($query->result());        
    }
    
    function updateBy_id($id, $data)
    {
        $this->db->where('id', $id);
        $this->db->update('ratings', $data);
    }
    
    function insert($data)
    {
        $this->db->insert('ratings', $data);
    }
}


Messages In This Thread
Ajax star rating bar - by El Forum - 02-14-2008, 12:12 AM
Ajax star rating bar - by El Forum - 02-14-2008, 12:14 AM
Ajax star rating bar - by El Forum - 02-14-2008, 12:16 AM
Ajax star rating bar - by El Forum - 02-14-2008, 12:20 AM
Ajax star rating bar - by El Forum - 02-14-2008, 12:32 AM
Ajax star rating bar - by El Forum - 02-14-2008, 12:51 AM
Ajax star rating bar - by El Forum - 02-15-2008, 12:13 AM
Ajax star rating bar - by El Forum - 02-16-2008, 07:20 AM
Ajax star rating bar - by El Forum - 02-16-2008, 07:36 AM
Ajax star rating bar - by El Forum - 02-16-2008, 11:49 AM
Ajax star rating bar - by El Forum - 02-16-2008, 03:34 PM
Ajax star rating bar - by El Forum - 02-16-2008, 06:06 PM
Ajax star rating bar - by El Forum - 02-16-2008, 07:26 PM
Ajax star rating bar - by El Forum - 02-18-2008, 12:11 AM
Ajax star rating bar - by El Forum - 02-18-2008, 03:25 AM
Ajax star rating bar - by El Forum - 02-18-2008, 02:43 PM
Ajax star rating bar - by El Forum - 02-18-2008, 03:14 PM
Ajax star rating bar - by El Forum - 02-18-2008, 08:40 PM
Ajax star rating bar - by El Forum - 02-18-2008, 08:43 PM
Ajax star rating bar - by El Forum - 02-18-2008, 08:47 PM
Ajax star rating bar - by El Forum - 02-18-2008, 08:52 PM
Ajax star rating bar - by El Forum - 02-18-2008, 08:56 PM
Ajax star rating bar - by El Forum - 02-18-2008, 09:51 PM
Ajax star rating bar - by El Forum - 02-18-2008, 10:20 PM
Ajax star rating bar - by El Forum - 02-18-2008, 10:32 PM
Ajax star rating bar - by El Forum - 02-19-2008, 12:45 AM
Ajax star rating bar - by El Forum - 02-19-2008, 02:00 AM
Ajax star rating bar - by El Forum - 02-19-2008, 06:23 PM
Ajax star rating bar - by El Forum - 02-19-2008, 09:13 PM
Ajax star rating bar - by El Forum - 03-14-2008, 06:18 PM
Ajax star rating bar - by El Forum - 03-17-2008, 01:17 PM
Ajax star rating bar - by El Forum - 03-18-2008, 07:54 PM
Ajax star rating bar - by El Forum - 03-19-2008, 07:53 PM
Ajax star rating bar - by El Forum - 03-19-2008, 08:06 PM
Ajax star rating bar - by El Forum - 06-02-2008, 04:05 PM
Ajax star rating bar - by El Forum - 06-05-2008, 10:01 PM
Ajax star rating bar - by El Forum - 06-05-2008, 10:25 PM
Ajax star rating bar - by El Forum - 06-06-2008, 06:39 AM
Ajax star rating bar - by El Forum - 06-06-2008, 12:28 PM
Ajax star rating bar - by El Forum - 06-11-2008, 06:53 PM
Ajax star rating bar - by El Forum - 06-12-2008, 01:00 PM
Ajax star rating bar - by El Forum - 06-17-2008, 07:42 AM
Ajax star rating bar - by El Forum - 06-17-2008, 11:04 AM
Ajax star rating bar - by El Forum - 06-17-2008, 10:50 PM
Ajax star rating bar - by El Forum - 06-23-2008, 11:43 AM
Ajax star rating bar - by El Forum - 08-24-2008, 07:10 AM
Ajax star rating bar - by El Forum - 10-15-2008, 12:30 AM
Ajax star rating bar - by El Forum - 02-24-2009, 04:11 AM
Ajax star rating bar - by El Forum - 06-03-2009, 10:47 PM
Ajax star rating bar - by El Forum - 06-03-2009, 10:56 PM
Ajax star rating bar - by El Forum - 06-04-2009, 04:34 AM
Ajax star rating bar - by El Forum - 06-04-2009, 05:27 AM
Ajax star rating bar - by El Forum - 08-09-2009, 01:26 PM
Ajax star rating bar - by El Forum - 10-28-2009, 03:18 AM
Ajax star rating bar - by El Forum - 07-01-2010, 03:39 PM
Ajax star rating bar - by El Forum - 07-02-2010, 01:18 AM
Ajax star rating bar - by El Forum - 07-02-2010, 02:04 AM
Ajax star rating bar - by El Forum - 07-02-2010, 05:41 AM
Ajax star rating bar - by El Forum - 07-04-2010, 12:14 AM
Ajax star rating bar - by El Forum - 03-10-2011, 01:56 PM
Ajax star rating bar - by El Forum - 06-26-2011, 08:53 PM
Ajax star rating bar - by El Forum - 08-16-2011, 09:43 AM
Ajax star rating bar - by El Forum - 02-01-2012, 10:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB