2012-04-09 7 views
0

PHPと2次元連想配列の問題について私は困惑しています。私はPHPのクラスに入っていて、インストラクターはむしろ無知です。PHP変更後の2次元連想配列の値を失う

いくつかの情報でグローバルにするために配列を宣言します。私は配列からのデータを表示するためにhtml形式を使用します。私はhtml形式の "calc"ボタンで配列の2つのヌル値を変更します。すべて素晴らしいです。配列が更新されます。次に、フォームの「注文」ボタンを使用して、配列値を使用してテキストファイルを作成します。これは奇妙な時です。配列内の変更された値はなくなりました。 $ _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

+0

これは、 'key'と' next'を使うよりも簡単な方法です... 'foreach'は同じ目的を果たしました。 – Baba

+0

馬場 - 私は2次元アソークでforeachを使う方法を掴むことに問題がありました。アレイ。それは今朝、最終的にクリックした。私はそれを後方に持っていた。ありがとう。 –

答えて

1

私のクラスでさえも私のプログラミング "GOD"クラスの仲間は、PHPとサーバー側処理の仕組みを教えてくれました。基本的に、処理が完了すると、配列は存続しません。ページがリロードされると、デフォルトの配列がデフォルトのNULL値で再作成されます。私はいくつかの関数を作成し、CalcボタンとOrderボタンの両方の計算を実行し、正しく動作するかどうかを調べます。