Welcome Guest, Not a member yet? Register   Sign In
Dealing with $_POST arrays.
#1

[eluser]billmce[/eluser]
I query a contact phone database to pull out ID, Phone Number, Extension, Other Info. into an array.

On a VIEW I use this array to help build a form:
Code:
foreach ($dataphones->result_array() as $keyname => $phone)
    {
        // arrays count from 0 in php ... keyname holds position ?>
    <ol>
        <li class="tel">
        
              &lt;?php
                $data_item = array(
                    'name' => 'tel_num[]',
                    'value' => $phone['Phone'],
                    'maxlength' => '20',
                    'size' => '30',
                );
                echo form_input($data_item); ?&gt;
         </li>  
         <li class="ext">  
            
              &lt;?php
                $data_item = array(
                    'name' => 'tel_ext[]',
                    'value' => $phone['Extension'],
                    'maxlength' => '10',
                    'size' => '15',
                );
                echo form_input($data_item); ?&gt;
        </li>
        <li class="info">    
            
               &lt;?php
                $data_item = array(
                    'name' => 'tel_info[]',
                    'value' => $phone['AdditionalInformation'],
                    'maxlength' => '255',
                    'size' => '80',
                );
                echo form_input($data_item);
                echo form_hidden('tel_id[]', $phone['UniqContactPhoneID']);

                ?&gt;
                
        </li>

    </ol>      
    
&lt;?php     } ?&gt;

I should add that using jQuery's .append method the number of rows may have changed since the original data query ... the user may have added/delete row(s).

After the submit button is pressed I know I can retrieve the individual phone number etc. using:
$_POST['tel_num'][0] for example.

I also know the size of this array:
count($_POST['tel_num'])

But to re-enter the VIEW (after a validation) I need to reconstruct the original array form (here $dataphones).

I know I have all the pieces ... how do I put them together again?

TIA.
#2

[eluser]danmontgomery[/eluser]
The extra rows should be added to the database when they are appended to the table, then when the page reloads they are included in the db query you are passing to the view.
#3

[eluser]billmce[/eluser]
I was originally going down that route ... but a design decision has been made ... dictating effectively that the details (a handful at most) are to be saved when the header is saved.

So .... I'm going to keep the details in an array.

When it's time to save them I'm going to delete the already stored details and re-create them.

My only problem is how-to pass the array between iterations of the view.
#4

[eluser]billmce[/eluser]
Besides the design decision ... once a question like this gets into my head I gotta flush it out. How the heck is this done? :-)
#5

[eluser]danmontgomery[/eluser]
To answer your question very generally, you pass arrays between page views by serializing them. http://php.net/manual/en/function.serialize.php

To be more specific, I would do this: The table should be inside of a form... I would add a hidden field in each new row with the value being a serialized array of the new data for that row, something like:

Code:
&lt;input type="hidden" name="new_val[]" value="{some_serialized_data}"/&gt;

Then when the form submits, you'll have an array ( $this->input->post('new_val') ) of all the new values. Those values get added to the database, when the DB is re-queried to populate the table, the new values are present.
#6

[eluser]bretticus[/eluser]
Ever being the paranoid developer here, make sure you use the encryption class to encrypt and decrypt your serialized arrays to prevent tampering.
#7

[eluser]billmce[/eluser]
So to flesh that out a bit, what I've got to do is:

- remove the
Code:
foreach ($dataphones->result_array() as $keyname => $phone)
from the VIEW.
- In the controller, prior to loading the view, I create a similar foreach loop to the above -- using it to create my own array.

That takes the result_array() off the form where it gets in the way on re-edits after validation.

I know that after the submit button I can use syntax of $_POST[‘array_name’][0]. In other words I can retrieve all the values in the array after submit. I can also reconstruct the array to be picked up on subsequent iterations of the view.

How/where are serialize and urlencode introduced?

I want to do it right but I don't understand why I need serialize/unserialize and urlencode/urldecode, or where I'd use them (in the controller?, the view?, both?).

Complete newbie question huh?




Theme © iAndrew 2016 - Forum software by © MyBB