1

我是 php 新手,几天来一直在努力寻找答案。我有一个使用表单内的 while 循环动态创建的学生列表。然后我添加了一个输入框,以便我可以在那里提交成绩。这很好用。

<form action="<? $_SERVER['PHP_SELF']; ?>" method="POST" name="assignments" id="assignments">
<?php

while($row_students = mysql_fetch_array($students))
{ ?>
    <input grade="grade[]" type="text" size="3" />
    <? echo $row_students['student_number'];?> 
    <input name="student_id[]" type="hidden" value="<? echo $row_students['student_number']; ?>" /> 

    <? echo $row_students['student_name'] ; ?>
    <input name="assignment_name[]" type="hidden" value="<? echo $row_assignments['assignment_name'];?>" /><br/>
    <? }

?>
<br/>
<input name="submit" type="submit" value="submit" />
| <a href="index.php">Cancel</a>
</form>

当我 print_r 发布数据时,我得到:

Array
(
[0] => Array
    (
        [0] => assignment 1
        [1] => assignment 1
        [2] => assignment 1
        [3] => assignment 1
        [4] => assignment 1
        [5] => assignment 1
        [6] => assignment 1
        [7] => assignment 1
    )

[1] => Array
    (
        [0] => 432100312
        [1] => 432100351
        [2] => 432100353
        [3] => 432100354
        [4] => 432100355
        [5] => 433536343
        [6] => 436253636
        [7] => 473634728
    )

[2] => Array
    (
        [0] => 21
        [1] => 21
        [2] => 33
        [3] => 42
        [4] => 15
        [5] => 26
        [6] => 37
        [7] => 18
    )

)

我的问题现在是两个方面。我认为它应该显示如下:

[0] => Array
(
[0] => assignment 1
[0] => 432100312
[0] => 21
)

因为我想使用这些数据将每个集合插入到具有以下字段的数据库中 - assignment_name、student_id 和 _grade。

我也尝试了几个插入循环无济于事,因此这篇文章。

4

3 回答 3

0

你可以试试下面的代码

$arrNew = array();
  foreach ($arr as $value) {
      foreach ($value as $key=>$value) {
          $arrNew[$key][] = $value;
       }
  }

考虑 $arr 是您的基本数组

于 2012-10-12T06:39:24.590 回答
0

只是在这里进行疯狂的猜测,但我相信我不久前做过类似的事情。下面将 print_r 如下所示: Array ( [student] => Array ( ['name'] => dfsdfsdf ['id'] => 3 ['so'] => 4 ) )

<form method="post" action"#">
    <input name="student['name']" type="text" size="3" />
    <input name="student['id']" type="hidden" value="3" /> 
    <input name="student['so']" type="hidden" value="4" />
    <input type="submit" />
</form>

在你的情况下

<form action="assignments.php" method="POST" name="assignments" id="assignments">
<?php

while($row_students = mysql_fetch_array($students))
{ ?>
    <input name="student['grade']" type="text" size="3" />
    <? echo $row_students['student_number'];?> 
    <input name="student['id']" type="hidden" value="<? echo $row_students['student_number']; ?>" /> 

    <? echo $row_students['student_name'] ; ?>
    <input name="student['assignmentName']" type="hidden" value="<? echo $row_assignments['assignment_name'];?>" /><br/>
    <? }

?>
<br/>
<input name="submit" type="submit" value="submit" />
| <a href="index.php">Cancel</a>
</form>

并尽量不要简写你的代码 IE 这只是不好的做法:D

于 2012-10-12T06:44:41.513 回答
0

下面的代码将为您工作,没有任何问题:

<form action="assignments.php" method="POST" name="assignments" id="assignments">
<?php
$key = 0;
while($row_students = mysql_fetch_array($students))
{ ?>
    <input name="student[<? echo $key ?>][grade]" type="text" size="3" value=""/>
    <? echo $row_students['student_number'];?> 
    <input name="student[<? echo $key ?>][student_id]" type="hidden" value="<? echo $row_students['student_number']; ?>" /> 

    <? echo $row_students['student_name'] ; ?>
    <input name="student[<? echo $key ?>][assignment_name]" type="hidden" value="<? echo $row_assignments['assignment_name'];?>" /><br/>
    <? $key++;}

?>
<br/>
<input name="submit" type="submit" value="submit" />
| <a href="index.php">Cancel</a>
</form>

此代码将生成您想要的数组。

于 2012-10-12T07:06:36.597 回答