簡體   English   中英

使用 foreach 循環計算來自 DB 的值的多輸入值

[英]Multi input values using foreach loop calculating with values from DB

<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" 
} 

鏈接中的參數顯示正確

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中的一個鍵

 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM