1

我有以下问题。

我有一个用于保存里程碑的 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...

因此,之后当我将事件添加到里程碑_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 )

问题出现在以粗体或 ** ** 标记的行中(不确定预览是否正常),里程碑_0 应该是里程碑_1。这个问题破坏了整个计数过程:)。

无论如何,如果这不是存储数据的好方法,$_POST我愿意接受改进数据存储的建议。

4

1 回答 1

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",
 ),
)
于 2012-04-25T05:51:51.433 回答