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 First array value as first loop second array value as 2nd loop ...etc

0

1 Answer 1

1

I just have changed the names of your inputs:

<form method="POST" action="">
 <?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 'people['.$i.'][fname]'?>" class="form-control"
                    type="text" />
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                <label>Surname</label> <input
                    name="<?php echo 'people['.$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="<?php echo 'people['.$i.'][dob]'?>"
                    class="date-pick-years form-control" type="text" />
            </div>
        </div>
    </div>

        <?php }?>

        <input type="submit" />
</form>

The result $_POST array after submit form is:

array(1) {
  ["people"]=>
  array(5) {
    [0]=>
    array(3) {
      ["fname"]=>
      string(5) "James"
      ["sname"]=>
      string(4) "Bond"
      ["dob"]=>
      string(10) "22.01.1950"
    }
    [1]=>
    array(3) {
      ["fname"]=>
      string(4) "Jack"
      ["sname"]=>
      string(6) "Sparow"
      ["dob"]=>
      string(10) "08.05.1970"
    }
    [2]=>
    array(3) {
      ["fname"]=>
      string(0) ""
      ["sname"]=>
      string(0) ""
      ["dob"]=>
      string(0) ""
    }
    [3]=>
    array(3) {
      ["fname"]=>
      string(0) ""
      ["sname"]=>
      string(0) ""
      ["dob"]=>
      string(0) ""
    }
    [4]=>
    array(3) {
      ["fname"]=>
      string(0) ""
      ["sname"]=>
      string(0) ""
      ["dob"]=>
      string(0) ""
    }
  }
}

Now you can loop in your controller using:

$arr_res = $_POST['people']

or your wrapper(I'm not sure)

$arr_res = $this->input->post('people');

I hope this helps.

Good practice: don't use the same name vars as function names(particulary in php) - $array = array(...) etc. better use e.g. $arr_res (array with results) or $people, $people_arr.

Sign up to request clarification or add additional context in comments.

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.