Welcome Guest, Not a member yet? Register   Sign In
Can a helper load a model?
#11

[eluser]taewoo[/eluser]
Thanks ucantblamem

In /system/application/helper/ajax_rating_helper.php

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');


    function rating_bar($id, $units=10, $static="FALSE")
    {
        $CI =& get_instance();            
        $CI->load->model('rating_model');
        
        $ip = $_SERVER['REMOTE_ADDR'];
        $row = $CI->rating_model->getRating(1);
        $count = ($row->total_votes < 1) ? 0 :  $row->total_votes;
        if($row->total_votes == 0 ) {
            $CI->rating_model->addRating($id);
        }
        
        $current_rating = $row->total_value; //total number of rating added together and stored
        $tense  = ($count==1) ? "vote" : "votes"; //plural form votes/vote
        
        $row = $CI->rating_model->getUserVotes($id, $ip);
        $voted = $row->count;
        
        // now draw the rating bar
        $rating_width = @number_format($current_rating/$count,2)*$rating_unitwidth;
        $rating1 = @number_format($current_rating/$count,1);
        $rating2 = @number_format($current_rating/$count,2);
        
        return ($static == "TRUE") ?
                static_votes($id, $units, $voted, $ip, $rating_unitwidth, $tense, $rating1, $rating2, $count) :
                non_static_votes($id, $units, $voted, $ip, $rating_unitwidth, $tense, $rating1, $rating2, $count);
    }
    
    function static_votes($id, $units, $voted, $ip, $rating_unitwidth, $tense, $rating1, $rating2, $count)
    {
        return "some_html";
        
    }
    
    function non_static_votes($id, $units, $voted, $ip, $rating_unitwidth, $tense, $rating1, $rating2, $count)
    {
        
        return "some_other_ html";
    }
    
?&gt;


And in /system/application/model/rating_model.php

Code:
&lt;?php
class Rating_model extends Model {

    function Rating_model()
    {
        parent::Model();
        $this->obj =& get_instance();
    }
    
    function getRating($id)
    {
        $query = $this->db->getWhere("rating", array("id" => $id));
        return $query->result();
    }

    function addRating($id)
    {
        $this->db->insert('rating', array("id" =>$id,
                                          "total_votes" => 0,
                                          "total_value" => 0,
                                          "used_ips" => NULL));
        return $this->db->insert_id();
    }
    
    function getUserVotes($id, $ip)
    {
        $query = $this->db->query("SELECT count(*) as count FROM rating WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' ");
        return $query->result();
    }

}
?&gt;

By the way.. i am trying to import this Unobtrusive Ajax rating thing into my site.


Messages In This Thread
Can a helper load a model? - by El Forum - 02-12-2008, 10:09 PM
Can a helper load a model? - by El Forum - 02-12-2008, 10:15 PM
Can a helper load a model? - by El Forum - 02-12-2008, 10:21 PM
Can a helper load a model? - by El Forum - 02-12-2008, 10:25 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:00 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:09 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:12 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:13 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:17 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:22 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:25 PM
Can a helper load a model? - by El Forum - 02-12-2008, 11:41 PM
Can a helper load a model? - by El Forum - 02-13-2008, 12:53 AM
Can a helper load a model? - by El Forum - 02-13-2008, 06:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB