dqvj51875 2014-03-25 07:25
浏览 20
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • duanchoupo1104 2014-03-25 08:17
    关注

    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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程