du6333137 2018-07-23 02:04
浏览 71
已采纳

如何在PHP中添加数字以创建唯一的分数并保存它

I am making a 'health program' and you get 1 point per 'healthy' thing you do each day, for example, if you have 30 minutes of exercise you get a point. How it works is shown below:

<form method="post">
    <label>Did you have 30 minutes of exercise today? (+1)</label>
    <select name="question">
    <option value="yes">Yes</option>
    <option value="no">No</option>
  </select> <input type="submit" class="btn btn-outline-light"  name="form1">
</form>
<form method="post">
    <label>Have you drinken 8 glasses of water today? (+1)</label>
    <select name="question">
    <option value="yes">Yes</option>
    <option value="no">No</option>
  </select> <input type="submit" class="btn btn-outline-light"  name="form2">
</form>
<form method="post">
    <label>Did you use your phone for more than two hours today? (-1)</label>
    <select name="question">
    <option value="yes">Yes</option>
    <option value="no">No</option>
  </select> <input type="submit" class="btn btn-outline-light"  name="form3">
</form>



<?php
/* Then using PHP I want to have each form submitted, add or remove 1 from an overall score, something like this: */
$score = 0;
if (isset($_POST['form1'])) {
  $question = $_POST['question'];
  if ($question = "yes") {
    // +1 point
    $score = +1;
  }
}

if (isset($_POST['form2'])) {
  $question = $_POST['question'];
  if ($question = "yes") {
    // +1 point
    $score = +1;
  }
}
if (isset($_POST['form3'])) {
  if ($question = "yes") {
     // -1 point
     $score = +1;
  }
} ?>

Then I want either +1 or -1 from $score. I'm not sure how to do the actual adding of the points using PHP. I want the $score to be updated outside of the if statement otherwise, for each if statement it will add one and $score will still be = to 1

Thanks!

Jacob

</div>
  • 写回答

1条回答 默认 最新

  • dongyied24121 2018-07-23 09:38
    关注

    You have to store the data and can do it in different places. Here's some of them:

    • session
    • cookies
    • server cache
    • browser local storage
    • database
    • ...

    Within a normal website scope, you'd usually go with a database. Each time you submit the form and process the data in the backend of your application (remember that php cannot be execute on the client), you save the data and use it for subsequent data processing.

    It seems to me that javascript would better handle what you're trying to achieve, allowing you to update the interface realtime, bypassing completely the need to send the form to the backend. In this case you can choose to send the total form score to the backend or save it directly to storage accessible from javascript (cookies, local storage, ...). Again, the most common way to do this is as follows:

    1. process data with javascript and update interface
    2. send processed data to backend along with unprocessed data to validate user input (one may "fake" javascript data)
    3. in the server, save data to backend
    4. when rendering the html page, use php with backend values to only show relevant content
    5. repeat

    If it is a must to use php (maybe you are working on an assignment) and cannot use a database, you may want to combine all the answers in one single form:

    <form method="post">
      <div>
        <label>Did you have 30 minutes of exercise today? (+1)</label>
        <select name="question0">
        <option value="yes">Yes</option>
        <option value="no">No</option>
      </div>
      <div>
      </select> <input type="submit" class="btn btn-outline-light"  name="form1">
        <label>Have you drinken 8 glasses of water today? (+1)</label>
        <select name="question1">
        <option value="yes">Yes</option>
        <option value="no">No</option>
      </div>
      <div>
      </select> <input type="submit" class="btn btn-outline-light"  name="form2">
        <label>Did you use your phone for more than two hours today? (-1)</label>
        <select name="question2">
        <option value="yes">Yes</option>
        <option value="no">No</option>
      </select> <input type="submit" class="btn btn-outline-light"  name="form3">
    </div>
    </form>
    
    
    // BackendController.php
    <?php
    $score = 0; // or grab initial value from persistent storage (cookie, db, etc)
    $questions = ['question0', 'question1', 'question2'];
    foreach($questions as $question) {
      $val = $_POST[$question];
      $question == 'yes' ? $score++ : $score--;  
    }
    
    ?>
    

    This way you can send and process all parameters in a single shot, and can also shorten the php script. Still, remember that you will lose the data once the connection is closed. In this case, you may want to save the result to session or cookie and use that retrieved value each next request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题