Welcome Guest, Not a member yet? Register   Sign In
How Best to Pass Variables From a View to a Model
#6

[eluser]Matt S.[/eluser]
Append square brackets ('[]') to your input names and it will turn them into arrays. That way you can do something like the following:


Lets suppose your $_SESSION['openorders'] has the following array:
Code:
array(
   0 => array('orderid' => 1, 'prodid' => 57, 'name' => 'product_num_1'),
   1 => array('orderid' => 2, 'prodid' => 65, 'name' => 'product_num_2')
)


In your view, generate your hidden input fields like this:
Code:
foreach($_SESSION['openorders'] as $values)
{
    $orderid = $values['orderid'];
    $prodid  = $values['prodid'];
    $name    = $values['name'];

    echo form_hidden('orderid[]', $orderid);
    // ...generates <input type="hidden" name="orderid[]" value="1" />
    // notice the '[]' at the end of order id, this will pair it with other orderid inputs and make it an array of values.

    echo form_hidden('prodid[' . $orderid . ']', $prodid);
    // ...generates <input type="hidden" name="prodid[1]" value="57" />
    // this time we index the prodid array with the $orderid value.

    echo form_hidden('name[' . $orderid . ']', $name);
    // ...generates <input type="hidden" name="name[1]" value="product_num_1" />
    // we did the same thing here, made the name[] an array and used $orderid as the index.
}

In your model or controller, you can retrieve the data and turn it back to an array, like so:
Code:
function my_data_accepting_function()
{
    $orderids = $this->input->post('orderid'); // array of order id values
    $prodids  = $this->input->post('prodid'); // array of product ids indexed with order id values
    $names    = $this->input->post('name'); // array of names indexed with order id values

    $openorders = array(); // array we will use to re-create the POSTed open orders

    // loop through the $orderids array
    // this will give us the order ID values that we can use to retreive the prod and name values associated with that order.
    foreach($orderids as $orderid)
    {
        $prodid = $prodids[$orderid];
        $name   = $names[$orderid];

        $openorders[] = array('orderid' => $orderid, 'prodid' => $prodid, 'name' => $name);
    }
}


Messages In This Thread
How Best to Pass Variables From a View to a Model - by El Forum - 04-20-2012, 04:24 PM
How Best to Pass Variables From a View to a Model - by El Forum - 04-20-2012, 11:16 PM
How Best to Pass Variables From a View to a Model - by El Forum - 04-21-2012, 12:00 AM
How Best to Pass Variables From a View to a Model - by El Forum - 04-21-2012, 05:01 PM
How Best to Pass Variables From a View to a Model - by El Forum - 04-21-2012, 08:58 PM
How Best to Pass Variables From a View to a Model - by El Forum - 04-22-2012, 10:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB