PHP划分不起作用

时间:2016-12-24 19:57:38

标签: php html

这是 send.php 文件,我希望它通过表单提交$givenRating,将其除以评分该人的人数({{ 1}}),然后在舍入后将该数据发送到文本文件。但是,所有发生的事情都是在表单框中输入的数字被添加到他们的评级中。

$numberOfRatings

1 个答案:

答案 0 :(得分:0)

仔细检查代码中传递给(Path)方法的字符串file_get_contents()$teacherRating$numberOfRatings。它们似乎完全一样。其次,在任何算术运算中使用它们之前,您可能需要将String中的某些值显式地转换为Integer或Float值。请参阅下面的代码段---

<?php
    $givenRating     = isset($_POST['rating'])  ? intval(trim($_POST['rating']))  : 0;
    $teacher1        = isset($_GET['teacher1']) ? trim($_GET['teacher1'])         : '';
    $tRatingFile     = "../game/teacher-profiles/". $teacher1 . "/rating.txt";
    $numRatingFile   = "../game/teacher-profiles/". $teacher1 . "/ratings.txt";

    // NOTICE/WARNING: THE $tRatingFile & THE $numRatingFile ARE THE SAME
    // IS THIS CORRECT? COMPARE WITH YOUR ORIGINAL POST...
    $teacherRating   = ($data = file_get_contents($tRatingFile))   ? floatval($data) : 0;
    $numberOfRatings = ($data = file_get_contents($numRatingFile)) ? floatval($data) : 0;
    $send_rating     = 0;    //<== INITIALIZE $send_rating TO ZERO: 0

    /*********************************************/
    /*** UNNECESSARY BLOCK: SEE intval() ABOVE ***/
    /*********************************************/
    /*--------------------------------------------
    | settype($teacherRating,   "integer");      |
    | settype($numberOfRatings, "integer");      |
    | settype($givenRating,     "integer");      |
    ---------------------------------------------*/

    $finalRating     = $givenRating/$numberOfRatings;

    if($finalRating > $teacherRating) {
        $send_rating = $teacherRating + $finalRating;
    }
    if($finalRating < $teacherRating) {
        $send_rating = $teacherRating - $finalRating;
    }

    $send_rating     = round($send_rating);
    $file            = fopen($tRatingFile, "w") or die("Unable to open file!");

    fwrite($file, $send_rating);