微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP – 将学生姓名和成绩放入合适的技能名称

(1)class

enter image description here


(2)studentmark

enter image description here


(3)skill

enter image description here


PHP代码

    <?PHP
    //DB CONNECTION

    //---(1)Get skillname---
    $q = "SELECT skillName FROM skill ORDER BY skillName asc";
    $r = MysqLi_query($dbc, $q);
    $num_rows = MysqLi_num_rows($r);
    while($row = MysqLi_fetch_array($r, MysqLI_ASSOC))
    {
        $skills[] = $row['skillName'];
    }

    //---(2)Get classname---
    $q1 = "SELECT className FROM class";
    $r1 = MysqLi_query($dbc, $q1);
    $num_rows1 = MysqLi_num_rows($r1);
    while($row1 = MysqLi_fetch_array($r1, MysqLI_ASSOC))
    {
        $className[] = $row1['className'];
    }

    //---(3)Create table---
    echo '<table border="1" style="border-collapse: collapse; text-align: center">';
    echo '<tr>';
    for($a = 0; $a < $num_rows; $a++)
    {
        echo '<th colspan="2">'.$skillName[$a].'</th>';
    }
    echo '</tr>';
    for($b = 0; $b < $num_rows; $b++)
    {
        echo '<th>Student Name</th>';
        echo '<th>Grade</th>';
    }
    echo '</tr>';

    //---(4)Get student name and grade---
    for($s = 0; $c < $num_rows1; $c++)
    {
          $q2 = "SELECT GROUP_CONCAT(sm.studentName) as studentName,
                        GROUP_CONCAT(sm.studentGrade) as studentGrade,
                        s.skillName
                 FROM studentmark sm
                 LEFT JOIN skill s ON sm.skillID = s.skillID
                 WHERE sm.className = '".$className[$c]."'
                 GROUP BY s.skillID";
          $r2 = MysqLi_query($dbc, $q2);
          $num_rows2 = MysqLi_num_rows($r2);

          $value = array();
          while($row2 = MysqLi_fetch_array($r2, MysqLI_ASSOC))
          {
              $value[] = $row2;
          }

          echo '<tr>';
          for($d = 0; $d < $num_rows2; $d++)
          {
               echo '<td>'.$value[$d]['studentName'].'</td>';
               echo '<td>'.$value[$d]['studentGrade'].'</td>';
          }
          echo '</tr>';
    }
    echo '</table>';
    ?>

从上面的代码,我的输出如下:

enter image description here

我差不多完成了.我可以在一行中显示学生姓名和成绩.

现在,我要做的最后一件事是将它们放入合适的技能名称,如下所示:

enter image description here

我想比较$q2上的$技巧和s.skillname.

以下是我的逻辑:

    if($value[X]['skillName'] == $skills[X])
    {
         //put student name and grade inside
    }
    else
    {
         //empty cell
    }

但我不知道我应该在哪里打开循环并将我的逻辑放入(4).有人能帮我吗?

解决方法:

因此,为了不多次循环数据,我肯定会弄乱你的干净代码.我还显示名称原因,看起来像有用的信息.

我更改了一些变量名称,因为我发现更容易记住每个变量的用途.另外,请注意学生信息查询仅执行一次. normaly(读:我想不出你为什么不会,但我是CMA),你想最小化你查询数据库次数

下面的代码将替换您发布的整个脚本.

<?PHP
//DB CONNECTION
$dbc = // magic connection sauce you already have

// get skills and stash how many there are
$q_class = "SELECT skillName FROM skill ORDER BY skillName asc";
$r_class = MysqLi_query($dbc, $q_class);
$num_skills = MysqLi_num_rows($r_class);
// start table code so that we can echo the skillname headers
echo '
<table border="1" style="border-collapse: collapse; text-align: center">
    <thead>
        <tr>
            <th rowspan=2>Classes</th>';//header for class name column
$header = array();
while($row = MysqLi_fetch_array($r_class, MysqLI_ASSOC))
{
    $skills[] = $row['skillName'];
    // store both thead rows at the same time so that we can echo them out properly later
    $header['first'][] = '
            <th colspan="2">' . $row['skillName'] . '</th>';
    $header['second'][] = '
            <th>Student Name</th>
            <th>Grade</th>';
}
echo '
        ' . implode($header['first']) . '
        </tr>
        <tr>' . implode($header['second']) . '
        </tr>';
// clean-up
MysqLi_free_result($r_class);

// get class names and stash how many there are
$classes = array();
$query_class = "SELECT className FROM class";
$r_class = MysqLi_query($dbc, $query_class);
$num_classes = MysqLi_num_rows($r_class);
while($row = MysqLi_fetch_array($r_class, MysqLI_ASSOC))
{
    $classes[] = $row['className'];
}
// clean-up
MysqLi_free_result($r_class);

echo '
    </thead>
    <tbody>';

// pull query out of loop so that you'll only have to execute it once.
$studentInfoQuery = "
SELECT
    GROUP_CONCAT(sm.studentName) as studentName,
    GROUP_CONCAT(sm.studentGrade) as studentGrade,
    s.skillName,
    sm.className
FROM studentmark sm
LEFT JOIN skill s ON sm.skillID = s.skillID
GROUP BY sm.className,s.skillID";
$r_students = MysqLi_query($dbc,$studentInfoQuery);
$num_studentRows = MysqLi_num_rows($r_students);
$studentRows = array();

while($row = MysqLi_fetch_array($r_students, MysqLI_ASSOC)) {
    // with our query, we only find 1 cell-pair per skill per class
    $studentRows[$row['skillName']][$row['className']] = '
            <td>' . $row['studentName'] . '</td>
            <td>' . $row['studentGrade'] . '</td>';
}
// everybody do their share! // actually, more clean-up
MysqLi_free_result($r_students);

for($j = 0; $j < $num_classes; $j++) {
    echo "
        <tr>
            <th>" . $classes[$j] . "</th>";
    for($i = 0; $i < $num_skills; $i++) {
        // always echo out a cell, even if we have student info for it
        // example: if(isset($studentRows['Listening']['1A'])) echo it out else echo cell
        if(isset($studentRows[$skills[$i]][$classes[$j]]))
            echo $studentRows[$skills[$i]][$classes[$j]];
        else
            echo "
            <td colspan=2>No skill-class-student value</td>";
    }
    echo "
        </tr>";
}

echo '
    </tbody>
</table>';
?>

结果:

Results of code

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐