使用 foreach 和计算值的多输入值
Multi input values using foreach and calculating values
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
当我打印时使用:var_dump($_GET["own_pools"]);
我得到
array(4) {
[0]=> string(1) "5"
[1]=> string(3) "300"
[2]=> string(3) "280"
[3]=> string(2) "50"
}
link 中的参数显示正确
index?own_pools[]=5&own_pools[]=300&own_pools[]=280&own_pools[]=50&visitor_expect=100000000&ticket_price=15&submit_calc=Calculate
但在那之后,这些值并没有在表单中得到正确的数据并显示如下:
如何计算它并从输入中正确获取值并将输入的值返回到输入值。
我已经搜索和阅读但找不到答案...
我有以下代码
<?php
$ticket_price=0;
$visitor_expect=0;
$total_value=0;
$own_pools=0;
$pool_result=0;
if(isset($_GET['submit_calc']) && isset($_GET['own_pools'])) {
$ticket_price = $_GET['ticket_price'];
$visitor_expect =$_GET['visitor_expect'];
$own_pools=$_GET['own_pools'];
if(is_numeric($ticket_price) && is_numeric($visitor_expect)){
// pools calculations
$rev_visitors=((int)$_GET["visitor_expect"]* (int)$_GET["ticket_price"]);
$total_value=($rev_visitors*0.01)*30;
$pool_result = $total_value ;
}
}
?>
<form action="" method="get" >
<table>
<tr>
<?php
// Display headers in one row as colmun
$tds = '';
$sum_main_pools=0;
foreach( $calculations as $index =>$calculation ) {
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs for the values
if ($calculation['Calculation']['poolname']<>"Regional Pool"){
$tds .= '<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
</div>
</td>';
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
} else {
$tds .= '<td>
' . $calculation['Calculation']['total_units'] . '
</td>';
}
}
var_dump($_GET["own_pools"]);
?>
</tr>
<tr>
<?php
// Printing the values
echo $tds;
?>
</tr>
</table>
<?php
$pool_result = $total_value + $sum_main_pools;
?>
<table>
<tr>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Expected Visitors per day
<input name="visitor_expect" type="text" value="100000000" id="visitor_expect" maxlength="9" size="9" >
</div>
</td>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Ticket Price
<input name="ticket_price" type="text" value="15" id="ticket_price" maxlength="2" size="3" >
</div>
</td>
</tr>
<tr >
<td style="width: 60%">
<div>
<label > <?php echo (isset($pool_result) ? $pool_result : "");?></label>
<input name="submit_calc" type="submit" value="Calculate" id="submit_calc">
</div>
</td>
</tr>
</table>
</form>
已解决:
在 foreach
中添加了一个键
foreach( $calculations as $key =>$calculation ) {
并在输入值中添加为数组
<input name="own_pools[]" type="number" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'][$key];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
当我打印时使用:var_dump($_GET["own_pools"]);
我得到
array(4) {
[0]=> string(1) "5"
[1]=> string(3) "300"
[2]=> string(3) "280"
[3]=> string(2) "50"
}
link 中的参数显示正确
index?own_pools[]=5&own_pools[]=300&own_pools[]=280&own_pools[]=50&visitor_expect=100000000&ticket_price=15&submit_calc=Calculate
但在那之后,这些值并没有在表单中得到正确的数据并显示如下:
如何计算它并从输入中正确获取值并将输入的值返回到输入值。
我已经搜索和阅读但找不到答案...
我有以下代码
<?php
$ticket_price=0;
$visitor_expect=0;
$total_value=0;
$own_pools=0;
$pool_result=0;
if(isset($_GET['submit_calc']) && isset($_GET['own_pools'])) {
$ticket_price = $_GET['ticket_price'];
$visitor_expect =$_GET['visitor_expect'];
$own_pools=$_GET['own_pools'];
if(is_numeric($ticket_price) && is_numeric($visitor_expect)){
// pools calculations
$rev_visitors=((int)$_GET["visitor_expect"]* (int)$_GET["ticket_price"]);
$total_value=($rev_visitors*0.01)*30;
$pool_result = $total_value ;
}
}
?>
<form action="" method="get" >
<table>
<tr>
<?php
// Display headers in one row as colmun
$tds = '';
$sum_main_pools=0;
foreach( $calculations as $index =>$calculation ) {
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs for the values
if ($calculation['Calculation']['poolname']<>"Regional Pool"){
$tds .= '<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
</div>
</td>';
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
} else {
$tds .= '<td>
' . $calculation['Calculation']['total_units'] . '
</td>';
}
}
var_dump($_GET["own_pools"]);
?>
</tr>
<tr>
<?php
// Printing the values
echo $tds;
?>
</tr>
</table>
<?php
$pool_result = $total_value + $sum_main_pools;
?>
<table>
<tr>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Expected Visitors per day
<input name="visitor_expect" type="text" value="100000000" id="visitor_expect" maxlength="9" size="9" >
</div>
</td>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Ticket Price
<input name="ticket_price" type="text" value="15" id="ticket_price" maxlength="2" size="3" >
</div>
</td>
</tr>
<tr >
<td style="width: 60%">
<div>
<label > <?php echo (isset($pool_result) ? $pool_result : "");?></label>
<input name="submit_calc" type="submit" value="Calculate" id="submit_calc">
</div>
</td>
</tr>
</table>
</form>
已解决: 在 foreach
中添加了一个键 foreach( $calculations as $key =>$calculation ) {
并在输入值中添加为数组
<input name="own_pools[]" type="number" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'][$key];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}