Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP foreach()只输出数组的最后一项_Php_Jquery_Arrays_Codeigniter - Fatal编程技术网

PHP foreach()只输出数组的最后一项

PHP foreach()只输出数组的最后一项,php,jquery,arrays,codeigniter,Php,Jquery,Arrays,Codeigniter,作为我上一篇文章的续篇,我的目的只是让我的问题直截了当、简短,但显然,为了清晰起见,我最好包括所有涉及的模块 我正在从事一个CodeIgniter项目,该项目要求用户在输入post数组的帮助下提供多个选项,如下图所示: <form action="javascript:void(0)" id="add_stock"> <div class="stock"> <div>

作为我上一篇文章的续篇,我的目的只是让我的问题直截了当、简短,但显然,为了清晰起见,我最好包括所有涉及的模块

我正在从事一个CodeIgniter项目,该项目要求用户在输入post数组的帮助下提供多个选项,如下图所示:

<form action="javascript:void(0)" id="add_stock">
   <div class="stock">
      <div>
         <div class="form-group col-md-4">
           <input type="text" name="item[]" placeholder="Name of item" class="form-control item" id="item" />
           <span class="item_err"></span>
         </div>
         <div class="form-group col-md-4">
           <input type="text" name="unit[]" list="units" placeholder="Enter unit of measurement" class="form-control unit" id="unit" />
           <datalist id="units">
              <option>cube</option>
              <option>cup</option>
           </datalist>
           <span class="unit_err"></span>
         </div>
         <div class="form-group col-md-3">
            <input type="text" onfocus="(this.type='number')"  name="quantity[]" placeholder="Enter quantity" class="form-control quantity" id="quantity" />
            <span class="quantity_err"></span>
          </div>
          <div class="form-group col-md-1">
             <button class="btn btn-primary add-more-stock" title="Add more item" style ="height:33px;margin-top: ;"><i class="fa fa-plus"></i></button>
          </div>
          <div class="clearfix"></div>
       </div>
     </div>
     <div class="clearfix"></div>
     <div class="form-group msg"></div>
     <div class="form-group col-md-12">
       <button class="btn btn-primary add-stock text-uppercase"><i class="fa fa-plus"></i> add stock</button>
     </div>
 </form>
现在的问题是,不是显示数组的全部内容,而是只输出最后一个内容,即如果$array的内容是['a','b',],而不是显示a,b,那么foreach()循环只显示b

但当我回显$items、$units或$quantitys本身时,就会显示数组的内容

那么,请问,如何获取数组的所有内容并将它们插入到不同的行中


谢谢大家!

你的问题开始于你的状态,然后在你的控制器中变得更糟

控制器中的具体问题如下:

foreach ($items as $item) {
   $stock['item'] = $item;
}
您正在迭代
$items
数组,但每次迭代时都会覆盖
$stock['item']
的值。只存储最后一个值

要解决此问题,您需要做很多事情:

首先,在表单名称中使用数组索引。这允许您在
$\u POST

在HTML中,在
字段中使用
name=“item[0]”
name=“unit[0]”
name=“quantity[0]”

然后,在添加新行的Javascript代码中,增加每一新行的索引。您已经有一个计数器可用于此=
x

'<input type="text" name="item[' + x + ']" placeholder="Name of item" class="form-control item" id="item" />'
(以及在Javascript中使用
x
的相应更改)

然后在$_POST]['products']上迭代:

foreach($_POST['products'] as $product) {
     $unit = $product['unit'];
     ...
}

你的问题开始于你的状态,然后在你的控制器中变得更糟

控制器中的具体问题如下:

foreach ($items as $item) {
   $stock['item'] = $item;
}
您正在迭代
$items
数组,但每次迭代时都会覆盖
$stock['item']
的值。只存储最后一个值

要解决此问题,您需要做很多事情:

首先,在表单名称中使用数组索引。这允许您在
$\u POST

在HTML中,在
字段中使用
name=“item[0]”
name=“unit[0]”
name=“quantity[0]”

然后,在添加新行的Javascript代码中,增加每一新行的索引。您已经有一个计数器可用于此=
x

'<input type="text" name="item[' + x + ']" placeholder="Name of item" class="form-control item" id="item" />'
(以及在Javascript中使用
x
的相应更改)

然后在$_POST]['products']上迭代:

foreach($_POST['products'] as $product) {
     $unit = $product['unit'];
     ...
}

好的,我会那样做,然后带着反馈回来…好的,我会那样做,然后带着反馈回来。。。
name="products[0]['item']" 
name="products[0]['unit']"
name="products[0]['quantity']"
foreach($_POST['products'] as $product) {
     $unit = $product['unit'];
     ...
}