doushou5761 2018-02-03 15:20
浏览 61
已采纳

计算不同行的多个列的值?

I have a problem in my logic. I have a table which has 10 col. Those 10 col contains 8 col of different question. Each questions is answered by yes and no and each ROW questions have been answered differently(Please look at the table below).I want to calculate marks for One particular row at a time But for some reason when I fetch value I have different title and description of rows but same marks for each rows(Please see the output below).And I am getting 4.5 marks for each row. All I wanted is separate marks for different rows.

MY TABLE STRUCTURE THE OUTPUT

The value of YES IS 1 The value of NO IS 0. and I am multiplying those values with my marking scheme.

I am calculating the value with the help of this code:

<!DOCTYPE html>
<?php
include 'common.php';
$command = "SELECT Title, Description, Question1, Question2, Question3, Question4, Question5, Question6, Question7, Question8  FROM sachjot";
// prepare and executing
$stmt = $dbh->prepare($command);
$result = $stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); // get all result using this

$p; // variable to store all the values of different questions per row

foreach($results as $data){
  $data['Question1'];
  $data['Question2'];
  $data['Question3'];
  $data['Question4'];
  $data['Question5'];
  $data['Question6'];
  $data['Question7'];
  $data['Question8'];


  if ($data['Question1'] == "YES"){
        $p +=  1 * 1;        // 1 is the marking scheme marks
  }else{
    $p = 0 * 1;
  }

  if ($data['Question2'] == "YES"){
    $p +=  + 1 * 0.5;       // 0.5 is the marking scheme marks
  }else{
    $p += 0 * 0.5;
  }

  if($data['Question3'] == "YES"){
    $p += 1 * 2;           // 2 is the marking scheme marks
  }else{
    $p += 0 *2;
  }

  if($data['Question4'] == "YES"){
    $p += 1*1;
  }else{
    $p += 0 * 1;
  }

  if($data['Question5'] == "YES"){
    $p += 1 *1.5;
  }else{
    $p += 0 * 1.5;
  }

  if($data['Question6'] == "YES"){
    $p += 1 * 1;
  }else{
    $p += 0 * 1;
  }

  if($data['Question7'] == "YES"){
    $p += 1 * 1;
  }else{
    $p += 0 *1;
  }

  if($data['Question8'] === "YES"){
    $p += 1 * 2;
  }else{
    $p += 0*2;

}
}
 ?>

And fetching them with the help of this code:

              <?php
        foreach($results as $row){
          // echo "<tr><td>".$row["rank"]."</td>";
          echo "<tr><td>".$row['Title']."</td>";
          echo "<td>".$p."</td>";
          echo "<td>".$row['Description']."</td></tr>";
         }
        ?>
  • 写回答

1条回答 默认 最新

  • dqf98772 2018-02-03 15:34
    关注

    Try with this:

    <?php
    include_once('common.php');
    $command = "SELECT * FROM `sachjot`";
    // prepare and executing
    $stmt = $dbh->prepare($command);
    $result = $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // get all result using this
    
    $p; // variable to store all the values of different questions per row
    
    function getMark($answer, $mark = 1){ // get the answer and the mark (1 is the mark by default, but you can change this. It means that you can simply not pass the parameter if the value is actually 1)
        $result = 0;
        if($answer == 'YES'){
            $result = $mark;
        }
        return $result;
    }
    foreach($results as $data){
        $p = 0;
        $p += getMark($data['Question1'], 1); // provide the answer and the mark
        $p += getMark($data['Question2'], .5);
        $p += getMark($data['Question3'], 2);
        $p += getMark($data['Question4'], 1);
        $p += getMark($data['Question5'], 1.5);
        $p += getMark($data['Question6'], 1);
        $p += getMark($data['Question7'], 1);
        $p += getMark($data['Question8'], 2);
    
        echo '<tr>
            <td>'.$data['Title'].'</td>
            <td>'.$p.'</td>
            <td>'.$data['Description'].'</td>
        </tr>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化