when i print using: var_dump($_GET["own_pools"]);" /> when i print using: var_dump($_GET["own_pools"]);" /> when i print using: var_dump($_GET["own_pools"]);"/>

Multi input values using foreach and calculating values

209 views Asked by At
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools"  maxlength="5" size="5" > 

when i print using: var_dump($_GET["own_pools"]); I get

array(4) { 
    [0]=> string(1) "5" 
    [1]=> string(3) "300" 
    [2]=> string(3) "280" 
    [3]=> string(2) "50" 
} 

the parameters in the link are showing correctly

index?own_pools[]=5&own_pools[]=300&own_pools[]=280&own_pools[]=50&visitor_expect=100000000&ticket_price=15&submit_calc=Calculate

but after that the values did not get the right data back in the form and shown like this:

Please view the photo

How can I get it calculated and get the values correctly from the input and returned the entered values back to the input value.

I have searched and read but couldn't find the answer...

I have the following code

<?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>
1

There are 1 answers

0
salgergawi On BEST ANSWER

Solved it: added in foreach a key in foreach

 foreach( $calculations as $key =>$calculation ) {

and in input value added as array

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