将数组和多维数组的组合插入 MySQL table

Inserting a combination of array and multidimensional array into MySQL table

从用户那里检索条目时有两 (2) 种情况。

场景一:

用户为每个人输入一 (1) 个条目。

var_dump() returns(正确)

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"
    }

场景二:

用户为一个或两个人输入了不止一 (1) 个条目。

var_dump() returns(不正确)

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"
    }

上面的转储显示第二个条目 Jane Doe 而不是 John DoeJane Doe 的条目 带有 NULL 名称条目。

HTML表格

 <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>

控制器

 $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]));                       
 }

如何修改循环以使两种情况都能正常工作?

感谢任何帮助。

您将需要一个隐藏输入:

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

每次添加或删除名称时,此输入应反映行数。

您更新的模板:

<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>

更新后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]);

 }

使用您的输入在我的机器上测试了这个:

 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" } 

第一个循环遍历所有名称。内部循环循环遍历所有数据。隐藏输入提供的行索引设置数据的偏移量。因此 John Doe 的偏移量是 0 + n,其中 n 是行数 (2)。对于 Jane,它是 2 + n,其中 n 是一行。

我没有使用 codeIngniter,所以我将 POST 变量的检索更改为 php 的原生 $_POST