doutan8506 2015-07-12 10:43
浏览 116
已采纳

制作PHP计算器存储和显示历史记录

I am new to PHP and I am trying to create a simple calculator that also displays the history (past calculations) in a div on the page.

You input something like "4+" in the first field and "3" in the second, and it displays "4+3=7" in the div called "results". However, I want to display the whole history of results there, so when I perform a new calculation, both calculations/results will be displayed the results div. What's the best way of doing this in PHP? Would DOMNode::appendChild be an option?

Here is my code this far:

<div id="form">
    <form action="" method="post">
        <input type ="text" name="firstNumber">
        <input type ="text" name="secondNumber">
        <input type ="submit" style="display:none">
    </form>
</div>

Result:

<?php 

    $number1 = $_POST["firstNumber"];
    $number2 = (int)$_POST["secondNumber"];

    @$operator = substr($number1, -1);


switch($operator){
    case '+':
        $result = (int)$number1+$number2;
        echo $result;
        break;
     case '-':
        $result = (int)$number1-$number2;
        echo $result;
        break;
    case '*':
        $result = (int)$number1*$number2;
        echo $result;
        break;
    case '/':
        $result = (int)$number1/$number2;
        echo $result;
        break;
}

?>


<div id="results">
    <?php echo substr($number1,0, -1);
        echo $operator; 
        echo $number2; ?>
 <br>=
    <?php echo $result; ?>
</div>

Thank you for helping out!

  • 写回答

1条回答 默认 最新

  • drkxgs9358 2015-07-12 10:49
    关注

    To store the history server-side, you'll either need a database, like MySQL, a caching server like Memcache or Redis, or you can just store the results in the $_SESSION. Here is a good resource on how to store data in the session. If you store it in $_SESSION, your history will be lost when the user clears their cookies (or logs out, if you implement user accounts).

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c