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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测