Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a combination of array and multidimensional array into MySQL table

There are two(2) scenarios when retrieving entries from the users.

  • Scenario 1: User enters one(1) entry per person
  • Scenario 2: User enters more than one(1) entry for either or both person(s)

Scenario 1:

User enters one(1) entry for each person. enter image description here

var_dump() returns (correct)

array(
    5
)
    {
    [0] => string(8) "John Doe"1 => string(6) "Apples"2 => string(2) "50"[3] => string(3) "200"[4] => string(10) "2015-01-16"
    }

array(
    5
)
    {
    [0] => string(8) "Jane Doe"1 => string(7) "Bananas"2 => string(2) "20"[3] => string(3) "100"[4] => string(10) "2015-01-16"
    }

Scenario 2:

User enters more than one(1) entry for either or both person(s). enter image description here

var_dump() returns (Incorrect)

array(
    5
)
    {
    [0] => string(8) "John Doe"1 => string(6) "Apples"2 => string(2) "50"[3] => string(3) "200"[4] => string(10) "2015-01-16"
    }

array(
    5
)
    {
    [0] => string(8) "Jane Doe"1 => string(7) "Oranges"2 => string(2) "20"[3] => string(3) "100"[4] => string(10) "2015-01-16"
    }

array(
    5
)
    {
    [0] => NULL1 => string(7) "Bananas"2 => string(2) "20"[3] => string(3) "200"[4] => string(10) "2015-01-16"
    }

The above dump shows the second entry with Jane Doe instead of John Doe and the entry for Jane Doe with a NULL name entry.

HTML Form

 <div class="toclone">

     <p class="name">
         <input type="text" name="name[]" id="sname" placeholder="Name"/> 
     </p> 
     <div class="inner-wrap">
         <!-- <strong>Procurement Information</strong> -->
         <div class="clone-inner">
             <p> 
                 <input type="text" name="item[]" id="sitem" placeholder="Item"/>
                 <input type="text" name="quantity[]" id="sqty" placeholder="Quantity"/> 
                 <input type="text" name="amount[]" id="samt" placeholder="Amount"/> 
                 <input type="text" name="date[]" id="sdate" placeholder="Date"/> 

                 <a href="#" class="phclone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i></a>
                 <a href="#" class="phdelete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i></a>
             </p>
         </div>
     </div>
     <a href="#" class="clone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i> Add Name</a>
     <a href="#" class="delete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i> Delete Name</a>
 </div>

 <p class="submit"> 
     <input type="submit" name="saveProcurement" value="Save" class="btn btn-sm btn-primary"/> 
 </p>

Controller

 $name  = $this->input->post('name');

 $item  = $this->input->post('item');

 $quantity = $this->input->post('quantity');

 $amount    = $this->input->post('amount');

 $date     = $this->input->post('date');

 $limit = array(); 

 if (count($name) >= count($item)) {
     $limit = $name;
 } else {
     $limit = $item; 
 }

 for ($i = 0; $i < count($limit); $i++) {   
     var_dump(array($name[$i], $item[$i], $quantity[$i], $amount[$i], $date[$i]));                       
 }

How do I modify the loop to allow both scenarios to work fine?

Any help is appreciated.

like image 760
h4kl0rd Avatar asked May 03 '26 23:05

h4kl0rd


1 Answers

You're going to need a hidden input:

<input type="hidden" name="rows[]" id="srows" value="1" /> 

Every time a name is added or deleted this input should reflect the number of rows.

Your updated template:

<div class="toclone">

     <p class="name">
         <input type="text" name="name[]" id="sname" placeholder="Name"/> 
         <input type="hidden" name="rows[]" id="srows" value="1" /> 
     </p> 
     <div class="inner-wrap">
         <!-- <strong>Procurement Information</strong> -->
         <div class="clone-inner">
             <p> 
                 <input type="text" name="item[]" id="sitem" placeholder="Item"/>
                 <input type="text" name="quantity[]" id="sqty" placeholder="Quantity"/> 
                 <input type="text" name="amount[]" id="samt" placeholder="Amount"/> 
                 <input type="text" name="date[]" id="sdate" placeholder="Date"/> 

                 <a href="#" class="phclone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i></a>
                 <a href="#" class="phdelete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i></a>
             </p>
         </div>
     </div>
     <a href="#" class="clone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i> Add Name</a>
     <a href="#" class="delete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i> Delete Name</a>
 </div>

 <p class="submit"> 
     <input type="submit" name="saveProcurement" value="Save" class="btn btn-sm btn-primary"/> 
 </p>

The updated controller.php:

$name  = $_POST['name'];

 $item  = $_POST['item'];

 $quantity = $_POST['quantity'];

 $amount    = $_POST['amount'];

 $date     = $_POST['date'];

 $limit = array(); 

 if (count($name) >= count($item)) {
     $limit = $name;
 } else {
     $limit = $item; 
 }

 $offset = 0;
 for ($i = 0; $i < count($_POST['name']); $i++) {  

     for ($j = 0; $j < intval($_POST['rows'][$i]); $j++)
     {
          $dataArray = array($name[$i]);
          array_push($dataArray, $item[$offset + $j], $quantity[$offset + $j], $amount[$offset + $j], $date[$offset + $j]);
          var_dump($dataArray);
     }
     $offset = intval($_POST['rows'][$i]);

 }

Tested this on my machine with your input:

 array(5) { [0]=> string(6) "Mouser" [1]=> string(4) "test" [2]=> string(2) "20" [3]=> string(2) "30" [4]=> string(4) "3000" } 
 array(5) { [0]=> string(6) "Mouser" [1]=> string(5) "test2" [2]=> string(2) "20" [3]=> string(2) "40" [4]=> string(4) "4000" } 
 array(5) { [0]=> string(7) "Mouser2" [1]=> string(5) "test3" [2]=> string(2) "39" [3]=> string(3) "489" [4]=> string(5) "34499" } 

The first loop runs through all names. The the inner loop cycles through all the data. The row index provided by the hidden input sets the offset for the data. So the offset for John Doe is 0 + n where n is the amount of rows (2). For Jane it's 2 + n where n is one row.

I'm not using codeIngniter so I've changed the retrieval of the POST vars to php's native $_POST.

like image 105
Mouser Avatar answered May 05 '26 13:05

Mouser