duan205571 2015-07-12 07:20
浏览 48

从php中的会话数组创建动态表

I am having issues creating a dynamic table from session arrays. The code that I have is working fine but doesn't seem to be updating the arrays with new variables (each "POST" replaces the previous). I am just getting my feet wet with session arrays, any help would be greatly appreciated!

<?php
    session_start();
    $user=array();
    $hours=array();
  $earned=array();
  $name="";
  $hourly=0;
    $tmp=0;

    // if button is pressed, retrieve data
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST['name'];
        $hourly = $_POST['num'];
    $_SESSION['name'] = array();
    array_push($_SESSION['name'],$name);
    $_SESSION['num'] = array();
    array_push($_SESSION['num'],$hourly);
        if($hourly <= 40){
                $tmp = $hourly * 15;
          $_SESSION['earn'] = array();
          array_push($_SESSION['earn'], $tmp);
          //array_push($earned,$tmp);
            }
            elseif($hourly > 40){
                  $tmp = (($hourly-40)*(27.5)+(40*15));
          $_SESSION['earn'] = array();
          array_push($_SESSION['earn'], $tmp);
          //array_push($earned,$tmp);
            }
    $_SESSION['earn'] = array();
    array_push($_SESSION['earn'], $tmp);
    }


?>
<br><br>
<div class="container" align="center">
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" `data-target="#myModal">Insert New</button>`

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;                   </button>`
          <h4 class="modal-title">Insert New Employee</h4>
                <form action="partB.php" method="post">
                <div class="form-group">
                    <input type="text" name="name" class="form-control" id="name" placeholder="Employee Name">
                </div>
                <div class="form-group">
                    <input type="number" name="num" class="form-control" id="num" min="0" placeholder="Hours Worked">
                </div>
                <div>
                    <input type="hidden" name="earn" class="form-control" id="earn">
                </div>
                <button type="submit" name ="submit" id="submit" class="pull-right btn btn-primary">Add</button>
                <button type="button" class="pull-right btn btn-default" data-dismiss="modal">Close</button>
                <button type="reset" class="btn btn-default">Reset</button>
                <div class="clearfix"></div>
          </form>
        </div>
      </div>

    </div>
  </div>

</div>


<table style="width:50%" align="center" id="tab">
  <caption><h2><center>Employee Payroll</center></h2></caption>
  <thead>
    <tr>
    <th><center>Index</center></th>
    <th><center>Hours Worked</center></th>
    <th><center>Weekly Earnings</center></th>
    </tr>
  </thead>
  <tbody>
    <?php   
        foreach($_SESSION['name'] as $keys=>$values){
          echo "<tr><td><center>".($keys+1)."</center></td>";
          foreach($_SESSION['num'] as $keys=>$values){
            echo "<td><center>".$values."</center></td>";
            foreach($_SESSION['earn'] as $keys=>$values){
              echo "<td><center>".$values."</center></td></tr>";
            }
          }
        }
    ?>
  </tbody>
  <tfoot>

  </tfoot>
</table>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douke9379 2015-07-12 07:51
    关注

    The main reason your session is failing is you keep overwriting your array with a new blank array, however, try this rearrangement:

    Create Session Array

    function AddEmployee()
        {
            $name   =   $_POST['name'];
            $hourly =   $_POST['num'];
            // Don't add if the hours are not numbers
            // You could return either an error or false here
            // then make an error message if not added
            if(!is_numeric($_POST['num']))
                return;
            // Since you don't have any more than two scenarios,
            // you can just check for one, the other is default
            $tmp    =   ($hourly <= 40)? $hourly * 15 : (($hourly-40)*(27.5)+(40*15));
            // Assign the values here
            // You can store the name as the key, or make this a numbered array
            // And store name in a new key/value pair
            $_SESSION[$name]['num']     =   $hourly;
            $_SESSION[$name]['earn']    =   $tmp;
    
        }
    
    // if button is pressed, retrieve data
    if($_SERVER["REQUEST_METHOD"] == "POST")
        AddEmployee();
    

    Table display:

    <table style="width:50%" align="center" id="tab">
      <caption><h2><center>Employee Payroll</center></h2></caption>
      <thead>
        <tr>
        <th><center>Index</center></th>
        <th><center>Name</center></th>
        <th><center>Hours Worked</center></th>
        <th><center>Weekly Earnings</center></th>
        </tr>
      </thead>
      <tbody>
        <?php 
            if(is_array($_SESSION)) {
                    $i = 1;
                    foreach($_SESSION as $name => $array){ ?>
                        <tr>
                            <td><?php echo $i; ?></td>
                            <td><?php echo $name; ?></td>
                            <td><?php echo $array['num']; ?></td>
                            <td><?php echo $array['earn']; ?></td>
                        </tr><?php
                      $i++;
                    }
            }
        ?>
      </tbody>
      <tfoot>
    
      </tfoot>
    </table>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备