Jump to content

Multi input values using foreach loop calculating with values from DB


salgergawi
 Share

Recommended Posts

    <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 ][1]][1]

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>
```

calc.png

Link to comment
Share on other sites

Because it's an array, you have to access it's elements using square brackets and an index:

$own_pools = $_GET['own_pools'][0];

$own_pools = (int)$_GET['own_pools'][0];

Each of your fields will need a different index in the brackets  [1], [2], [3] and so on.

  • Thanks 1
Link to comment
Share on other sites

Thank you found it

foreach( $calculations as $key =>$calculation ) { // added a key in foreach

            if(isset($_GET['own_pools'])){
                $own_pools=(int)$_GET['own_pools'][$key];   // and here added the key
                $global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
                $sum_main_pools +=$global_calc;
            }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...