This is my data table1. I want this table change to as a table2. I have to use PHP for this. I wrote the some code using foreach but that is not working properly. C
Try this once i didn't check the output i am assuming array like follow
array(
[
"STUDENT_ID" => "1",
'MATHS' =>
array(
"EXAM_1" => 'test 1',
"EXAM_2" => 'test2'),
'ART' =>
array(
"EXAM_1" => 'test 1',
"EXAM_2"=> 'test2')
],
[
"STUDENT_ID" => "2",
'MATHS' =>
array(
"EXAM_1" => 'test 1',
"EXAM_2" => 'test2'),
'ART' =>
array(
"EXAM_1" => 'test 1',
"EXAM_2"=> 'test2')
]
);
`
$output .= "<table border=1>
<tr bgcolor=#ffffff><td>SeqNo</td>
<td>Student Id</td>
<td colspan="2">maths</td>
<td colspan="2">art</td>
";
$studentCounter = 0;
foreach($result as $item)
{
$output .= "<tr><td>" . ++$studentCounter . " </td>
<td>" . $item[STUDENT_ID] . "</td>
<td>" . $item[MATHS][EXAM_1] . "</td>
<td>" . $item[MATHS][EXAM_2] . "</td>
<td>" . $item[ART][EXAM_1] . "</td>
<td>" . $item[ART][EXAM_2] . "</td></tr>";
"
}
$output .= "</table>";
you can generate result using sql by group_concat mysql function
SELECT * FROM table1 GROUP_CONCAT(student_id) ORDER BY subject_name DESC
this query will generate result something like this
student id=>1,subject=>math,exam1=>15,exam2=>20,subject=>art,exam1=>17,exam2=>19
If I understand correctly you want the table 2 to be as shown in the illustration so based on that I'm going to answer your question.
You can use the below HTML which will look like the table you wanted.
<table border=1>
<tr bgcolor=#ffffff>
<td>SeqNo</td>
<td>Student Id</td>
<td colspan = "2">maths</td>
<td colspan = "2">art</td>
</tr>
<tr>
<td colspan = "2"></td>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
<tr>
<td></td>
<td></td>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
</table>
Want see jsFiddle here
You didn't post your SQL you don't need to use a forwach
you can while
loop it like shown below.
<table border=1>
<tr bgcolor=#ffffff>
<td>SeqNo</td>
<td>Student Id</td>
<td colspan = "2">maths</td>
<td colspan = "2">art</td>
</tr>
<tr>
<td colspan = "2"></td>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
<?PHP while($row = $stmt -> fetch(PDO::FETCH_ASSOC){ ?>
<tr>
<td>$row["seqNo"]</td>
<td>$row["studen_id"]</td>
<td>$row["Exam1"]</td>
<td>$row["Exam2"]</td>
<td>$row["Exam1"]</td>
<td>$row["Exam2"]</td>
</tr>
<?php } ?>
</table>
Keep in mind this is with out your SQL so adjust it to fit your need and is in PDO
.
Rowspan tells a cell how many rows to occupy, colspan how many columns.
$output .= "<table border=1>
<tr bgcolor=#ffffff>
<td rowspan="2">SeqNo</td>
<td rowspan="2">Student Id</td>
<td colspan="2">maths</td>
<td colspan="2">art</td>
</tr>
<tr>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>";
$studentCounter = 0;
foreach($result as $item) {
$output .= "<tr>
<td>" . $studentCounter++ . " </td>
<td>" . $item["STUDENT_ID"] . "</td>
<td>" . $item["MATHS"] . "</td>
<td>" . $item["ART"] . "</td>
<td>" . $item["EXAM_1"] . "</td>
<td>" . $item["EXAM_2"] . "</td></tr>";
}
$output .= "</table>";
It seems to me that the table structure you are using is incorrect.
Here is a correct table structure of the table layout you are looking for.
<table border=1>
<thead>
<tr>
<th rowspan="2">Seq N0</th>
<th rowspan="2">Student Id</th>
<th colspan="2">maths</th>
<th colspan="2">art</th>
</tr>
<tr>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>82</td>
<td>90</td>
<td>60</td>
<td>82</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>82</td>
<td>90</td>
<td>60</td>
<td>82</td>
</tr>
</tbody>
</table>
This would be your table design: (https://jsfiddle.net/vg3whzvq/2/)
<table border=1>
<tr bgcolor=#ffffff>
<td rowspan="2">SeqNo</td>
<td rowspan="2">Student Id</td>
<td colspan = "2">maths</td>
<td colspan = "2">art</td>
</tr>
<tr>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
<tr>
<td>1</td>
<td>200301</td>
<td>51</td>
<td>25</td>
<td>64</td>
<td>45</td>
</tr>
</table>
Now to display the data with php you can do something like that:
$output .= "<table border=1>
<tr bgcolor=#ffffff>
<td rowspan="2">SeqNo</td>
<td rowspan="2">Student Id</td>
<td colspan = "2">maths</td>
<td colspan = "2">art</td>
</tr>
<tr>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>";
$studentCounter = 0;
foreach($result as $item)
{
$output .= "<tr>
<td>" . ++$studentCounter . " </td>
<td>" . $item[STUDENT_ID] . "</td>
<td>" . $item[MATHS_EXAM_1] . "</td>
<td>" . $item[MATH_EXAM_2] . "</td>
<td>" . $item[ART_EXAM_1] . "</td>
<td>" . $item[ART_EXAM_2] . "</td>
</tr>";
}
$output .= "</table>";
It now depends on how your $result
looks like.