PHP と 2 次元の連想配列に関する問題に当惑しています。私は PHP のクラスに参加していますが、インストラクターはかなり無知です。
配列を宣言して、いくつかの情報を使用してグローバルにします。html フォームを使用して、配列からのデータを表示します。HTMLフォームの「計算」ボタンを使用して、配列内の2つのnull値を変更します。すべてがうまく機能します。配列が更新されます。次に、フォームの「注文」ボタンを使用して、配列値を使用してテキスト ファイルを作成します。これは奇妙なときです。配列内の変更された値はなくなりました。$_Post calc If ステートメント内でテキスト ファイルを作成して保存できます。$_Post 送信ボタンを使用した同じテキスト - 古い null 値が返されます。
<!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>