PHP - enter student name and class in the corresponding skill name

(1) class (2) studentmark (3) skill
enter image description here

enter image description here

enter image description here

PHP code:

    <?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>';
    ?>

From the above code, my conclusion is below:

enter image description here

I'm almost done. I can show the name and class of the student in 1 line.

Now, the last thing I want to do is put them in the appropriate skill name, as shown below:

enter image description here

I want to compare $skillsand s.skillnamefrom $q2.

Below is my logic:

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

But I do not know where I should open the loop and put my logic in (4). Can anybody help me?

+4
source share
2 answers

, , . , .

, , . , - . (: , , CMA),

script, .

<?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>';
?>

:

Code results

+1

( ) 4 .

, , , , . , , .

, , - , html.

:

|Skill 1 | Skill 2 | Skill3|
|stdnt 1 |stdnt 2,3|stdnt 4|
|        |         |stdnt 5|

, array_search, array_push, . HTML

:

//This is our memory table. Let create it and add number of child arrays 
//equal to number of skills.
$memTable = array();    
for ($i = 0; $i <= sizeOf($skills) - 1; $i++) {
    $memTable[$i] = array();
}

//Lets spread out your student information in to this 2d array now  
foreach ($value as $student) {

    //Get the index of the skill
    $skillIndex = array_search($student['skillName'], $skills);

    //Now go to appropriate child array and insert your student there
    array_push($memTable[$skillIndex], $student);        
};


//Lets create the table now

$emptyCount = 0;
$currentRow = 0;

//Do until all the skill arrays are empty for a row
while ($emptyCount < 3) {

    echo "<tr>";

    $emptyCount = 0;

    foreach ($memTable as $skillLevel) {

        if (sizeof($skillLevel) - 1 < $currentRow) {

            $emptyCount ++;

            echo "<td>&nbsp</td>";
            echo "<td>&nbsp</td>";

        }
        else {

            echo "<td>" . $skillLevel[$currentRow]['studentGrade'] . "</td>";
            echo "<td>" . $skillLevel[$currentRow]['studentGrade'] . "</td>";  

        }
    }

    $currentRow++;

    echo "</tr>";
};    

, , $skills. , , - . , , .

while ($ emptyCount < sizeof ($ skills)) .

+1

Source: https://habr.com/ru/post/1609742/


All Articles