This is my code :
PHP Code:
<?php
$host="localhost";
$myuser="root";
$mypass="admin";
$mydb="ex1";
mysql_connect($host,$myuser,$mypass);
mysql_select_db($mydb);
$query=mysql_query;
$fetch=mysql_fetch_array;
echo "<table border=1>
<tr valign=top bgcolor=#eeeeee>
<td>name</td>";
$e=$query("SELECT * FROM std ORDER by id1")or die(mysql_error());
while($headers=$fetch($e))
{
echo "<td><div align=center>$headers[name]</td></div>";
}
echo "</tr>";
$e=$query("SELECT DISTINCT exam FROM exam ORDER BY id2");
while($exam=$fetch($e))
{
echo "<tr valign=top><td bgcolor=#eeeeee>$exam[exam]</td>";
$d=$query("SELECT * FROM exam LEFT JOIN std ON std.id1=exam.id1 ORDER BY id2");
while($e=$fetch($d))
{
if(!$e[result])
{
$e[result]=0;
}
else
{
$e[result]="<b>$e[result]</b>";
}
echo "<td><div align=right>$e[result]</div></td>";
}
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
And this is the database :
Code:
CREATE TABLE `std` (
`id1` int(5) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id1`)
) ENGINE=MyISAM;
INSERT INTO `std` VALUES ('', 'John');
INSERT INTO `std` VALUES ('', 'Matt');
INSERT INTO `std` VALUES ('', 'Jean');
CREATE TABLE `exam` (
`id2` int(5) NOT NULL auto_increment,
`id1` int(5) NOT NULL,
`exam` varchar(10) NOT NULL,
`result` int(5) NOT NULL,
PRIMARY KEY (`id2`)
) ENGINE=MyISAM;
INSERT INTO `exam` VALUES ('', '1', 'bio', '7');
INSERT INTO `exam` VALUES ('', '1', 'phy', '8');
INSERT INTO `exam` VALUES ('', '1', 'mat', '8');
INSERT INTO `exam` VALUES ('', '2', 'bio', '6');
INSERT INTO `exam` VALUES ('', '2', 'phy', '7');
INSERT INTO `exam` VALUES ('', '2', 'mat', '8');
INSERT INTO `exam` VALUES ('', '3', 'bio', '8');
INSERT INTO `exam` VALUES ('', '3', 'phy', '6');
INSERT INTO `exam` VALUES ('', '3', 'mat', '7');
But the result isnt exactly what I want.
The example of result what I want should be like this :
exam| John | Matt | Jean |
bio |---7------6------8
phy|---8------7------7
mat|---7------8------7
Pls help. Thx.