通过PHP的形式输入数组循环 [英] Loop through form input arrays in php

查看:109
本文介绍了通过PHP的形式输入数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设codeIgniter的应用程序。我有使用jQuery来创建新的行项目的发票形式。该生产线项格式化如下:

I'm building an app in CodeIgniter. I have an invoice form which uses jQuery to create new line items. The line items are formated as follows:

<input type="text" name="invoice[new_item_attributes][][description]" class="ui-corner-all text invDesc" title="Description" />
<input type="text" name="invoice[new_item_attributes][][qty]" class="ui-corner-all text invQty" title="Qty" />
<input type="text" name="invoice[new_item_attributes][][amount]" class="ui-corner-all text invAmount" title="Amount" onChange="CalculateTotal(this.form)" />
<input type="hidden" name="invoice[new_item_attributes][][rowTotal]" class="row-total-input" />

我不能工作是我怎么能遍历多个订单项,然后存储在一个数据库称为行项目。

What I can't work out is how can I loop through multiple line items to store then in a DB called line items.

目前,我有

foreach ( $_POST['invoice'] as $key => $value)
	{
		$data = array(
			'description'	=>	$value;
					);
	}

但我知道,不能写,因为我需要以某种参考*发票[new_item_attributes] [] [说明] *将其存储在描述等等...

But I know that can't be write because I need to somehow reference the *invoice[new_item_attributes][][description]* to store it in description etc...

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

正确的解决方案将取决于您是否打算在存储标量值
$ _ POST ['发票'] ['new_item_attributes'],或者如果你打算使其成为一个数组的数组(换句话说,你打算具有new_item_attributes的倍数。

The correct solution will depend on whether you plan on storing scalar values under $_POST['invoice']['new_item_attributes'] or if you plan on making it an array of arrays (in other words, you plan on having multiples of the new_item_attributes.

如果你只在存储标量值计划,那么你首先需要更改每个表单元素看起来是这样的:

If you only plan on storing scalar values then you'll first need to change each of the form elements to look like this:

name="inovoice[new_item_attributes][description]"

您会发现空[]不见了。

You'll notice that the empty [] is gone.

然后你的循环应该看起来像这样:

And then your loop should look like so:

foreach($_POST['invoice']['new_item_attributes'] as $key => $val) {
    $data = array('description => $value);
}

否则,你就需要在你的PHP code使用这样的:

Otherwise you'll need to use this in your PHP code:

foreach($_POST['invoice']['new_item_attributes'] as $key => $val) {
         $data = array('description' => $val['description']);
}

或者

foreach($_POST['invoice']['new_item_attributes'] as $key => $val) {
     foreach($val as $sub => $value) {
         $data = array($sub => $value);
     }
}

这篇关于通过PHP的形式输入数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆