php - 将数组和多维数组的组合插入到 MySQL 表中

标签 php arrays multidimensional-array

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

  • 场景 1:用户每人输入一 (1) 个条目
  • 场景 2:用户为一个或两个人输入了不止一 (1) 个条目

场景 1:

用户为每个人输入一 (1) 个条目。 enter image description here

var_dump() 返回(正确)

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

场景 2:

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

var_dump() 返回(不正确)

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 Doe 并且 Jane 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>

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

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

感谢任何帮助。

最佳答案

你将需要一个隐藏的输入:

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

每次添加或删除名称时,此输入都应反射(reflect)行数。

您更新后的模板:

<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

关于php - 将数组和多维数组的组合插入到 MySQL 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011054/

相关文章:

javascript - 如何对二维数组中的两个数组进行连接排序?

c - 在 C 中通过引用传递动态长度的二维结构数组

php - 生成带有数字签名的 PDF

c - c中数组的大小

javascript - 合并两个 Javascript 对象,其中每个键包含多个值

javascript - jQuery/Javascript - 从数组中的无序列表中查找字符串

javascript - 观察 Ember.js 中嵌套数组中对象属性的变化

php - 将动态 PHP/mySQL 网站转换为存档的 HTML 版本?

javascript - 基于浏览器的实时 MMO 游戏的内部运作

PHP & MySQL - 检查数据库条目是否不存在?