我如何循环但将缺失的数据替换为表中的空白

时间:2019-06-25 14:43:53

标签: php

我正在尝试显示一个表格,以显示第一个学生修读的所有科目,然后显示该学生在该科目中取得的所有进步等级。

但是,学生可能在某一列中没有成绩,因此我需要在其上放置空白或“无成绩”。 相反,我让它们并排堆叠...

如您所见,英语的“ 7(Pc3)”应在“ PC3”列中,而“ PC2”应说没有成绩或空白。...如果可能的话- 谢谢

table so far...

database table

我有一个循环来获取学生,再加上一个循环来获取该学生的所有主题。

并且可以显示所有成绩-但它们与右列不一致

while ($res = $result->fetch_assoc()) {
        echo "<tr><td>" . $res['subname'] . "</td>";

        $result2 = mysqli_query($mysqli, "SELECT *
                                            FROM grades
                                            JOIN gradesets ON grades.gradeset_id = gradesets.id
                                            WHERE grades.student_id = {$row['id']} 
                                            AND grades.subject_id = {$res['id']} 
                                            ORDER BY grades.gradeset_id ") or die($mysqli->error);

        while ($res2 = $result2->fetch_assoc()) {

            echo "<td>" . $res2['grade'] . "</td>";
            //echo "<td>" . $res2['gradeset_id'] . "</td>";
            //print_r($res2);
            $resset = $res2['gradeset'];
            $resset2 = substr($resset, -1);
            //print_r($resset);
            //print_r($resset2);

        }
    }

所以我可以给出正确的成绩,但是需要测试它们在正确的列中是否匹配...如果需要,这里是完整的代码...

$student = $mysqli->query("SELECT * FROM student");

echo "<center>";
echo "<h2>Data Wall</h2>";
echo "<h3>PHP</h3>";
echo "<hr/>";

while ($row = $student->fetch_assoc()) {
    echo "<table border='1'>
            <tr>
                <th>ID</th>
                <th>STUDENT</th>
                <th>HOUSE</th>
            </tr><br>";
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['stuname'] . "</td>";
    echo "<td>" . $row['house'] . "</td>";
    echo "</tr><br><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>";
    echo "<tr><th>SUBJECTS</th><th>PC1</th><th>PC2</th><th>PC3</th><th>PC4</th></tr>";
    $result = mysqli_query($mysqli, "SELECT  subjects.id,subjects.subname
                                    FROM student
                                    JOIN grades ON student.id = grades.student_id
                                    JOIN subjects ON subjects.id = grades.subject_id
                                    WHERE student.id = {$row['id']} 
                                    GROUP BY subjects.subname ORDER BY subjects.id ") or die($mysqli->error);

    while ($res = $result->fetch_assoc()) {
        echo "<tr><td>" . $res['subname'] . "</td>";

        $result2 = mysqli_query($mysqli, "SELECT *
                                            FROM grades
                                            JOIN gradesets ON grades.gradeset_id = gradesets.id
                                            WHERE grades.student_id = {$row['id']} 
                                            AND grades.subject_id = {$res['id']} 
                                            ORDER BY grades.gradeset_id ") or die($mysqli->error);

        while ($res2 = $result2->fetch_assoc()) {

            echo "<td>" . $res2['grade'] . "</td>";
            //echo "<td>" . $res2['gradeset_id'] . "</td>";
            //print_r($res2);
            $resset = $res2['gradeset'];
            $resset2 = substr($resset, -1);
            //print_r($resset);
            //print_r($resset2);

        }
    }
}
echo "</tr>";
echo "</table>";
echo "</center>";
$mysqli->close();
?>

3 个答案:

答案 0 :(得分:0)

从PHP 5.3开始,您可以使用Elvis operator- #create Dataframe file1 lines = sc.textFile(path_file_one) parts = lines.map(lambda l: l.split(";")) file1 = parts.map(lambda co: Row(column1=co[0], column2=co[1],column3=co[2],)) df1 = sqlContext.createDataFrame(file1) #create Dataframe file2 lines = sc.textFile(path_file_one) parts = lines.map(lambda l: l.split(";")) file2 = parts.map(lambda co: Row(columnf1=co[0], columnf2=co[1],columnf3=co[2],)) df1 = sqlContext.createDataFrame(file2) def get_number_of_differences(df1, df2, columnIdFile1, columnIdFile2): quatity = 0 filetxt1 = df1.alias("filetxt1") filetxt2 = df2.alias("filetxt2") df3 = filetxt2.join(filetxt1, col('filetxt1.'+columnIdFile1+'') == col('filetxt2.'+columnIdFile2+''), 'inner') for cop,xp in itertools.izip(df1.columns,df2.columns): diferenceResult=df3.filter(str("{0} != {1}".format(cop,xp))).count() quatity += diferenceResult files.write(str('\n In the columns file1: {0} file2 {1}, there are {2} differents. \n'.format(cop,xp,diferenceResult))) return quatity get_number_of_differences(df1, df2, 'column1', 'columnf1')

而且从PHP 7开始,您可以使用Null Coalescing Operator-?:

如果行为空,则可以使用其中任何一种来显示其他信息。例如(PHP 7 +):

??

如果字符串为空或echo "<td>" . ($res2['grade'] ?? 'No grade') . "</td>"; ,则结果为成绩,或者为No grade文本。

希望这会有所帮助!

答案 1 :(得分:0)

在内部查询中,您正在执行INNER JOIN,该操作仅选择那些在成绩集表中具有匹配项的行。看起来您想要LEFT OUTER JOIN,以便在没有匹配项的地方获得空占位符:

SELECT *
FROM grades
LEFT JOIN gradesets ON grades.gradeset_id = gradesets.id
WHERE grades.student_id = {$row['id']} 
AND grades.subject_id = {$res['id']} 
ORDER BY grades.gradeset_id

这样,在您的查询结果中,不会得到:

4 (PC1)
7 (PC3)
6 (PC4)

您将获得:

4 (PC1)
null
7 (PC3)
6 (PC4)

答案 2 :(得分:0)

您可以构建一个空等级数组,然后将其替换为查询中的任何数据。像这样:


$grades = [1 => '', 2 => '', 3 => '', 4 => ''];

while ($res2 = $result2->fetch_assoc()) {
    $grades[$res2['gradeset']] = $res2['grade'];
}

foreach ($grades as $grade) {
    echo "<td>" . $grade . "</td>";
}