How would I structure the loop to go through inputs?

Posted by dmanexe on Stack Overflow See other posts from Stack Overflow or by dmanexe
Published on 2010-03-29T18:21:40Z Indexed on 2010/03/29 18:23 UTC
Read the original article Hit count: 320

Filed under:
|
|
|
|

I am attempting to make a loop that will go through an array structured like this:

$input[n][checked]
$input[n][input]

The 2nd input acts as a price multiplier, but doesn't have to exist (field can be blank). I don't think a foreach loop is right because I don't think it handles the inputs from the form in the correct dimensional array order to keep the information together.

I have inputs on a form that look like this:

<input type="checkbox" name="measure[<?php echo $item->id; ?>][checked]" value="<?php echo $item->id; ?>"> 
<input class="item_mult" type="text" name="measure[<?php echo $item->id; ?>][input]" />

I am attempting to make the loop go through and act as a multiplier on the input relative to its sibling field. (i.e. input[1][input] would be an integer that I want to retrieve later, grouped with input[1][checked])

<? $field = $this->input->post('measure',true);
$totals = array();

foreach($field as $value):  
        if ($value['input'] == TRUE) { 
            $query = $this->db->get_where('items', array('id' => $value['input']))->row();
            $totals[] = $query->price;      
        ?>

    <p><?=$query->name?> - <?=money_format('%(#10n', $query->price)?></p>

    <?php 

    }
        else { }

    endforeach; ?>

And finally, the last code to array_sum and print the grand total:

<? $grand_total = array_sum($totals); ?>

<p><?=money_format('%(#10n', $grand_total)?></p>

Eventually, I will need to store these records in a database, so I am sending complete item IDs through, etc.

© Stack Overflow or respective owner

Related posts about php

Related posts about form