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.
#12

[eluser]ejangi[/eluser]
You "getRating()" method in the model is return an array of objects, yet in the helper you're trying to ask for properties of an array (which is impossible as an array is not an object in PHP, hence the error).

Try changing getRating() to this:
Code:
function getRating($id)
{
    $query = $this->db->getWhere("rating", array("id" => $id));
    $return = $query->result();
    return (isset($return[0])) ? $return[0] : 0;
}
#13

[eluser]taewoo[/eluser]
Doh!
You're right. How could i've been so el-stupid-o
Thanks for your help.

I had this issue before but I totally forgot. This is what happens when you're coding 16+ hrs straight. Dammit.
#14

[eluser]wiredesignz[/eluser]
I have done pretty much the same thing, take a look it may help.

http://ellislab.com/forums/viewthread/70361/




Theme © iAndrew 2016 - Forum software by © MyBB