2

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?

6
  • write the errror here ? Commented Jan 15, 2016 at 7:04
  • do you have a student id somewhere in the data or a unique identifier other than their name? Commented Jan 15, 2016 at 7:08
  • @VipinJain there is no error sir I just want to know the correct mysql query and the arrangement of the table? or table data(<td>)? Commented Jan 15, 2016 at 7:08
  • @ChrisBanks yes sir, Commented Jan 15, 2016 at 7:10
  • ok. what you want i dont understand what you say 'but can be both prelim and midterm, midterm and final, prelim and final. ' Commented Jan 15, 2016 at 7:12

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Let me know if it works if I need to change anything but should get you one step closer to the result.
Hi sir I set aside this problem coz I misinterpret ehat really my problem is XD but now I have a problem similar to this please check it out -> stackoverflow.com/questions/34931624/php-html-table-pivot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.