如何在PHP中以单一形式循环使用公共字段集?
我有一个表单,它包含多个虚拟字段集 例如,我的表格:如何在PHP中以单一形式循环使用公共字段集?,php,arrays,forms,loops,Php,Arrays,Forms,Loops,我有一个表单,它包含多个虚拟字段集 例如,我的表格: Name1 Age1 Location1 Name2 Age2 Location2 Name3 Age3 Location3 Submit 如果我允许用户在客户端上动态创建更多字段集,我如何循环设置了名称(x)的所有字段集,并使用整数(1、2、3等)作为唯一标识符,对每个组执行类似操作 我想指定一次操作,循环,每次根据字段集的数量更改使用的变量 现在,我正在为3个硬编码Fireldset手动执行此操作,但无法扩展: 伪代码: if($
Name1
Age1
Location1
Name2
Age2
Location2
Name3
Age3
Location3
Submit
如果我允许用户在客户端上动态创建更多字段集,我如何循环设置了名称(x)的所有字段集,并使用整数(1、2、3等)作为唯一标识符,对每个组执行类似操作
我想指定一次操作,循环,每次根据字段集的数量更改使用的变量
现在,我正在为3个硬编码Fireldset手动执行此操作,但无法扩展:
伪代码:
if($name1 is set) {
do something using $age1 and $location1
}
if($name2 is set) {
do something using $age2 and $location2
}
if($name3 is set) {
do something using $age3 and $location3
}
谢谢 输入字段应使用数组。然后可以创建任意数量的以下块:
<input name="name[]" />
<input name="age[]" />
<input name="location[]" />
这样做:
$maxIndex = 3
for(var $i=1; $i<=$maxIndex; $i++){
$name = $_POST["Name$i"];
$age = $_POST["Age$i"];
$location = $_POST["Location$i"];
//do something using $name, $age and $location
}
$maxIndex=3
对于(var$i=1;$i,您可以命名元素name=“name[1]”
,name=“name[2]”
等等
在php中执行以下操作:
for($i=1;$i<=count($name);$i++){
// do the stuff.
}
对于($i=1;$i您可以使用php中的数组
在fome的第一部分中,您可以使用
<?php
//n is no of records u want at one time
$available = $n;
for($i=1; $i<=$available; $i++){
?>
Name <input type=hidden name="pname<?=$i?>" value=<?=$pid?>>
Age<input type=text name="age<?=$i?>" />
Location<input type=text name="age<?=$i?>" />
<?php
}
?>
名称谢谢。你能在这样的变量名中使用$i吗?在我的测试中它不起作用。D'oh-单引号。这个解决方案对我来说是有效的,因为我的JS是如何在客户端设置的。
<?php
//n is no of records u want at one time
$available = $n;
for($i=1; $i<=$available; $i++){
?>
Name <input type=hidden name="pname<?=$i?>" value=<?=$pid?>>
Age<input type=text name="age<?=$i?>" />
Location<input type=text name="age<?=$i?>" />
<?php
}
?>
<?php
$available_count = $n;
for($i=1; $i<=$available_count; $i++){
$pname = "pname".$i;
$age = "age".$i;
$location = "location".$i;
$pname1 = trim($_POST[$pname]);
$age1 = trim($_POST[$pname]);
$location1 = trim($_POST[$location]);
//now you can insert these values into table/views
}
?>