从外部文本文件回显时的数学函数

This is my form where I send rating information to my textfile.txt.

<form name="Star" id="Star">    
    <div id="rating-area" class="shadow">   

    <img src="star-icon.png" id="thumb1" data-value="1" />
    <img src="star-icon.png" id="thumb2" data-value="2" />
    <img src="star-icon.png" id="thumb3" data-value="3" />
    <img src="star-icon.png" id="thumb4" data-value="4" />
    <img src="star-icon.png" id="thumb5" data-value="5" />

    </div>

</form>
<script>


   jQuery('div#rating-area img').click(function(e){
        var val = jQuery(this).data('value') ;
        console.log(val) ;
        jQuery.post('post.php',{ Star : val },function(data,status){
            console.log('data:'+data+'/status'+status) ;
        }) ;
    }) ;

</script>

And this is my php call:

<?php

    $file = file("textfile.txt");
    $file_content = file_get_contents("textfile.txt");
    $file_content_separated_by_dash = explode("-", $file_content);


    echo "Number of votes in file: " . count($file_content_separated_by_dash) . "<br>";
    $sum = 0;

    foreach ($file_content_separated_by_dash as $vote) {
        $sum = $sum + $vote;
    }
    echo "Total: " . $sum;
?>

I don't seems to get the hang of this. When I run the progress to count number of votes in the textfile.txt I do only get the first vote as the total sum. No matter if I reload the page or not. It's still that very first vote that shows up. I've checked the textfile.txt. It updates itself whenever the "star-rating" is "onclick".

As well the "number of votes". To see how many times someone has clicked on it. It stays on 1 through the whole time. What have I missed?

Im still quite a newbie with php. So if I forget any necassary information please tell me and I will try to update the post with that info.

Your textfile content doesn't match with code. Numbers in textfile must be separated with - to work the current code..

If you cant modify your Textfile insertion then change the reading code like this

<?php

    $file = file("textfile.txt");
    $file_content = file_get_contents("textfile.txt");
    $file_content_separated_by_dash = str_split($file_content);


    echo "Number of votes in file: " . count($file_content_separated_by_dash) . "<br>";
    $sum = 0;

    foreach ($file_content_separated_by_dash as $vote) {
        $sum = $sum + intval($vote);
    }
    echo "Total: " . $sum;
?>

Can you post your textfile.txt file? Code seems okay, need to check how data is being saved in text file.

Your file doesn't have -, it has only numbers.

so you can Use this code

$numbers = file_get_contents("textfile.txt");

$count =  strlen($numbers);
echo 'Total votes'.$count;

for($i = 0; $i < count; $i++) {
   echo $numbers[$i] . '<br />';
}