我有以下问题。
我有一个jQuery手风琴,用于保存里程碑,在每个里程碑内,我有一个按钮,可以将事件添加到里程碑。这很好。我为每个输入分配名称的方式如下。
<input type="text" name="milestone_0_name">
<input type="text" name="milestone_0_start_date">
<input type="text" name="milestone_0_end_date">
<input type="text" name="milestone_0_description">
<input type="text" name="milestone_1_name">
<input type="text" name="milestone_1_start_date">
<input type="text" name="milestone_1_end_date">
<input type="text" name="milestone_1_description">
<input type="text" name="milestone_2_name">
<input type="text" name="milestone_2_start_date">
<input type="text" name="milestone_2_end_date">
<input type="text" name="milestone_2_description">
and so on...
所以之后当我向milestone_0添加事件时,例如生成的标记如下:
<input type="text" name="milestone_0_events_event_0_event_name">
<input type="text" name="milestone_0_events_event_0_event_start_date">
<input type="text" name="milestone_0_events_event_1_event_name">
<input type="text" name="milestone_0_events_event_1_event_start_date">
我不是$_POST
数据处理的专家,因此我使用这种方式来了解哪些事件属于某个里程碑。问题是我用来识别形成里程碑的变量(里程碑的信息和每个事件信息)的函数有一个我似乎无法找到的问题。
功能是:
// Assign all $_POST data to $milestones
$milestones = $_POST;
print_r($milestones);
$milestoneNames = array();
$milestoneSize = array();
$default = "milestone_";
$indexOfMilestone=0;
$ind=0;
$text=$default.$indexOfMilestone;
$indexOfIteration=0;
$actualSize=0;
$change=false;
foreach($milestones as $ms)
{
$currKey = KeyName($milestones,$indexOfIteration);
echo $text.'.'.$currKey.'<br />';
$pos = strripos($currKey, $text);
if ($pos === false)
{
$milestoneSize[$indexOfMilestone] = $indexOfIteration;
$indexOfMilestone++;
$text=$default.$indexOfMilestone;
}
$indexOfIteration++;
}
print_r($milestoneSize);
对于测试数据,输出为:
[milestone_0_name] => Milestone 1
[milestone_0_start_date] => 12-4-25
[milestone_0_end_date] => 12-4-26
[milestone_0_description] => abcdefg
[milestone_0_events_event_0_event_name] => asdg
[milestone_0_events_event_0_event_start_date] => ff
[milestone_0_events_event_1_event_name] => fghjk
[milestone_0_events_event_1_event_start_date] => ff
[milestone_0_events_event_2_event_name] => zxcv
[milestone_0_events_event_2_event_start_date] => ff
[milestone_0_events_event_3_event_name] => bnm,
[milestone_0_events_event_3_event_start_date] => zz
[milestone_0_events_event_4_event_name] => qwerty
[milestone_0_events_event_4_event_start_date] => zz
[milestone_0_events_event_5_event_name] => uiop
[milestone_0_events_event_5_event_start_date] => zz
[milestone_1_name] => Milestone 2
[milestone_1_start_date] => 12-4-26
[milestone_1_end_date] => 12-4-27
[milestone_1_description] => sadg ads asdgasdg adsga sgdsa gsad adsg asdg
[milestone_1_events_event_0_event_name] => 1234
[milestone_1_events_event_0_event_start_date] => ff
[milestone_1_events_event_1_event_name] => 4321
[milestone_1_events_event_1_event_start_date] => ff
[milestone_1_events_event_2_event_name] => 6542
[milestone_1_events_event_2_event_start_date] => ff
)
milestone_0.milestone_0_name
milestone_0.milestone_0_start_date
milestone_0.milestone_0_end_date
milestone_0.milestone_0_description
milestone_0.milestone_0_events_event_0_event_name
milestone_0.milestone_0_events_event_0_event_start_date
milestone_0.milestone_0_events_event_1_event_name
milestone_0.milestone_0_events_event_1_event_start_date
milestone_0.milestone_0_events_event_2_event_name
milestone_0.milestone_0_events_event_2_event_start_date
milestone_0.milestone_0_events_event_3_event_name
milestone_0.milestone_0_events_event_3_event_start_date
milestone_0.milestone_0_events_event_4_event_name
milestone_0.milestone_0_events_event_4_event_start_date
milestone_0.milestone_0_events_event_5_event_name
milestone_0.milestone_0_events_event_5_event_start_date
**milestone_0.milestone_1_name**
milestone_1.milestone_1_start_date
milestone_1.milestone_1_end_date
milestone_1.milestone_1_description
milestone_1.milestone_1_events_event_0_event_name
milestone_1.milestone_1_events_event_0_event_start_date
milestone_1.milestone_1_events_event_1_event_name
milestone_1.milestone_1_events_event_1_event_start_date
milestone_1.milestone_1_events_event_2_event_name
milestone_1.milestone_1_events_event_2_event_start_date
Array ( [0] => 16 )
问题出现在以粗体或** **标记的行中(不确定预览是否正常),milestone_0应为milestone_1。这个问题破坏了整个计数过程:)。
无论如何,如果这不是存储$_POST
数据的好方法,我愿意接受改善数据存储的建议。
答案 0 :(得分:1)
根据您的要求,以下是我将如何构建表单:
<ul class="milestones">
<li>
<label>
Name
<input type="text" name="milestones[0][name]">
</label>
<label>
Start date
<input type="text" name="milestones[0][start_date]">
</label>
<label>
End date
<input type="text" name="milestones[0][end_date]">
</label>
<label>
Description
<input type="text" name="milestones[0][description]">
</label>
<ul class="events">
<li>
<label>
Name
<input type="text" name="milestones[0][events][0][name]">
</label>
<label>
Start date
<input type="text" name="milestones[0][events][0][start_date]">
</label>
</li>
<li>
<label>
Name
<input type="text" name="milestones[0][events][1][name]">
</label>
<label>
Start date
<input type="text" name="milestones[0][events][1][start_date]">
</label>
</li>
</ul>
</li>
<li>
<label>
Name
<input type="text" name="milestones[1][name]">
</label>
<label>
Start date
<input type="text" name="milestones[1][start_date]">
</label>
<label>
End date
<input type="text" name="milestones[1][end_date]">
</label>
<label>
Description
<input type="text" name="milestones[1][description]">
</label>
</li>
</ul>
这将产生如下的POST数据:
milestones => array(
0 => array(
name => "name",
start_date => "2012-04-25",
end_date => "2012-04-28",
description => "desc",
events => array(
0 => array(
name => "name0",
start_date => "date0",
),
1 => array(
name => "name1",
start_date => "date1",
),
),
),
1 => array(
name => "name",
start_date => "2012-05-01",
end_date => "2012-05-14",
description => "desc",
),
)