在PHP中提交表单后重复表格行

时间:2019-06-21 07:06:46

标签: php

我需要重复回显的行,在提交表单时它会显示该行,但是一旦我再次提交,它将覆盖我的上一行,是否可以添加行?

我使用了一个会话,因为我想在没有数据库的情况下执行此操作

我还需要计算“总费用”列中的所有数字。

isNullOr

2 个答案:

答案 0 :(得分:0)

回答您的问题的问题是,我该怎么说呢,没有答案是完整的。总是会有其他问题。请记住,此答案仅是给您一个提示,它不能解决所有问题。这只是问题的答案:“ ...可以添加一行吗?”

答案当然是:“是!”

提交表单后,您当前所做的操作将覆盖会话中存在的信息:

$_SESSION['ingredient'] = $_POST['ingredient'];
$_SESSION['num1'] = $_POST['num1'];
$_SESSION['num2'] = $_POST['num2'];
$_SESSION['num3'] = $_POST['num3'];

这显然不是您想要的。一种解决方案是使用包含成分的数组并将其存储在会话中,如下所示:

$_SESSION['ingredients'][] = ['ingredient' => $_POST['ingredient'],
                              'num1'       => $_POST['num1'],
                              'num2'       => $_POST['num2'],
                              'num3'       => $_POST['num3']];

我完全不赞成使用num1num2num3,但我会坚持回答这个问题。

现在,当您要渲染表时,必须遍历数组:

// first start the table here

foreach ($_SESSION['ingredients'] as $ingredient) {
    $ingredient = $ingredient['ingredient'];
    $num1       = $ingredient['num1'];
    $num2       = $ingredient['num2'];
    $num3       = $ingredient['num3'];

    // render one row of the table here

}

// finally end the table here

我没有故意包括价格,但这只是另一个领域。

如前所述:这个答案并不能解决所有问题,只是一个提示。

答案 1 :(得分:0)

<?php
// Start the session
session_start();
$_SESSION = array();
    session_destroy();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="edit-Type" edit="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSFiddle</title>

<meta name="robots" content="index, follow">
<meta name="googlebot" content="index, follow">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="/js/embed/highlight.pack.js"></script>
<script src="/js/embed/embed.js"></script>
<link rel="stylesheet" media="screen" href="/css/embed/embed-light.css" />
<link rel="stylesheet" media="screen" href="//fonts.googleapis.com/css?family=Inconsolata" />

<style type="text/css" media="screen">
</style>

</head>
<body>


<?php 

if(isset($_POST['add_ingredient'])) {

$_SESSION['ingredients'][] = ['ingredient' => $_POST['ingredient'],
                              'num1'       => $_POST['num1'],
                              'num2'       => $_POST['num2'],
                              'num3'       => $_POST['num3']];

}


if(isset($_POST['selling_price_input'])) {

$_SESSION['selling_price'] = $_POST['selling_price'];
}      


?>

<form action="" method="post" enctype="multipart/form-data">    

<div class="form-group">
<label>Ingredient</label>
<input type="text" class="form-control" name="ingredient">
</div>
<div class="form-group">
<label>Price</label>
<input type="number" class="form-control" name="num1">
</div>
<div class="form-group">
<label>Qty Grams/Item</label>
<input type="number" class="form-control" name="num2">
</div>
<div class="form-group">
<label>Recipe Qty Grams/Item</label>
<input type="number" class="form-control" name="num3">
</div>


<div class="form-group">
<input class="btn btn-primary" type="submit" name="add_ingredient" value="Add Ingredient">
</div>

<div class="form-group">
<label>Selling Price</label>
<input type="number" class="form-control" name="selling_price">
</div>

<div class="form-group">
<input class="btn btn-primary" type="submit" name="selling_price_input" value="Update">
</div>
</form>


<!-----------------------------------------------------DISPLAYING------------->   

<table class="table table-bordered">
<thead>
<tr>
<th>Ingredients</th>
<th>Item Price</th>
<th>Item Qty Grams/Unit</th>
<th>Recipe Qty Grams/Unit</th>
<th>Total Cost</th>

</tr>

</thead>
<tbody>


<?php

//calculation variables
//$num1 = $_SESSION['num1'];
//$num2 = $_SESSION['num2'];
//$num3 = $_SESSION['num3'];
//$ingredient = $_SESSION['ingredient'];
//$selling_price = $_SESSION['selling_price'];
////$sellnum = $_GET['sellnum'];
//$cost = $num1 / $num2 * $num3;
//$profit =  $selling_price - $cost ;
//$profper =  $profit / $selling_price * 100;  

    foreach ($_SESSION['ingredients'] as  $ingredient) {
    $ingredient = $ingredient['ingredient'];
    $num1       = $ingredient['num1'];
    $num2       = $ingredient['num2'];
    $num3       = $ingredient['num3'];

   echo "<tr>";
echo "<td>$ingredient</td>";
echo "<td>$num1</td>";

echo "<td>$num2</td>";
echo "<td>$num3</td>";
//echo "<td>$cost</td>";
echo "</tr>";

}




?>
<!--
<tr>
<td colspan="4">Total Cost of Recipe</td>
<?php  echo "<td>$cost</td>"; ?>
</tr>
<tr>
<td colspan="4">Selling price of each baked item</td>
<?php echo "<td>$selling_price</td>"; ?>

</tr>
<tr>
<td colspan="4">Profit per item</td>
<?php  echo "<td colspan='2'>$profit</td>"; ?>
</tr>
<tr>
<td colspan="4">Profit Percentage</td>
<?php  echo "<td colspan='2'>$profper%</td>"; ?>
</tr>

<tr>
<td colspan="6">Note: Your profit should never be more than 30% of the cost of you your product</td>
</tr>
</tbody>
</table>
-->

</body>
</html>