douna2014 2018-05-10 06:49
浏览 43

如何使用PHP根据类别计算得分

Users input are divided into 3 categories which are DEPRESSION, STRESS and ANXIETY. I want to calculate users answer_value according to the categories. Array for depression will be calculated seperately same goes with array for anxiety and stress. Can someone please help :(

TABLES

usersTbl (user_id, first_name, last_name, email, pwd);
questionTbl (question_id, question_no, ques_category, question_desc);
answerTbl (answer_id, user_id, question_no, answer_value);
recordTbl (record_id, user_id, depression_score, stress_score, anxiety_score);

question.php

 <?php
 include 'includes/test.inc.php';?><br><br>

 <form  action="submit_answer.php" method="POST">
 <center>
 <table>
 <tr>
 <th><center>Question</center></th>
    <th><center>0</center></th>
    <th><center>1</center></th>
    <th><center>2</center></th>
    <th><center>3</center></th>
 </tr>
 <tr>
    <td>I found it hard to wind down.</td>
    <td><center><input id="stress11" name="stress1" type="radio" value="0"/></center></td>
    <td><center><input id="stress12" name="stress1" type="radio" value="1"/></center></td>
    <td><center><input id="stress13" name="stress1" type="radio" value="2"/></center></td>
    <td><center><input id="stress14" name="stress1" type="radio" value="3"/></center></td>
</tr>
<tr>
    <td>I was aware of the dryness of my mouth.</td>
    <td><center><input id="anxiety11" name="anxiety1" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety12" name="anxiety1" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety13" name="anxiety1" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety14" name="anxiety1" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I couldn't seem to experience any positive feeling at all.</td>
    <td><center><input id="depression11" name="depression1" type="radio" value="0"/></center></td>
    <td><center><input id="depression12" name="depression1" type="radio" value="1"/></center></td>
    <td><center><input id="depression13" name="depression1" type="radio" value="2"/></center></td>
    <td><center><input id="depression14" name="depression1" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I experienced breathing difficulty(i.e excessively rapid breathing,<br>breathlessnes in the absence of physical exertion)</td>
    <td><center><input id="anxiety21" name="anxiety2" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety22" name="anxiety2" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety23" name="anxiety2" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety24" name="anxiety2" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I found it difficult to work up on the initiative to do things</td>
    <td><center><input id="depression21" name="depression2" type="radio" value="0"/></center></td>
    <td><center><input id="depression22" name="depression2" type="radio" value="1"/></center></td>
    <td><center><input id="depression23" name="depression2" type="radio" value="2"/></center></td>
    <td><center><input id="depression24" name="depression2" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I tended to over-react to situations.</td>
    <td><center><input id="stress21" name="stress2" type="radio" value="0"/></center></td>
    <td><center><input id="stress22" name="stress2" type="radio" value="1"/></center></td>
    <td><center><input id="stress23" name="stress2" type="radio" value="2"/></center></td>
    <td><center><input id="stress24" name="stress2" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I experienced trembling (i.e. in the hands)</td>
    <td><center><input id="anxiety31" name="anxiety3" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety32" name="anxiety3" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety33" name="anxiety3" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety34" name="anxiety3" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt that I was using a lot of nervous energy.</td>
    <td><center><input id="stress31" name="stress3" type="radio" value="0"/></center></td>
    <td><center><input id="stress32" name="stress3" type="radio" value="1"/></center></td>
    <td><center><input id="stress33" name="stress3" type="radio" value="2"/></center></td>
    <td><center><input id="stress34" name="stress3" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I was worried about situations in which I might panic and make a fool of myself.</td>
    <td><center><input id="anxiety41" name="anxiety4" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety42" name="anxiety4" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety43" name="anxiety4" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety44" name="anxiety4" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt that I had nothing to look forward to.</td>
    <td><center><input id="depression31" name="depression3" type="radio" value="0"/></center></td>
    <td><center><input id="depression32" name="depression3" type="radio" value="1"/></center></td>
    <td><center><input id="depression33" name="depression3" type="radio" value="2"/></center></td>
    <td><center><input id="depression34" name="depression3" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I found myself getting agitated.</td>
    <td><center><input id="stress41" name="stress4" type="radio" value="0"/></center></td>
    <td><center><input id="stress42" name="stress4" type="radio" value="1"/></center></td>
    <td><center><input id="stress43" name="stress4" type="radio" value="2"/></center></td>
    <td><center><input id="stress44" name="stress4" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I found it difficult to relax.</td>
    <td><center><input id="stress51" name="stress5" type="radio" value="0"/></center></td>
    <td><center><input id="stress52" name="stress5" type="radio" value="1"/></center></td>
    <td><center><input id="stress53" name="stress5" type="radio" value="2"/></center></td>
    <td><center><input id="stress54" name="stress5" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt down-hearted and blue.</td>
    <td><center><input id="depression41" name="depression4" type="radio" value="0"/></center></td>
    <td><center><input id="depression42" name="depression4" type="radio" value="1"/></center></td>
    <td><center><input id="depression43" name="depression4" type="radio" value="2"/></center></td>
    <td><center><input id="depression44" name="depression4" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I was intolerant of anything that kept me from getting on with what I was doing.</td>
    <td><center><input id="stress61" name="stress6" type="radio" value="0"/></center></td>
    <td><center><input id="stress62" name="stress6" type="radio" value="1"/></center></td>
    <td><center><input id="stress63" name="stress6" type="radio" value="2"/></center></td>
    <td><center><input id="stress64" name="stress6" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt that I was so close to panic.</td>
    <td><center><input id="anxiety51" name="anxiety5" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety52" name="anxiety5" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety53" name="anxiety5" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety54" name="anxiety5" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I was unable to become enthusiastic about anything.</td>
    <td><center><input id="depression51" name="depression5" type="radio" value="0"/></center></td>
    <td><center><input id="depression52" name="depression5" type="radio" value="1"/></center></td>
    <td><center><input id="depression53" name="depression5" type="radio" value="2"/></center></td>
    <td><center><input id="depression54" name="depression5" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt that I wasnt worth as much as a person.</td>
    <td><center><input id="depression61" name="depression6" type="radio" value="0"/></center></td>
    <td><center><input id="depression62" name="depression6" type="radio" value="1"/></center></td>
    <td><center><input id="depression63" name="depression6" type="radio" value="2"/></center></td>
    <td><center><input id="depression64" name="depression6" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt that I was rather touchy.</td>
    <td><center><input id="stress71" name="stress7" type="radio" value="0"/></center></td>
    <td><center><input id="stress72" name="stress7" type="radio" value="1"/></center></td>
    <td><center><input id="stress73" name="stress7" type="radio" value="2"/></center></td>
    <td><center><input id="stress74" name="stress7" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I was aware of the action of my heart in the absence of physical exertion(i.e. sense of heart rate increases)</td>
    <td><center><input id="anxiety61" name="anxiety6" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety62" name="anxiety6" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety63" name="anxiety6" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety64" name="anxiety6" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt scared without any good reason.</td>
    <td><center><input id="anxiety71" name="anxiety7" type="radio" value="0"/></center></td>
    <td><center><input id="anxiety72" name="anxiety7" type="radio" value="1"/></center></td>
    <td><center><input id="anxiety73" name="anxiety7" type="radio" value="2"/></center></td>
    <td><center><input id="anxiety74" name="anxiety7" type="radio" value="3"/></center></td>
  </tr>
  <tr>
    <td>I felt that life was meaningless.</td>
    <td><center><input id="depression71" name="depression7" type="radio" value="0"/></center></td>
    <td><center><input id="depression72" name="depression7" type="radio" value="1"/></center></td>
    <td><center><input id="depression73" name="depression7" type="radio" value="2"/></center></td>
    <td><center><input id="depression74" name="depression7" type="radio" value="3"/></center></td>
  </tr>

</table><br>
<button type="submit" value="insert" name="submit">submit</button>
</center>
</form>
?>

submit_answer.php

<?php
session_start();

if (isset($_POST['submit'])) {

include_once("C:/xampp/htdocs/questionnaire/includes/dbh.inc.php");


$answers = array();


$uid = $_SESSION['u_id'];

// Fill the answers array with answers

$answers[1] = $_POST['stress1'];
$answers[2] = $_POST['anxiety1'];
$answers[3] = $_POST['depression1'];
$answers[4] = $_POST['anxiety2'];
$answers[5] = $_POST['depression2'];
$answers[6] = $_POST['stress2'];
$answers[7] = $_POST['anxiety3'];
$answers[8] = $_POST['stress3'];
$answers[9] = $_POST['anxiety4'];
$answers[10] = $_POST['depression3'];
$answers[11] = $_POST['stress4'];
$answers[12] = $_POST['stress5'];
$answers[13] = $_POST['depression4'];
$answers[14] = $_POST['stress6'];
$answers[15] = $_POST['anxiety5'];
$answers[16] = $_POST['depression5'];
$answers[17] = $_POST['depression6'];
$answers[18] = $_POST['stress7'];
$answers[19] = $_POST['anxiety6'];
$answers[20] = $_POST['anxiety7'];
$answers[21] = $_POST['depression7'];


// loop through the answers array

for ($i=1;$i<22;$i++) {

    // $i contains the ques_no (1,2,3,4,5,....)
    // $answers[$i] contains the answer value

    $answer = $answers[$i];


    // Let's control the user input

    switch ($answer){
        case 0:
        case 1:
        case 2:
        case 3:
            // OK
            break;
        default:
            // This is abnormal, we might face a hacker, lets reset the answer to nothing.
            $answer = '';
    }


    // all done, ready to insert

    $sql = "INSERT INTO record (user_id, question_no, answer_value)
            VALUES ('$uid', '$i', '$answer');";

    mysqli_query($conn, $sql);
}

exit ();
}
else {
header("Location: ../index.php");
exit ();
}
?>
  • 写回答

2条回答 默认 最新

  • doujia7779 2018-05-10 08:12
    关注

    I hope this queries will be work for you.

    SELECT usersTbl.user_id, usersTbl.first_name, usersTbl.last_name, usersTbl.email
        , SUM(COALESCE(CASE WHEN questionTbl.ques_category = 'DEPRESSION' THEN answerTbl.answer_value END,0)) depression_score
        , SUM(COALESCE(CASE WHEN questionTbl.ques_category = 'STRESS' THEN answerTbl.answer_value END,0)) stress_score
        , SUM(COALESCE(CASE WHEN questionTbl.ques_category = 'ANXIETY' THEN answerTbl.answer_value END,0)) anxiety_score
    FROM usersTbl
        Left JOIN answerTbl ON answerTbl.user_id = usersTbl.user_id,
        Left JOIN questionTbl ON questionTbl.question_id = answerTbl.question_no,
    GROUP  
        BY usersTbl.user_id;
    

    If you have any doubt.Please post your comment on comment section

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示