ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-02 16:02:32  阅读:177  来源: 互联网

标签:php mysql html


(1)classenter image description here
(2)studentmarkenter image description here
(3)skillenter 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

标签:php,mysql,html
来源: https://codeday.me/bug/20190702/1357764.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有