[PHP] Creating a simple Rating System [Easy]

Discussion in 'Archives' started by photoshopexp3rt, Sep 28, 2010.

[PHP] Creating a simple Rating System [Easy]
  1. Unread #1 - Sep 28, 2010 at 5:01 AM
  2. photoshopexp3rt
    Joined:
    Jan 25, 2009
    Posts:
    620
    Referrals:
    1
    Sythe Gold:
    5

    photoshopexp3rt Forum Addict
    Banned

    [PHP] Creating a simple Rating System [Easy]

    Hey guys, thanks for coming, in this tutorial I will show you how to create a basic and simple to use rating system. I hope it helps ya. :)

    The basics

    We divide the task into the following steps:
    • Display a HTML form where a visitor can select a rating.
    • Read the existing results
    • Check if the actual IP exists among the old results
    • Add valid rating to the result list
    • Display the actual rating


    Displaying the HTML form

    This is an easy part. As usually we create a normal HTML form which is displayed during the first call of the script. As the script itself contains the processing part as well we first check if we really need to display the form or process the submitted data. So the first part of the code looks like this:

    Your rating: 1 2 3 4 5


    Reading the Existing Results

    For the next step we need to process the user input. In this step we initialize some variables to store total ratings, total points, and last, but not least, actual rating. This is quite simple:

    IP Obtaining, locking in ratings.
    Afterwards we retrieve the actual user IP. Later we will check if this IP is already saved in the result list. If there is already a result from the actual IP, then we'll ignore the new one. With this solution we can avoid manipulations of our ratings. So, we need to open the result file and read it into an array using the "file" function. Next, we iterate over this array and summarize the ratings until now. Of course we also check the IP, below will be the coding used for such:

    Code:
    // Read the result file
    $oldResults = file('results/'.$filename.'.txt');
    
    // Summarize total points and rates
    foreach ($oldResults as $value) {
    $oneRate = explode(':',$value);
    // If our IP is in the list then set the falg
    if ($ip == $oneRate[0]) $alreadyRated = true;
    $totalRates++;
    $totalPoints += $oneRate[1];
    }
    ?>
    
    Now, if the rating seems to be valid then we open the result file again and append a new result entry at the end of the file with the actual user IP. Besides this we update the total rating points we have collected in the previous step.


    The Final Stages:
    Lastly, what we want to do is only display the actual rating. We can do that with only displaying the value or we can displaying some graphics like stars to make it a bit nicer.


    Code:
    echo "Actual rating from $totalRates rates is: "
    .substr(($totalPoints/$totalRates),0,3)."
    ";
    
    // Display the actual rating
    for ($i=0;$i<round(($totalPoints/$totalRates),0);$i++){
    echo "";
    }
    ?>

    Thanks for viewing my tutorial, if you liked this one, don't forget to check out my other tutorials including:
     
  3. Unread #2 - Sep 29, 2010 at 5:16 AM
  4. Liquid_stranger
    Joined:
    Sep 13, 2010
    Posts:
    267
    Referrals:
    0
    Sythe Gold:
    0

    Liquid_stranger Forum Addict
    Banned

    [PHP] Creating a simple Rating System [Easy]

    Another greatly detailed and easy to read guide.

    I enjoy reading them(Even if I know what you're explaining!).

    Keep 'em coming, dude!
     
  5. Unread #3 - Sep 29, 2010 at 10:37 AM
  6. photoshopexp3rt
    Joined:
    Jan 25, 2009
    Posts:
    620
    Referrals:
    1
    Sythe Gold:
    5

    photoshopexp3rt Forum Addict
    Banned

    [PHP] Creating a simple Rating System [Easy]

    Hehe thank you.


    I've posted these on a few sites and got some good replies. It's what makes me write more ;). And the fact I enjoy helping others.

    Glad I helped you!
     
  7. Unread #4 - Sep 29, 2010 at 10:55 AM
  8. Mr Smith
    Joined:
    Aug 22, 2010
    Posts:
    874
    Referrals:
    0
    Sythe Gold:
    0

    Mr Smith I am alive.
    Banned

    [PHP] Creating a simple Rating System [Easy]

    Great guide, I learn new stuff everyday, thanks.
     
  9. Unread #5 - Oct 7, 2010 at 6:07 AM
  10. Dr. James
    Joined:
    Sep 17, 2010
    Posts:
    120
    Referrals:
    0
    Sythe Gold:
    0

    Dr. James Active Member
    $5 USD Donor New

    [PHP] Creating a simple Rating System [Easy]

    Ripped you fag.
     
  11. Unread #6 - Oct 7, 2010 at 6:37 AM
  12. Persia_Ugly
    Joined:
    Aug 27, 2008
    Posts:
    2,513
    Referrals:
    0
    Sythe Gold:
    7
    Discord Unique ID:
    696893665490239590
    Discord Username:
    Persia_Ugly

    Persia_Ugly Grand Master
    $5 USD Donor

    [PHP] Creating a simple Rating System [Easy]

    I agree this seems ripped, unless your the admin of the website I seen it on, which I doubt.

    edit:lol its word for word
     
< Arsenal Best Football Team Who Agree's ;) | Report a ripped guide! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site