Welcome Guest, Not a member yet? Register   Sign In
How should I handle these variables and forms?
#1

[eluser]danbirlem[/eluser]
I am building a piece of software (very small and simple) that works as an estimation generator.

There are two components, items and estimates. I have created a visual backend to enter items (category, unit of measurement and an auto-incrementing ID). From this, the estimation generator dynamically pulls the items from these categories, and depending on their unit of measurement, either presents a checkbox or a checkbox and a input field.

What my question is, I am looking for code that will take input from this estimation generator page and total them to an amount on the next page, without having to write specific calls for each input. The value of these input are by ID so I was thinking of making a query that would collect the values of all the inputs and do a database lookup by ID, collect the price and add them all together.

I thought about a loop that would go back into the POST stream and collect all IDs 1-100 that are present with a value (radio button, checked checkbox), look up the price, check for the input (I named the modifier input [to specify quantity] as m-$id, so the script I was writing could easily find and associate those together) and then output it.
#2

[eluser]icomefromthenet[/eluser]
shot in the dark, might try using arrays for the input fields names. A good example can be found in the codeigniter docs here http://ellislab.com/codeigniter/user-gui...ysasfields
Code:
<input type="text" name="measure[][input]" value="0.00" size="50" />
<input type="text" name="measure[][input]" value="0.00" size="50" />
<input type="text" name="measure[][input]" value="0.00" size="50" />

to get the total of measurments:

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

foreach($field as $value) {
  $totals[] = $value['input'];
}

$grand_total = array_sum($totals);
#3

[eluser]danbirlem[/eluser]
I like the direction this goes, by collecting the dollar value from each item and then submitting a grand total.
#4

[eluser]danbirlem[/eluser]
I've gone and made the input pass the ID, which leads to a database query. What you wrote was the perfect start for me to work off of! I'm still new to programming, anyways.

My question now; how do I make this query only run if there's a check on the page before? It runs across all the inputs, and gives an error when no value is passed, so I need to establish some kind of conditional statement. I was thinking an if statement with isset()?

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

    foreach($field as $value):
        $query = $this->db->get_where('items', array('id' => $value['input']))->row();
    
        $totals[] = $query->price; ?>
        
#HTML for displaying the item goes here
    
    <?php endforeach; ?>
    

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

<p>&lt;?=$grand_total?&gt;</p>




Theme © iAndrew 2016 - Forum software by © MyBB