Mootools Rapid Ratings : Passing variable to Php via the Request class [Tuto]

#1
[eluser]TheIgniter[/eluser]
Solution :

After some good time reading the mootools documentation, i found the solution to my problem and here the solution using the rabid rating widget (You can implement it to codeigniter easily :
This the widget :
Code:
http://msteigerwalt.com/widgets/ratings/v1.2/


First, i'm french Tongue and here is a simple example (tuto) :
File to edit :
Code:
demo.php, ratings.php, rating.js

First we are going to add a new field to the rabid_ratings table called user_id.
demo.php :
Put the user id in a hidden input :
Code:
<input  type="hidden" id="userid" value="<?php echo $row->id ;?>" />
id="userid"
$row->id or wtv.

ratings.js:

Line 80 :
Code:
.post({vote:votePercent,id:el.ratableId});

we are going to add a new parameter to the ajax post method and select the input id (id="userid") to grab it's value.
Code:
.post({vote:votePercent,id:el.ratableId,'someid': $('userid').value});
'someid'
If you want to check if the value is passed via ajax, add an alert to print the value :
After line 71 :
Code:
alert($('userid').value);

ratings.php :

Let's grab the value via the $_POST method in php :
Line 235 :
Code:
$r->doVote($_POST['id'], $_POST['vote']);
add :
Code:
$r->doVote($_POST['id'], $_POST['vote'],$_POST['someid']);
someid

add a new parameter to the doVote function :
Code:
function doVote($ratableId, $percent,$data)
...
$userid = $data;
...
$SQL_INSERT_VOTE = "INSERT INTO $this->ratings(ratable_id, ip_address, user_id, ".
            "rating) VALUES ('$id', '$ip', '$userid'  , '$rating');";
user_id

PLEASE CORRECT ME IF THERE IS SOMETHING WRONG Smile

----------------------- That all Smile -----------------------------------------------
Really easy Tongue!


Hi there ,

I really need your help guys, im stuck stuck stuck!! I'm using rabidratings widget! I create a the library and everythings going smooth. Now i want to pass the user id to the doVote function to store the id of the voter :
Code:
/**
     * RabidRatings::doVote
     *
     * This is the function in charge of handling a vote and saving it to the
     * database.
     *
     * NOTE: This method is meant to be called as part of an AJAX request.  As
     * such, it unitlizes the die() function to display its errors.  THIS
     * WOULD BE A VERY BAD FUNCTION TO CALL FROM WITHIN ANOTHER PAGE.
     *
     * @param integer id      - The id of key to register a rating for.
     * @param integer percent - The rating in percentages.
     */
    function doVote($ratableId, $percent) {
         echo $this->profid  ;
        $ip = $_SERVER['REMOTE_ADDR'];
        //Make sure that the ratable ID is a number and not something crazy.
        if (is_numeric($ratableId)) {
            $id = $ratableId;
        } else {
            die("ERROR: Id invalid.");
        }

        //Make sure the percent is a number and under 100.
        if (is_numeric($percent) && $percent < 101) {
            $rating = $percent;
        } else {
            die("ERROR: Rating percent invalid.");
        }

        //Insert the data.
        $SQL_INSERT_VOTE = "INSERT INTO $this->ratings(ratable_id, ip_address, ".
            "rating) VALUES ('$id', '$ip', '$rating');";

        //Die with an error if the insert fails (duplicate IP for a vote).
        if (!mysql_query($SQL_INSERT_VOTE)) {
            die("ERROR: Duplicate votes not allowed.");
        }

        $rating = $this->loadRating($id);
        echo $this->getStarMessage($rating);
    }

I'm not really good in ajax (Im a newbie).. the dovote function is called like :


Code:
if (isset($_POST['vote']) && isset($_POST['id']))
        {
        $this->doVote($_POST['id'], $_POST['vote']);
        }

In the js file :

Code:
window.addEvent('domready', function(e) {
    var rating = new RabidRatings({url:'http://localhost/projectolearn/index.php/users/loadlib'});

HERE : I passed the user id in a var to the RabidRating class, but when the ajax call executed to store the vote, it reset the user id variable...

I get the user id from the view, samething goes to the call vote :

Code:
&lt;?php echo $row->id; ?&gt; //user id

    &lt;?php
    $rr = new RabidRatings() ;
        for ($i = 1; $i < 5; $i++) {
            $rr->showStars("myArticle$i);
           }

What i want is to pass the user id to the doVote function! I passed almost 5 hours to find a solution or a hack! But no way..

Please help me! sorry for my poor english Smile

Thanks.

#2
[eluser]TheIgniter[/eluser]
Help please!

#3
[eluser]pickupman[/eluser]
Anytime you are dealing with js & ajax, do yourself a favor and checkout [url="http://api.jquery.com/jQuery.ajax/"]jQuery[/url]. It will save your life. The tricky part with ajax is that different browsers behave differently. You have to write routines for IE/mozilla/webkit. That's were jQuery comes in as it is cross browser. I don't know if you are trying to reference a js class with
Code:
var rating = new RabidRatings({url:'http://localhost/projectolearn/index.php/users/loadlib'});

You are using similar syntax in other spots using php. The way I have code some simple voting widgets with ajax is creating the form with options. Use jQuery to submit the form with the selected option to the server. The ajax call goes to my CI controller where I check user session, poll id, and vote id. I then submit values to my model if successfully. Send appropriate data back to user once voted has been counted (like results).

#4
[eluser]TheIgniter[/eluser]
Hi pickupman,

Thanks for you reply! I started learning jquery last week but i found that rabidratings (Using mootools javascript framework) is a good widget. i'll try jquery with the five star rating plugins when i feel ready to jquery.

Well, with
Code:
var rating = new RabidRatings({url:'http://localhost/projectolearn/index.php/users/loadlib'});

i'm refering to the rabidratigns library that i create from the default rabidrating class code. The vote work fine, it store all data in the database, what i need is just to pass other variables to the database with the user vote...

This my code :

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Loadlib extends Public_Controller
{
    public function __construct()
    {
        // Call the parent's constructor method
            parent::__construct();
          // Load the required classes
        $this->load->library('RabidRatings');  
        
    
    }
    public function index()
    {
        echo "blah test " ;
    }
};

You can take a look to the rabidrating codes :
Code:
http://msteigerwalt.com/widgets/ratings/v1.2/

Thanks dude.

#5
[eluser]pickupman[/eluser]
You have two options:
1. Add a variable to the js for rabidratings. It has a few default options declared at lines 22-27.
2. When the rating is posted to your controller or library just use the session class or your auth library to add a user id.

On a side note, any particular reason for not using CI's active record (db) class? You are using plain queries.

#6
[eluser]TheIgniter[/eluser]
Thanks dude for your reply! No, no particular reason! I let the rabidrating class as it is. Well, as u told me, i'm going to try jquery with this rate widget:
Code:
http://orkans-tmp.22web.net/star_rating/index.html#main-menu=4

and i'll try what u suggest me for the rabidrating ^_^

Thank you !

#7
[eluser]pickupman[/eluser]
The nice thing about this one is that is a little more fine grained control. You can modify the callback to send your data. Looking at the api example, they use .getJSON to submit the vote. I would recommend using $.post, and you can specify the json data type for the response from the server. CI plays nicer with POST data.

#8
[eluser]TheIgniter[/eluser]
Yeah i agree with you! Can't wait to implement it on codeigniter Smile!! cya bro and thans again

#9
[eluser]TheIgniter[/eluser]
Fixed! Check first post, i shared my solution to this problem.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.