我有这个任务,我整个周末一直在努力。
这是作业的范围,
我坚持更新表格,因为每次我点击表格上的另一个“+”按钮时,其他表格行反向回到原始状态,我不希望这样。我想在点击桌子上的另一个“+”按钮后保留他们的值。
我想要使用的逻辑是“在每次单击各种'+'和' - '按钮后更新整个多维数组,并将'updated'数组显示回表中。但是我在实现这个逻辑时遇到问题或者是他们的另一个逻辑可以解决这个问题吗?
到目前为止,这是我的代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
$ShowForm = True;
$items = array(
"Product 1" => array
(
"Qty" => 0,
"Item" => "Product 1",
"Unit Price" => 5,
"Subtotal" => 0
),
"Product 2" => array
(
"Qty" => 0,
"Item" => "Product 2",
"Unit Price" => 2.3,
"Subtotal" => 0
),
"Product 3" => array
(
"Qty" => 0,
"Item" => "Product 3",
"Unit Price" => 3.54,
"Subtotal" => 0
),
"Product 4" => array
(
"Qty" => 0,
"Item" => "Product 4",
"Unit Price" => 1.67,
"Subtotal" => 0
),
"Product 5" => array
(
"Qty" => 0,
"Item" => "Product 5",
"Unit Price" => 3.4,
"Subtotal" => 0
)
);
?>
<h3>Update the products below as required.<br>Then you can click on the checkout button below.</h3>
<form method="POST" action= "<?php $_SERVER['PHP_SELF']; ?>" >
<?php
echo '<table border = 1 id=\'products\'>';
echo '<th>Qty</th>';
echo '<th>Item</th>';
echo '<th>Unit Price</th>';
echo '<th>Subtotal</th>';
$i = count($items);
foreach ($items as $key => $value) {
echo'<tr class=\'' . $key . '\'>';
$i--;
if ($key === "Product 1") {
$product1 = array();
foreach ($value as $dim => &$num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add4'])) {
$newQty = ($_POST['qty4']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty4\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove4\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
$value['Qty'] = $newQty;
}
if (ISSET($_POST['remove4'])) {
$newQty = ($_POST['qty4']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty4\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove4\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 2") {
foreach ($value as $dim => &$num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add3'])) {
$newQty = ($_POST['qty3']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty3\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove3\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
$value['Qty'] = $newQty;
}
if (ISSET($_POST['remove3'])) {
$newQty = ($_POST['qty3']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty3\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove3\'> </td>';
}
//
}
}elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 3") {
foreach ($value as $dim => $num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add2'])) {
$newQty = ($_POST['qty2']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty2\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove2\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
}
if (ISSET($_POST['remove2'])) {
$newQty = ($_POST['qty2']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty2\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove2\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 4") {
foreach ($value as $dim => $num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add1'])) {
$newQty = ($_POST['qty1']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty1\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove1\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
}
if (ISSET($_POST['remove1'])) {
$newQty = ($_POST['qty1']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty1\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove1\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 5") {
foreach ($value as $dim => $num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add0'])) {
$newQty = ($_POST['qty0']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty0\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove0\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
}
if (ISSET($_POST['remove0'])) {
$newQty = ($_POST['qty0']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty0\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove0\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
echo'</tr>';
}
?>
<p><input type="submit" value="Check Out" name="checkout">
</form>
<?php
if (ISSET($_POST['checkout'])) {
echo $newQty;
}
?>
</body>
</html>
P.S:我不是要求代码讲义,只是解释和代码片段(示例)没问题。感谢。
答案 0 :(得分:0)
以下是您需要处理的关键项目。看看它会产生什么,你应该知道该怎么做。你将不得不应用逻辑来获得其他效果,但你可以看到如何去做。
$item = array(
array
(
"Qty" => 0,
"Item" => "Product 1",
"Unit_Price" => 5,
"Subtotal" => 0
),
array
(
"Qty" => 0,
"Item" => "Product 2",
"Unit_Price" => 2.3,
"Subtotal" => 0
),
array
(
"Qty" => 0,
"Item" => "Product 3",
"Unit_Price" => 3.54,
"Subtotal" => 0
),
array
(
"Qty" => 0,
"Item" => "Product 4",
"Unit_Price" => 1.67,
"Subtotal" => 0
),
array
(
"Qty" => 0,
"Item" => "Product 5",
"Unit_Price" => 3.4,
"Subtotal" => 0
)
);
function ReOrganizeArray($array) {
$i = 0;
foreach($array['Qty'] as $category => $rows) {
$items[$i]['Qty'] = $array['Qty'][$i];
$items[$i]['Item'] = $array['Item'][$i];
$items[$i]['Unit_Price'] = $array['Unit_Price'][$i];
$items[$i]['Subtotal'] = $array['Subtotal'][$i];
$items[$i]['add'] = (isset($array['add'][$i]))? true:false;
$items[$i]['remove'] = (isset($array['remove'][$i]))? true:false;
$i++;
}
return $items;
}
$items = (isset($_POST) && !empty($_POST))? ReOrganizeArray($_POST):$item; ?>
<h3>Update the products below as required.<br>Then you can click on the checkout button below.</h3>
<form method="POST" action= "<?php $_SERVER['PHP_SELF']; ?>" >
<table>
<?php foreach($items as $i => $rows) { ?>
<tr>
<td><input type="submit" name="add[<?php echo $i; ?>]" value="+" /></td>
<td><input type="submit" name="remove[<?php echo $i; ?>]" value="-" /></td>
<td><input type="text" name="Qty[<?php echo $i; ?>]" value="<?php echo $rows['Qty']; ?>" /></td>
<td><input type="text" name="Item[<?php echo $i; ?>]" value="<?php echo $rows['Item']; ?>" /></td>
<td><input type="text" name="Subtotal[<?php echo $i; ?>]" value="<?php echo $rows['Subtotal']; ?>" /></td>
<td><input type="text" name="Unit Price[<?php echo $i; ?>]" value="<?php echo $rows['Unit Price']; ?>" /></td>
</tr>
<?php
} ?>
</table>
</form>
答案 1 :(得分:0)
print_r数组正在输出此输出。
Array
(
[add] => Array
(
[0] => +
)
[Qty] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[Item] => Array
(
[0] => Product 1
[1] => Product 2
[2] => Product 3
[3] => Product 4
[4] => Product 5
)
[Unit_Price] => Array
(
[0] => 5
[1] => 2.3
[2] => 3.54
[3] => 1.67
[4] => 3.4
)
[Subtotal] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
)
为什么这样做?将每个键分组到$ items数组中?