对 HTML 表单提交数据行进行转置和排序,而不丢失关联关系

问题描述 投票:0回答:2

我有一个 HTML 表单,可以提交姓名和考试成绩的有效负载。

我需要调换结构,以便考试编号分组在一起,并且考生姓名直接与分数相关联。

<form action="process.php" method="POST">
    <table>
        <tr>
            <th style='width:10%'>No.</th>
            <th style='width:40%'>NAME</th>
            <th  style='text-align: center'>EXAM1</th>
            <th  style='text-align: center'>EXAM2</th>
            <th  style='text-align: center'>EXAM3</th>
        </tr>
        <tr>
            <td>1</td>
            <td><input type="text" size="27" name="name[]" required></td>
            <td><input type="text" size="5" name="ex1[]" required></td>
            <td><input type="text"  size="5" name="ex2[]" required></td>
            <td><input type="text"  size="5" name="ex3[]" required></td>
        </tr>
        <tr>
            <td>2</td>
            <td><input type="text" size="27" name="name[]" required></td>
            <td><input type="text" size="5" name="ex1[]" required></td>
            <td><input type="text"  size="5" name="ex2[]" required></td>
            <td><input type="text"  size="5" name="ex3[]" required></td>
        </tr>
        <tr>
            <td>3</td>
            <td><input type="text" size="27" name="name[]" required></td>
            <td><input type="text" size="5" name="ex1[]" required></td>
            <td><input type="text"  size="5" name="ex2[]" required></td>
            <td><input type="text"  size="5" name="ex3[]" required></td>
        </tr>
        <tr>
            <td colspan="2"style='text-align: center' ><input type="submit" name="submit" value="SUBMIT REQUEST"></td>
            <td colspan="3" style='text-align: center'> <button type="reset" value="Reset">CLEAR</button></td>
        </tr>
    </table>
</form>

我的处理/显示代码:

<?php        
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $ex1 = $_POST['ex1'];
    $ex2 = $_POST['ex2'];
    $ex3 = $_POST['ex3'];
        
    $exam = array();
        
    for ($i = 0; $i < count($name); $i++) {
        $exam[] = array(
            'name' => $name[$i], 
            'exam1' => $ex1[$i],
            'exam2' => $ex2[$i],
            'exam3' => $ex3[$i],
        );
    }

    echo "<pre>";
        print_r($exam);
    echo "<pre>";

    $keys = array_keys($exam);
    for ($i = 0; $i < count($exam); $i++) {
        echo "<p><b>Exam Result $keys[$i]</b></p>";
        echo "<ul>";
            foreach($exam[$keys[$i]] as $key => $value) {
                echo $key . " : " . $value . "<br>";
            }
        echo "</ul>";
    }
}
 

Original Activity from Prof. Form and Output

将文本输入分配给数组没有问题,但在显示正确的输出时遇到问题。

这是我的目标输出:

考试成绩1

  • 滚滚99
  • 詹姆斯80
  • 戴夫79

考试成绩2

  • 戴夫95
  • 罗利86
  • 詹姆斯80

考试成绩3

  • 詹姆斯88
  • 戴夫75
  • 罗利60
php arrays sorting transpose submission
2个回答
0
投票
 echo "<p><b>TOTAL STUDENTS: ". count($exam)."</b></p>";
     for($i = 0; $i < count($exam); $i++) {
        echo "<p><b>Exam Result ".($i +1)."</b></p>";
        $keys = array_column($exam, "exam$i");
        array_multisort($keys, SORT_DESC, $exam);
        foreach ($exam as $row) {
            echo "<li>". $row['name']. " ". $row["exam$i"]."</li>";
        }
     }
 }

0
投票

在将考试成绩与考生姓名合并之前,请务必验证传入的数据以避免代码损坏。

以下脚本旨在适应 HTML 表单结构的变化(以防考试或考生数量发生变化)。尽管循环遍历

$_POST
数组中的所有元素,
sscanf()
函数仍会阻止
name
行被推入结果,并从考试键中提取考试编号。

在每个构建的关联行上调用

arsort()
将会按降序对分数进行排序并保留名称。显示检查编号时可以使用结果数组第一级中的
$i
键。

代码:(演示

$_POST = [
    'name' => ['Dave', 'James', 'Rolly'],
    'ex1' => [79, 80, 99],
    'ex2' => [95, 80, 86],
    'ex3' => [75, 88, 60]
];

$exams = [];
$totalNames = count($_POST['name'] ?? []);
foreach ($_POST as $key => $row) {
    // because user submissions cannot be trusted...
    if (!sscanf($key, 'ex%d', $i) || $totalNames !== count($row)) {
        continue;
    }
    $exams[$i] = array_combine($_POST['name'], $row);
    arsort($exams[$i]);
}
var_export($exams);

输出:

array (
  1 => 
  array (
    'Rolly' => 99,
    'James' => 80,
    'Dave' => 79,
  ),
  2 => 
  array (
    'Dave' => 95,
    'Rolly' => 86,
    'James' => 80,
  ),
  3 => 
  array (
    'James' => 88,
    'Dave' => 75,
    'Rolly' => 60,
  ),
)
© www.soinside.com 2019 - 2024. All rights reserved.