我对 PHP 和 2-dim 关联数组的问题感到困惑。我正在上 PHP 课程,而讲师却一无所知。
我声明该数组以使其具有一些信息的全局性。我使用 html 表单来显示数组中的数据。我使用 html 表单“计算”按钮修改了数组中的两个空值。一切都很好。数组已更新。然后,我使用表单“订单”按钮使用数组值创建一个文本文件。这是它变得奇怪的时候。数组中的修改值消失了。我可以在 $_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>