user3660378
user3660378

Reputation:

PHP HTML: mysql data as table row

Please help me with my problem.

I am trying to populate a table but 3 of the rows are based on my data in database(it's kinda hard to explain) sample is attach below enter image description here

enter image description here

this is my query

$query1 = mysql_query("SELECT * FROM tb_grade WHERE instructor_id = '$inst_id' AND description = '$desc' AND remark = '$remark' AND term = '$term' ORDER BY stud_name ASC");

this is my html table

<table class="table table-bordered table-condensed" align="center" bordercolor="#CCCCCC">
    <tr bgcolor="#009933">
    <td align="center" style="color:#FFF;">Name</td>
    <td align="center" style="color:#FFF;">Course</td>
    <td align="center" style="color:#FFF;">Prelim</td>
    <td align="center" style="color:#FFF;">Midterm</td>
    <td align="center" style="color:#FFF;">Final</td>
    <td align="center" style="color:#FFF;">Remark</td>
    </tr>
    <?php
    while($result= mysql_fetch_array($query1)){
        echo "<tr>";
        echo "<td class=\"text-center\">".$result['stud_name']."</td>";
        echo "<td class=\"text-center\">"."</td>";
        echo "<td class=\"text-center\">".$result['remark']."</td>";
        echo "<td class=\"text-center\">"."</td>";
        echo "<td class=\"text-center\">"."</td>";
        echo "<td class=\"text-center\">"."</td>";
    }
    ?>
</table>

The student will be shown in the table along with his or her remark in every term if he or she failed in every term its either Prelim, Midterm, Final but can be both prelim and midterm, midterm and final, prelim and final. etc. etc. so what is the proper query and arrangement of the table?

Upvotes: 2

Views: 124

Answers (1)

Clay
Clay

Reputation: 4760

Hopefully I understood what you were asking.... but here is what I have for you. Basically you want to fetch the results and remap the data before rendering the HTML. What I posted will not work if 2 students have the same name which is why I asked about the student id in the comments -- it'll be an exercise for you to change it to use the student id instead.

<?

$query1 = mysql_query("SELECT * FROM tb_grade WHERE instructor_id = '$inst_id' AND description = '$desc' AND remark = '$remark' AND term = '$term' ORDER BY stud_name ASC");

$students = array();
while ($row=mysql_fetch_assoc($query1)) {

    if (!isset($students[$row['stud_name']])) {
        $students[ $row['stud_name'] ] = array();
    }
    $students[ $row['stud_name'] ][ $row['term'] ] = $row['remark'];
}

?>
<table class="table table-bordered table-condensed" align="center" bordercolor="#CCCCCC">
    <tr bgcolor="#009933">
    <td align="center" style="color:#FFF;">Name</td>
    <td align="center" style="color:#FFF;">Course</td>
    <td align="center" style="color:#FFF;">Prelim</td>
    <td align="center" style="color:#FFF;">Midterm</td>
    <td align="center" style="color:#FFF;">Final</td>
    <td align="center" style="color:#FFF;">Remark</td>
<?

foreach ($students as $name => $terms) {
    echo "<tr>";
    echo "<td class=\"text-center\">".$name."</td>";
    echo "<td class=\"text-center\">?</td>";
    echo "<td class=\"text-center\">".$terms['Prelim']."</td>";
    echo "<td class=\"text-center\">".$terms['Midterm']."</td>";
    echo "<td class=\"text-center\">".$terms['Final']."</td>";
    echo "<td class=\"text-center\">?</td>";
    echo "</tr>";
}
?>
</table>

Upvotes: 1

Related Questions