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.

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

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求