Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts

    PHP/Ajax Rater Possible?

    Hi,
    I'm looking for a ready-made script that would allow me to host the rating device on one site and use it on another via PHP + Ajax. I'm thinking there must be something like this out there already. Basically something where I can host a script that would store the ids/ratings into a database and then have other websites that want to use the script simply use a javascript that would make calls to the php script residing on the server.

    I can whip up something but it'll take a much longer time, ..anyone know if such a script exists or have any advice on how to begin?

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

  • #3
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I couldn't find anything even remotely usable. I created the Ajax interface, all I need is the backend, and I'm really really inexperienced with MySQL.

    Are there any php/sql whizzes that could write a very simple script that would do this: When given a rating (between 1-5) would add it to a table called "votes" given the id.

    Basically something like this,
    Code:
    ?id=1&rating=5
    In a table called votes, it looks for id=1 (if it doesn't exist, it inserts a new record with that ID) and the insert the rating=5, or add the rating to whatevers in there already. Maybe this is too much to do, I have no clue..I've written the entire frontend with ajax and over time I'll release it open-source..I just hope that someone can code something simple to just store and update the db.

    I've looked around and all I can find are bloated rating scripts and tutorials that have too much garbage.

  • #4
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I did it myself, it turned out pretty messy....worked well in firefox, but would not work at all in Internet Explorer - when the user clicked on the rating, it would pop open a new window in IE (without voting).

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    hey I just built one of these the other day...

    getURL IE 7 Download Bug ???

    Check this link out. On the main page you will see (rate here) once you click that you will get 5 rating icons, click one and it cast the rating. If you were to double click it after you've voted then you can update your vote. Also feel free to help me in that thread as well .
    Last edited by StupidRalph; 08-09-2007 at 10:42 AM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #6
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    hey it works, not bad. You releasing the source though? lol I mean mines in really bad shape because it won't work in IE, BTW double clicking on it doesn't work for me, but hey at least it lets me vote.

  • #7
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    I just sent you a PM with the link. I'll transition the code to be more abstract so I can package it and allow it to be used by others. As for right now, any help you need with installing it...just ask.

    P.S.- What browser are you using? It has worked on Opera 9, IE 6, IE 7, Firefox 1.5, Firefox 2.0,Avant and Safari (on Macintosh). Are you using Netscape by chance? I must install that on my system when I get a chance.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #8
    New Coder
    Join Date
    Aug 2007
    Location
    Seattle, WA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This would work if the articles/blahh where stored in a mysql database.

    arcticle.php?id=x --
    Code:
    <?php
    //mysql connect
    
    //select article from DB
    $id = $_GET['id'];
    $result = mysql_query("SELECT * FROM articles WHERE id='$id' LIMIT 1")or die(mysql_error());
    $row = mysql_fetch_array($result);
    echo $row['article'];
    echo "<center>";
    //display form
    echo "<b>Rate This Article</b><br>";
    echo "<form method=post action=vote.php>";
    echo "Rating <select name=rating><option style='background-color: red; color: white' value='1'>Bad - 1<option style='background-color: orange; color: white' value='2'>Blah - 2<option style='background-color: green; color: black' value='3'>Blah - 3<option style='background-color: lightgreen; color: black' value='4'>Blah - 4<option style='background-color: limegreen; color: black'value='5'>Blah - 5</select><br><input type='hidden' value='".$_GET['id']."' name='article'><input type=submit value=Rate></form>
    vote.php --
    Code:
    <?php
    //mysql connect
    
    //fetch POST'd vars
    $rating = $_POST['rating'];
    $article = $_POST['article'];
    
    //Local IP address
    $ip = $_SERVER['REMOTE_ADDR'];
    
    //See if IP address has voted -- If result proves positive, die, else, continue
    $result = mysql_query("SELECT * FROM votes WHERE article='$article' AND ip='$ip' LIMIT 1")or die(mysql_error());
    $row = mysql_fetch_array($result);
    if(isset($row['article'])){
    echo "<center><b>You've already voted!</b></center>";
    }else{
    mysql_query("INSERT INTO votes (article, ip, rating)VALUES('$article', '$ip', '$rating') ")or die(mysql_error());
    header("Location: article.php?id=$article");
    }
    
    /*MYSQL TABLES
    article {
    article - Longtext;
    id - AUTO_INCREMENT int(255);
    }
    
    votes{
    rating - INDEX int(1);
    ip - varchar(50);
    article - int(255);
    }
    */
    There's an example. I can recode it in a better format (ie. so it's all on one page) and stuff like that.

  • #9
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    this is my example, sorry i had it ready a couple of days ago but totally forgot to post it up for you. It has ajax as well so it does not have to reload the page.

    test.php:
    PHP Code:
    <html>
    <
    head>
    <
    title>Rate me</title>
    <
    script type="text/javascript" src="ajax.js"></script>
    </head>

    <body onload="Rate('0')">
    <input type="button" value="1" onclick="Rate('1')" /><input type="button" value="2" onclick="Rate('2')" /><input type="button" value="3" onclick="Rate('3')" /><input type="button" value="4" onclick="Rate('4')" /><input type="button" value="5" onclick="Rate('5')" />
    <br />Your Rating:
    <div id="rating"></div>
    Total amount of ratings:
    <div id="amount"></div>
    </body>
    </html> 
    ajax.js:
    Code:
    function createRequestObject() {
    var xmlHttp;
      try
       {
         // Firefox, Opera 8.0+, Safari
         xmlHttp=new XMLHttpRequest();
       }
      catch (e)
       {
         // Internet Explorer
       try
        {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
       catch (e)
        {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
       }
      return xmlHttp;
    }
    
    var http = createRequestObject();
    
    function Rate(action) {
        http.open('get', 'rate.php?rating='+action, true);
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
    
    function handleResponse() {
        if(http.readyState == 4){
            var response = http.responseText;
            var elements = new Array();
            elements = response.split("|");
            document.getElementById('rating').innerHTML = elements[1];
            document.getElementById('amount').innerHTML = elements[0];
        }
    }
    and rate.php
    PHP Code:
    <?php
    $rates 
    0;
    $rating 0;
    $connect mysql_connect("my_server""my_user""my_pass");
    mysql_select_db("rate"$connect);

    if(
    $_GET["rating"] != 0)
    {
    $rating $_GET["rating"];

    $sql "INSERT INTO rating (rate) VALUES ('$rating')";
    mysql_query($sql);
    }

    $sql "SELECT * FROM rating";
    $result mysql_query($sql);

    while(
    $rate mysql_fetch_array($result)) {
      
    $rates $rates 1;
    }
    mysql_close($connect);
    echo 
    "$rates|$rating";
    ?>
    of course this is just an example, if i wanted it to be really fancy I would have images, e.g. stars, instead of buttons and do a nice mouseover effect to show the user how many stars they are rating it. also I would add extra rows to the table rate, like id and user so you can link the rates to the right object and only allow a user to rate on a particular item once


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •