0

I need to get post value as array from form created using for loop ,

Here is my view

   <?php for ($i=0;$i<5;$i++) {?>
            <div class="row">

            <div class="col-md-3">
                <div class="form-group">
                    <label>Name</label>
                    <input name="<?php echo 'fname'.$i?>" class="form-control" type="text" />
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>Surname</label>
                    <input  name="<?php echo 'sname'.$i?>" class="form-control" type="text" />
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>Date of Birth</label>
                    <input name="<?php echo 'dob'.$i?>" class="date-pick-years form-control" type="text" />
                </div>
            </div>
        </div>

        <?php }?>

        <input type="submit" />

How to get these post value in controller , i need as a array for example i value is 2 means i need my output as

    $array1=array('name'=>$this->input->post('name0')),'sname'=>$this->input->post('sname0')),'dob'=>$this->input->post('dob0'))

   $array2=array('name'=>$this->input->post('name1')),'sname'=>$this->input->post('sname1')),'dob'=>$this->input->post('dob1'))

Total array =array($array1,$array2);

How to get this in a loop in controller to get required output

3 Answers 3

0

Try this:

<?php for ($i = 0; $i < 5; $i++) { ?>
    <div class="row">

        <div class="col-md-3">
            <div class="form-group">
                <label>Name</label>
                <input name="data[item<?php echo $i; ?>][<?php echo 'fname' . $i ?>]" class="form-control" type="text"/>
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                <label>Surname</label>
                <input name="data[item<?php echo $i; ?>][<?php echo 'sname' . $i ?>]" class="form-control" type="text"/>
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                <label>Date of Birth</label>
                <input name="data[item<?php echo $i; ?>][<?php echo 'dob' . $i ?>]" class="date-pick-years form-control" type="text"/>
            </div>
        </div>
    </div>

<?php } ?>

You will get:

$_POST['data'] = array (
    'item0' =>
        array (
            'fname0' => '',
            'sname0' => '',
            'dob0' => '',
        ),
    'item1' =>
        array (
            'fname1' => '',
            'sname1' => '',
            'dob1' => '',
        ),
    'item2' =>
        array (
            'fname2' => '',
            'sname2' => '',
            'dob2' => '',
        ),
    'item3' =>
        array (
            'fname3' => '',
            'sname3' => '',
            'dob3' => '',
        ),
    'item4' =>
        array (
            'fname4' => '',
            'sname4' => '',
            'dob4' => '',
        ),
);

If you want to use them in variables you can extract it:

$data = $this->input->post('data');
if(is_array($data)) {
    extract($data);
    if(isset($item1)) {
        var_export($item1);
// it will contains:
//        array (
//            'fname1' => '',
//            'sname1' => '',
//            'dob1' => '',
//        );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to get an array of values that belong semantically to the same name value, use [] in your html:

    <div class="col-md-3">
        <div class="form-group">
            <label>Name</label>
            <input name="fname[<?=$i?>]" class="form-control" type="text" />
        </div>
    </div>

Then use the usual $data = $this->input->post() in your controller to read out the data. The data should be in this format:

$post[
       fname [
           0 => 'name1',
           1 => 'name2',
           ...
       ],
       sname [
           0 => 'sname1',
           1 => 'sname2',
           ...
       ],
       ...
   ]
]

Update: Format this into the format you want

$resultArray = array();
for($i = 0; $i < <formlength>; $i++) {
    $resultArray[$i]['fname'] = $post['fname'][$i];
    $resultArray[$i]['sname'] = $post['sname'][$i];
    ...
}

2 Comments

Thanks but i need as first form fields in 1st array 2nd form fields in 2nd array like that it should be
Ok so with this you should be able to do this, it's really easy. Ill update my answer accordingly.
0

use this code in your html code than you get the right result as per your requirement.

<?php for ($i=0;$i<5;$i++) {?>
                <div class="row">

                <div class="col-md-3">
                    <div class="form-group">
                        <label>Name</label>
                        <input name="name[<?=$i?>][fname]" class="form-control" type="text" />
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label>Surname</label>
                        <input  name="name[<?=$i?>][sname]" class="form-control" type="text" />
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label>Date of Birth</label>
                        <input name="name[<?=$i?>][dob]" class="date-pick-years form-control" type="text" />
                    </div>
                </div>
            </div>

            <?php }?>

            <input type="submit" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.