php rating with text file

Discussion in 'PHP' started by lunkan87, Mar 5, 2014.

  1. #1
    Hi

    Im having a problem writing a php code for a course im taking. We have to make a star rating widget that saves the ratings in a text file. What I got so far is that the ratings is being saved in a text file with each rating on a single row. Now I have to take the numbers in that text file and display the number of votes and the averages sum of the ratings. And I don't know were start can someone please help me?

    this is the code i have so far in the php file.


    <?php
    $score = $_POST['score'];
    $pid = $_POST['pid'];
    
    
    $file = fopen("rating.txt", "r") or exit("Unable to open file!");
    
    if(isset($_POST['score']) && isset($_POST['pid'])) {
        $data = $_POST['score'] . "\r\n";
        $ret = file_put_contents('rating.txt', $data, FILE_APPEND | LOCK_EX);
        if($ret === false) {
            die('There was an error writing this file');
        }
        else {
            echo "$ret bytes written to file";
        }
    }
    else {
       die('no post data to process');
    }
    
    
    $files = file("rating.txt");
    
    ?>
    
    
    Code (markup):
    here is the code i have in the html file

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!--For Raty-->
    <script type="text/javascript" src="js/jquery.raty.min.js"></script>
    
    
    <script type="text/javascript">
    
    $(function() {
    //Below is a path to img folder.
          $.fn.raty.defaults.path = 'js/img'; 
    //Below will activate Raty on div where class is star.     
          $('.star').raty({
        half  : true,
            number: 5,
            score : 0,
            click: function(score, evt) {
    //Below will get id value and store in variable pid               
            var pid=$(this).prop('id');
    //Below will post score and id value to raty1.php page behind the scenes.   
                $.post('raty1.php',{score:score, pid:pid},
                        function(data){
                          
                });
              }
         });
    });
    
    </script>
    
    </head>
    
    <body>
    <img src="img/Desert.jpg" width="400" height="400" alt="Desert"/>
    <div class= "star"></div>
    
    <p>Average rating:</p>
    <div class="average"></div>
    
    </body>
    </html>
    
    Code (markup):
     
    lunkan87, Mar 5, 2014 IP