ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

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

June 27, 2010 3:25am

Subscribe [2]
  • #1 / Jun 27, 2010 3:25am

    TheIgniter

    30 posts

    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 :

    <a href="http://msteigerwalt.com/widgets/ratings/v1.2/">http://msteigerwalt.com/widgets/ratings/v1.2/</a>


    First, i’m french 😛 and here is a simple example (tuto) :
    File to edit :

    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 :

    <input  type="hidden" id="userid" value="<?php echo $row->id ;?>" >

    id=“userid”
    $row->id or wtv.

    ratings.js:

    Line 80 :

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

    .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 :

    alert($('userid').value);

    ratings.php :

    Let’s grab the value via the $_POST method in php :
    Line 235 :

    $r->doVote($_POST['id'], $_POST['vote']);

    add :

    $r->doVote($_POST['id'], $_POST['vote'],$_POST['someid']);

    someid

    add a new parameter to the doVote function :

    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 😊

    ———————————- That all 😊———————————————————————-
    Really easy 😛!


    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 :

    /**
         * 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 :


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

    In the js file :

    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 :

    <?php echo $row->id; ?> //user id 
    
        <?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 😊

    Thanks.

  • #2 / Jun 27, 2010 1:37pm

    TheIgniter

    30 posts

    Help please!

  • #3 / Jun 27, 2010 6:45pm

    pickupman

    1317 posts

    Anytime you are dealing with js & ajax, do yourself a favor and checkout jQuery. 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

    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 / Jun 27, 2010 9:00pm

    TheIgniter

    30 posts

    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

    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 :

    <?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 :

    <a href="http://msteigerwalt.com/widgets/ratings/v1.2/">http://msteigerwalt.com/widgets/ratings/v1.2/</a>

    Thanks dude.

  • #5 / Jun 27, 2010 10:19pm

    pickupman

    1317 posts

    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 / Jun 27, 2010 11:40pm

    TheIgniter

    30 posts

    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:

    <a href="http://orkans-tmp.22web.net/star_rating/index.html#main-menu=4">http://orkans-tmp.22web.net/star_rating/index.html#main-menu=4</a>

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

    Thank you !

  • #7 / Jun 27, 2010 11:54pm

    pickupman

    1317 posts

    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 / Jun 27, 2010 11:59pm

    TheIgniter

    30 posts

    Yeah i agree with you! Can’t wait to implement it on codeigniter 😊!! cya bro and thans again

  • #9 / Jun 30, 2010 12:55pm

    TheIgniter

    30 posts

    Fixed! Check first post, i shared my solution to this problem.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases