2015-07-12 67 views
0

我在從會話數組創建動態表時遇到問題。我有的代碼工作正常,但似乎沒有用新變量更新數組(每個「POST」取代了以前的)。我只是用會話陣列弄溼我的腳,任何幫助將不勝感激!從會話數組在php中創建動態表格

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

回答

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>