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 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2008
    Location
    Finland
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Item rating system with flat files

    Hi,

    I'm wondering how to create a flat file based item rating system that has ratings from 1 point to 5 points. Finally, the script would echo the average rating of the item and displays the amount of votes. It also allows to cast one vote per ip address. I don't have a clue how to do this, so any help concerning the issue would be appreciated.

    Here's my start:

    ITEM (e.g. picture)

    Code:
    <form method="post" action="rate.php"> 
    <select name="rating">
    <option>1 point - Terrible</option>
    <option>2 points - Bad</option>
    <option>3 points - Fair</option>
    <option>4 points - Good</option>
    <option>5 points - Excellent</option>
    </select>
    <input type="submit" value="Rate!" />
    </form>
    Last edited by T.K.; 03-02-2009 at 05:18 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Any compelling reason you won't be able to use a relational database (MySQL for example)?

  • #3
    New Coder
    Join Date
    Jan 2008
    Location
    Finland
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts
    My web hotel service provider don't offer a relational database as a free attribute.
    Last edited by T.K.; 03-02-2009 at 09:09 PM.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Alright well it's all certainly very possible to do this using flat files, but it will be kludgy. You'll need to become familiar with how to open, read, write, append and rewrite flat files.

    http://us2.php.net/manual/en/ref.filesystem.php

    The general design will consist of functions to: 1. add a new rating (open and append), and 2. display a rating (open and read).

    The unique IP address portion is tough to do without a login system and should be a separate discussion (search this forum, it's been discussed many times before).

  • #5
    New Coder
    Join Date
    Jan 2008
    Location
    Finland
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok, this is what I came up with:

    Code:
    <html>
    <head>
    
    <title>Rating script</title>
    
    
    </head>
    
    
    <body>
    
    <p>Rate this paragraph!</p>
    
    <form method="post" action="rate_index.php">
    <select name="rating">
    <option>Terrible</option>
    <option>Bad</option>
    <option>Fair</option>
    <option>Good</option>
    <option>Excellent</option>
    <input type="submit" name="submit" value="Rate!" />
    </form>
    
    <?php
    
     $Rates = $_POST["rating"];
     
     $File1 = "rate_data.php";
     $Handle = fopen($File1, 'a+');
     fwrite($Handle, $Rates."\n");
     fclose($Handle);
    
     $File2 = "rate_data.php";
     $Lines = file($File2);
     $Count = count($Lines);
     echo "Total rates: ".$Count.".<br/><br/>";
    
    ?> 
    
    </body>
    </html>
    The code above writes a selected rating into a remote file and counts the number of total ratings. My solution may be rough, but I'm just a newbie with programming.

    Now I ought to know how to count the rating which has most votes and finally to display it. I've already spent several hours googlin' around to find out how to implement that. Any guide to right direction would be more than helpful
    Last edited by T.K.; 03-03-2009 at 05:37 PM.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are in luck, because there is a PHP function to do just that. It's called array_count_values().

    http://us3.php.net/manual/en/functio...unt-values.php

    It will produce an array that uses your ratings as the index names and the number of times they occur as the values. You then just need to sort that array descending to pop the most common rating to the top.

    http://us3.php.net/manual/en/function.rsort.php

  • #7
    New Coder
    Join Date
    Jan 2008
    Location
    Finland
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks! I just found the same function by myself, heh.

  • #8
    New Coder
    Join Date
    Jan 2008
    Location
    Finland
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Well, I searched the forum and found nothing really useful than the information that I should avoid using cookies in preventing a user submitting the form twice (in case the user deletes them). So, maybe I ought to try with ip. I do know how to get user's ip address but need to be aware how to insert it into an array and then execute a query (maybe using "in_array" function) if the ip already exists in the array and then disallow resubmission.

    Here's the current code I'm using:

    Code:
    THE HTML FORM:
    
    <form method="post" action="rate.php" name="form1">
    <legend>RATE THIS ITEM</legend>
    <select name="rating">
    <option>Terrible</option>
    <option>Bad</option>
    <option>Fair</option>
    <option>Good</option>
    <option>Excellent</option>
    
    <input type="submit" name="submit" value="Rate!" />
    </form>
    
    And the PHP:
    
    <?php
    
     $File1 = file_get_contents("rate_data.php");
     $Words = (array_count_values(str_word_count(strtolower($File1),1)));
     arsort($Words);
     
     if($File1 == NULL) {
     echo "<p>Currently no ratings.</p>";
     }
     else {
     echo "<p><b>Rated:</b> ".key($Words)."</p>";
     }
     
     echo "<p><b>Vote statistics:</b></p>";
     if($File1 == NULL) {
     echo "<p>No stats available.</p>";
     }
     else {
     $File3 = file_get_contents("rate_data.php");
     $Words = (array_count_values(str_word_count(strtolower($File3),1)));
     arsort($Words);
     foreach ($Words as $key => $val) {
     echo "<p>$key = $val votes.</p>\n";
     }
     }
     
     $File2 = "rate_data.php";
     $Lines = file($File2);
     $Count = count($Lines);
     echo "<p><b>Total rates:</b> ".$Count.".</p>";
     
    ?>
    Here's the source code of the rate.php:

    Code:
    <?php
    
     $URL="rate_index.php";
    
     header ("Location: $URL");
     
     $Rates = $_REQUEST["rating"];
     $IP = $_SERVER["REMOTE_ADDR"];
     
     $File1 = "rate_data.php";
     $Handle = fopen($File1, 'a+');
     fwrite($Handle, $Rates."\n");
     fclose($Handle);
     
    ?>
    Thanks in advance for your help.
    Last edited by T.K.; 03-06-2009 at 04:44 PM.

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can see the IP address in the global variable $_SERVER['REMOTE_ADDR']. This value comes through the header and can easily be spoofed by the technically savvy ones. The level of spoofage will be directly related to your user's demographics; are they computer geeks, or grandmas? How much advantage will they gain by cheating the rating system? Is there money or status to be gained by cheating the ratings? These factors will mold your system.

  • #10
    New Coder
    Join Date
    Jan 2008
    Location
    Finland
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I changed the script that it will ask a reviewer's email address and then compares it to the the data file if it already exists. So everyone can rate only once with a valid email. Of course it doesn't stop people to do multiple rates with different email addresses, but as the script is for people to rate different lures (the website is a fishing portal), not a "bigger" question, I hardly think that many people would bother to register (if doesn't already possess) several email accounts to rate more than once. However, any suggestions how to make it more reliable (for someone would yet cheat the system)?
    Last edited by T.K.; 03-07-2009 at 11:57 AM.


  •  

    Posting Permissions

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