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

[eluser]vincej[/eluser]
Hi - I have an order form with products on each line. It has a combination of POST variables which can be easily Posted up to my controller up but also some straight variables - see View:

Code:
<?php foreach ( $_SESSION['openorders'] as $key=>$value) {  ?>
<tr align="center">
<td width="30">&lt;?php $data=array('name' =>'quantity','value'=>$value['quantity']); echo form_input($data); ?&gt;</td> //POST VARIABLE
<td>&lt;?php echo $value['prodid']; ?&gt;</td> // STRAIGHT VARIABLES
    <td>&lt;?php echo $value['name'];?&gt;</td>
    <td>&lt;?php echo $value['ordervalue']; ?&gt;</td>
<td>&lt;?php echo $value['pricelb']; ?&gt;</td>
<td align="right">&lt;?php echo form_checkbox('delete','delete',FALSE); // POST VARIABLES
$data = array(
'orderid'  => $value['orderid'],
'productid'=> $value['prodid'],
'name'=>$value['name'],
);
echo form_hidden($data);
   $totalordervalue += $value['ordervalue'];
} ?&gt;
    </td>                      
</tr>
    <tr><td></td><td></td><td>&lt;?php echo "Total Unweighed Order Value:&nbsp;$";?&gt;</td>
    <td>&lt;?php echo $totalordervalue; ?&gt;</td>
    </tr>
    </table> <br/>

&lt;?php

    echo form_submit('submit','Update and Submit to Picking >> ');
    echo form_close();

My code loops through the $_Session['openorders'] building the page - great.

I now modify the quantities.

I need recommendations on what is the best way of getting all the variables for each product line packed up into some kind of array so I can process them.

I thought of using hidden values and POSTING them to the controller - but what is the best way of getting them into an array ?

Many Thanks for your advice !
#2

[eluser]Ajaxboy[/eluser]
[quote author="vincej" date="1334964250"]Hi - I have an order form with products on each line. It has a combination of POST variables which can be easily Posted up to my controller up but also some straight variables - see View:

Code:
&lt;?php foreach ( $_SESSION['openorders'] as $key=>$value) {  ?&gt;
<tr align="center">
<td width="30">&lt;?php $data=array('name' =>'quantity','value'=>$value['quantity']); echo form_input($data); ?&gt;</td> //POST VARIABLE
<td>&lt;?php echo $value['prodid']; ?&gt;</td> // STRAIGHT VARIABLES
    <td>&lt;?php echo $value['name'];?&gt;</td>
    <td>&lt;?php echo $value['ordervalue']; ?&gt;</td>
<td>&lt;?php echo $value['pricelb']; ?&gt;</td>
<td align="right">&lt;?php echo form_checkbox('delete','delete',FALSE); // POST VARIABLES
$data = array(
'orderid'  => $value['orderid'],
'productid'=> $value['prodid'],
'name'=>$value['name'],
);
echo form_hidden($data);
   $totalordervalue += $value['ordervalue'];
} ?&gt;
    </td>                      
</tr>
    <tr><td></td><td></td><td>&lt;?php echo "Total Unweighed Order Value:&nbsp;$";?&gt;</td>
    <td>&lt;?php echo $totalordervalue; ?&gt;</td>
    </tr>
    </table> <br/>

&lt;?php

    echo form_submit('submit','Update and Submit to Picking >> ');
    echo form_close();

My code loops through the $_Session['openorders'] building the page - great.

I now modify the quantities.

I need recommendations on what is the best way of getting all the variables for each product line packed up into some kind of array so I can process them.

I thought of using hidden values and POSTING them to the controller - but what is the best way of getting them into an array ?

Many Thanks for your advice ![/quote]

I think using Javascript would be your best option to interact with data already set on the page. And you can also update the elements, and settings on the page with Jascript, before is submitted to the server.. unless you require of more info from the server or need to save the info on its current state, Javascript is what you need.
#3

[eluser]M52 Studios[/eluser]
I'm assuming that the code you posted isn't working?

I think you may be constructing the hidden array wrong:
Code:
$data = array(
'orderid'  => $value['orderid'],
'productid'=> $value['prodid'],
'name'=>$value['name'],
);
echo form_hidden($data);


Remember that you have to pass the same attributes into the hidden input the same way you would for a regular input. In other words you need to pass it the "name", and "value" attributes.
Working with you array structure, you can do the following
Code:
$data = array(
'orderid'  => $value['orderid'],
'productid'=> $value['prodid'],
'name'=>$value['name'], // I would stay away from 'name', rename it to something like 'prod_name'
);

foreach($data as $key => $val)
{
   echo form_hidden($key, $val);
}


But, honestly, you can skip the whole array idea and just throw in three lines of code, unless you are constructing a different number of hidden input fields every time, of course.
Code:
echo form_hidden('orderid', $value['orderid'];
echo form_hidden('productid', $value['prodid'];
echo from_hidden('prod_name', $value['name'];

Code untested, but hopefully should work
#4

[eluser]vincej[/eluser]
Thanks so much for the feedback. I appreciate that JS could be a route however, I would prefer to stay away from that as it complicates things considerably.

Thanks M52 for your thoughts, however, I think I need to explain myself better. My form presents products, eg:

Quantity / ID / ProductName / Price / Total / Delete

My code loops through the OPen_orders Session variable and generates as many lines as the customer has ordered. The customer can now change the quantity or delete the line ( checkbox). These are post variables. The other data items are also post variables as a hidden value. All is good, untill you try to upload to the controller.

My problem is that there might be 20 line items ie 20x 'ProductName', ID, Total etc.

I need to capture this as an array.

I'm really unsure how to upload all these values as 1 single array in the controller / Model as they all have the same name in the HTML form. My code as it stands works - except that it only grabs the last product line, because there is no array grabbing all the values I assume.

Maybe I'm being stupid here. Maybe it's obvious.

Anyway - anyone who might steer me in the correct direction, will be forever in my Gratitude !

Many thanks !!
#5

[eluser]cPage[/eluser]
Code:
$this->input->post()
contains all your data into an array.
#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 &lt;input type="hidden" name="orderid[]" value="1" /&gt;
    // 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 &lt;input type="hidden" name="prodid[1]" value="57" /&gt;
    // this time we index the prodid array with the $orderid value.

    echo form_hidden('name[' . $orderid . ']', $name);
    // ...generates &lt;input type="hidden" name="name[1]" value="product_num_1" /&gt;
    // 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);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB