0

我在从会话数组创建动态表时遇到问题。我的代码运行良好,但似乎没有用新变量更新数组(每个“POST”都替换了前一个)。我只是用会话数组弄湿了我的脚,任何帮助将不胜感激!

<?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>
4

1 回答 1

0

您的会话失败的主要原因是您不断用新的空白数组覆盖您的数组,但是,请尝试以下重新排列:

创建会话数组

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 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>
于 2015-07-12T07:51:54.473 回答