2012-04-09 95 views
0

我对使用PHP和2-dim关联数组产生的问题感到困惑。我正在上PHP课,而导师则相当无知。PHP在修改后丢失了一个2-diminational关联数组中的值

我声明数组使其具有一些全局信息。我使用html表单来显示数组中的数据。我使用html格式“calc”按钮修改数组中的两个空值。一切都很好。数组已更新。然后我使用表单“order”按钮来使用数组值创建一个文本文件。这是奇怪的时候。数组中的修改值已消失。我可以在$ _Post calc If语句中创建并保存文本文件。使用$ _Post提交按钮的相同文本 - 给我旧的空值。

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> 

<html xmlns='http://www.w3.org/1999/xhtml'> 

<head> 

<title>Online Orders</title> 

</head> 

    <body> 
    <?php 

     function DisplayTable(){ 
      echo "<h2 style ='text-align:center'>Online Order</h2>\n"; 
      echo "<p>Enter the desired items you wish to order:</p>\n"; 

      echo "<form action='OnlineOrders.php' method ='POST'>"; 
      echo "<table style='background-color:lightgray' border='1' width='80%'>";   
      echo "<tr><td width = '10%' style = 'text-align:center'><span style = 'font-weight:bold'>Item</span></td> 
      <td width = '40%' style = 'text-align:center'><span style = 'font-weight:bold'> 
         Description</span></td>"; 
      echo "<td width = '10%' style = 'text-align:center'><span style = 'font-weight:bold'>Price</span></td>"; 
      echo "<td width = '10%' style = 'text-align:center'><span style = 'font-weight:bold'>Quantity</span></td>"; 
      echo "<td width = '10%' style = 'text-align:center'><span style = 'font-weight:bold'>Total</span></td></tr>"; 


      //foreach($products as $item){ 

      global $products; 
      for ($i = 0; $i < count($products); ++$i){ 
       $name = key($products); 
       echo "<tr><td style = 'text-align:left'>" . $name . "</td>"; 
       echo "<td style = 'text-align:left'>" . $products[$name]['Description']. "</td>";  
       echo "<td style = 'text-align:center'>" . $products[$name]['Price']. "</td>"; 
       if($products[$name]['Quantity'] > 0) 
        $value = $products[$name]['Quantity']; 
       else 
        $value = ""; 
       echo "<td style = 'text-align:left'><input type='text' name = 'quantity[" . $name ."]' value = $value></td>"; 
       if($products[$name]['Total'] > 0) 
        echo "<td style = 'text-align:left'>" . $products[$name]['Total']. "</td>"; 
       else 
        echo "<td></td>"; 
       echo "</tr>"; 
       next($products); 
      } 
      echo "</table>"; 
      echo "<p></p>"; 
      echo "<input type ='submit' name='submit' value='Order' />"; 
      echo "<input type ='submit' name='calc' value='Calculate' />"; 
      echo "</form>";  
     } 

     //creates data array 
     $pencils = array("Price" => "1.50", "Description" =>"12pk of #2 pencils", "Quantity" => NULL, "Total" => NULL); 
     $pens = array("Price" => "3.50", "Description" =>"12pk of Bic blue ink pens", "Quantity" => NULL, "Total" => NULL); 
     $paper = array("Price" => "5.50", "Description" =>"6pk of letter-sized, 100 count paper pads", 
        "Quantity" => NULL, "Total" => NULL); 
     $stapler = array("Price" => "6.40", "Description" =>"Streamline stapler - black", "Quantity" => NULL, 
        "Total" => NULL); 
     $staples = array("Price" => "2.00", "Description" =>"Streamline staples, 5000 count", "Quantity" => NULL, 
        "Total" => NULL); 
     $products = array("Pencils" => $pencils, "Pens" => $pens, "Paper" => $paper, "Stapler" => $stapler, 
         "Staples" => $staples); 


//doesn't work right doesn't have updated values 
//Uses the array to create a text file and saves it to the hard drive in a folder entitled "/OnlineOrders" 
    if(isset($_POST['submit'])){ 
     global $products; 
     $Dir = "OnlineOrders"; 
      if (is_dir($Dir)){ 
       echo "Products in order"; 
       //print_r($products); //prints out the array as it was declared not modified! 
       $SaveString = "Online Order\r\n\r\n"; 
       $SaveString .= date('m/d/Y') . "\r\n\r\n"; 
       $SaveString .= "Item\t\tPrice\t Quantity\tTotal\r\n"; 
       for ($i = 0; $i < count($products); ++$i){ 
        $name = key($products); 
       // if ($products[$name]['Quantity']>0){ 
         $SaveString .= $name ."\t\t" . $products[$name]['Price'] ."\t\t". $products[$name]['Quantity'] . 
             "\t" . $products[$name]['Total'] ."\r\n\r\n";       

       // } 
       // else 
       //  echo "Nothing to write to file"; 
        next($products); 
       } 
       $TimeStamp = date('Y_m_d_G_i'); 
       $SaveFileName = "$Dir/order.$TimeStamp.txt"; 
       $fp = fopen($SaveFileName, "wb"); 
       if ($fp === FALSE){ 
        echo "There was an error creating \"" . htmlentities($SaveFileName) . "\".<br />\n";      
       } 
       else { 
        if (flock($fp, LOCK_EX)) { 
         if (fwrite($fp, $SaveString)>0) 
          echo "Successfully wrote to file \"" . htmlentities($SaveFileName) . "\".<br />\n"; 
         else 
          echo "There was an error writing to file \"" . htmlentities($SaveFileName) . "\".<br />\n"; 
         flock($fp, LOCK_UN); 
        } 
        else{ 
         echo "There was an error locking file \"" . htmlentities($SaveFileName) . 
          " for writing\".<br />\n"; 
        } 
        fclose($fp); 
       } 
      } 
     echo "<p><a href='OnlineOrders.php'>Order Again?</p>\n"; 
    } 
    //enter values into the quantity input items and addes qty and total to array and redisplays the html table with updated values 
    else if(isset($_POST['calc'])){ 
     global $products; 
     $quantity = $_POST['quantity']; 
     if(is_array($quantity)){ 
      foreach ($quantity as $item => $value){ 

       if($value <> NULL){ 
        $amount = stripslashes($value); 
        if(is_numeric($amount) && ($amount > 0)){ 
         $products[$item]['Quantity'] = $amount; 
         $products[$item]['Total'] = ($products[$item]['Price'] * $amount); 
        } 
        else 
         echo "<p>You must enter a number greater than 0 for $item's quantity</p>\n"; 
       } 
      } 

      DisplayTable(); 
      //print_r($products); //TEST - PRINTS OUT THE ARRAY WITH UPDATED VALUES CORRECTLY 


//TESTING STARTS HERE - SAME CODE FROM submit button and it works 
     $Dir = "OnlineOrders"; 
      if (is_dir($Dir)){ 
       $SaveString = "Online Order\r\n\r\n"; 
       $SaveString .= date('m/d/Y') . "\r\n\r\n"; 
       $SaveString .= "Item\t\tPrice\t Quantity\tTotal\r\n"; 
       reset($products); 
       for ($j = 0; $j < count($products); ++$j){ 
        $name = key($products); 
        if ($products[$name]['Quantity']>0){ 
         $SaveString .= $name ."\t\t" . $products[$name]['Price'] ."\t\t". $products[$name]['Quantity'] . 
             "\t" . $products[$name]['Total'] ."\r\n"; 
        } 
        next($products); 
       } 
       $TimeStamp = date('Y_m_d_G_i'); 
       $SaveFileName = "$Dir/order.$TimeStamp.txt"; 
       $fp = fopen($SaveFileName, "wb"); 
       if ($fp === FALSE){ 
        echo "There was an error creating \"" . htmlentities($SaveFileName) . "\".<br />\n";      
       } 
       else { 
        if (flock($fp, LOCK_EX)) { 
         if (fwrite($fp, $SaveString)>0) 
          echo "Successfully wrote to file \"" . htmlentities($SaveFileName) . "\".<br />\n"; 
         else 
          echo "There was an error writing to file \"" . htmlentities($SaveFileName) . "\".<br />\n"; 
         flock($fp, LOCK_UN); 
        } 
        else{ 
         echo "There was an error locking file \"" . htmlentities($SaveFileName) . 
          " for writing\".<br />\n"; 
        } 
       fclose($fp); 
       } 
      } 
//Testing Ends Here 
     } 
    } 
    else 
     DisplayTable(); 
    ?> 
    </body> 

</html> 
+0

请提供一个更简单的例子。这是很多代码和一个相当模糊的解释。这听起来像你期望数组的变化持续在不同的请求之间。如果是这样,那不会奏效。 – deceze 2012-04-09 04:33:58

+0

他们更简单的方式来处理数组,比使用'key'和'next' ... foreach'可以达到同样的目的 – Baba 2012-04-09 10:15:41

+0

Baba - 我在处理如何使用2-dim关联的foreach时遇到了问题。阵列。它今天早上终于点击了。我倒退了。谢谢。 – 2012-04-09 14:15:56

回答

1

我的程序设计“上帝”同学甚至没有在我的课堂上教我如何PHP和服务器端处理工作。基本上,一旦你完成处理,阵列不会活着。当页面重新加载时,默认数组是用默认的NULL值重新创建的。我创建了几个函数,并为Calc按钮和Order按钮运行计算,并使其正确工作。