Congrat Bogey! You got my double-smile
Your latest code is work, and
ALMOST reach my need.

Thanks to Bogey, thanks to righteous_trespasser for your interests on helping me.
for righteous_trespasser, your latest code (in this forum, not in http://ohdesignx.co.za/exam/temp.php) displayed nothing in my browser, although i have modified again.
I didn't know what's my wrong, i have modified your code to be match with my database.
for Bogey:
I used Your script:
<?php
function exams() {
$con = mysql_connect('localhost','root','myrootpassword') or die(mysql_error());
$select = mysql_select_db('dtbs') or die(mysql_error());
if($con && $select)
{
$sql = "SELECT `exam_name` FROM exam ORDER BY exam_id";
$exams = mysql_query($sql);
// Get the table started
$table = "<table border=\"1px\" style=\"width: 100%;\">\n\t<tr>\n\t\t<td>\n\t\t\t<p>Student</p>\n\t\t</td>\n";
while($exam = mysql_fetch_assoc($exams))
{
$e_name = $exam['exam_name'];
$table .= "\t\t<td>\n\t\t\t<p>{$e_name}</p>\n\t\t</td>\n";
}
$table .= "\t</tr>\n";
$students = mysql_query("SELECT * FROM students");
while($student = mysql_fetch_assoc($students))
{
$st_id = $student['student_id'];
$st_name = $student['student_name'];
$process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
$table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
while($row = mysql_fetch_assoc($process))
{
$score = $row['result'];
$table .= "\t\t<td>\n\t\t\t{$score}\n\t\t</td>\n";
}
$table .= "\t</tr>\n";
}
$table .= "</table>";
echo $table;
}
}
// Call the function you have made above :)
exams();
?>
-
- <?php
- function exams() {
- $con = mysql_connect('localhost','root','myrootpassword') or die(mysql_error());
- $select = mysql_select_db('dtbs') or die(mysql_error());
- if($con && $select)
- {
- $sql = "SELECT `exam_name` FROM exam ORDER BY exam_id";
- $exams = mysql_query($sql);
- // Get the table started
- $table = "<table border=\"1px\" style=\"width: 100%;\">\n\t<tr>\n\t\t<td>\n\t\t\t<p>Student</p>\n\t\t</td>\n";
- while($exam = mysql_fetch_assoc($exams))
- {
- $e_name = $exam['exam_name'];
- $table .= "\t\t<td>\n\t\t\t<p>{$e_name}</p>\n\t\t</td>\n";
- }
- $table .= "\t</tr>\n";
-
- $students = mysql_query("SELECT * FROM students");
- while($student = mysql_fetch_assoc($students))
- {
- $st_id = $student['student_id'];
- $st_name = $student['student_name'];
-
- $process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
- $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
- while($row = mysql_fetch_assoc($process))
- {
- $score = $row['result'];
- $table .= "\t\t<td>\n\t\t\t{$score}\n\t\t</td>\n";
- }
- $table .= "\t</tr>\n";
- }
- $table .= "</table>";
-
- echo $table;
- }
- }
- // Call the function you have made above :)
- exams();
- ?>
-
-
and calling my database:
-- ----------------------------
-- Table structure for exam
-- ----------------------------
CREATE TABLE `exam` (
`exam_id` int(10) NOT NULL auto_increment,
`exam_name` varchar(50) NOT NULL,
PRIMARY KEY (`exam_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `exam` VALUES ('1', 'Biology');
INSERT INTO `exam` VALUES ('2', 'Math');
INSERT INTO `exam` VALUES ('3', 'Physics');
INSERT INTO `exam` VALUES ('4', 'Chemist');
INSERT INTO `exam` VALUES ('5', 'Sociology');
-- ----------------------------
-- Table structure for exam_results
-- ----------------------------
CREATE TABLE `exam_results` (
`id` int(10) NOT NULL auto_increment,
`student_id` int(10) NOT NULL,
`exam_id` int(10) NOT NULL,
`result` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `exam_results` VALUES ('1', '1', '1', '8');
INSERT INTO `exam_results` VALUES ('2', '1', '2', '7');
INSERT INTO `exam_results` VALUES ('3', '1', '4', '6');
INSERT INTO `exam_results` VALUES ('4', '1', '5', '9');
INSERT INTO `exam_results` VALUES ('5', '2', '1', '5');
INSERT INTO `exam_results` VALUES ('6', '2', '2', '8');
INSERT INTO `exam_results` VALUES ('7', '2', '3', '8');
INSERT INTO `exam_results` VALUES ('8', '2', '4', '6');
INSERT INTO `exam_results` VALUES ('9', '2', '5', '7');
INSERT INTO `exam_results` VALUES ('10', '3', '1', '7');
INSERT INTO `exam_results` VALUES ('11', '3', '2', '9');
INSERT INTO `exam_results` VALUES ('12', '3', '3', '7');
INSERT INTO `exam_results` VALUES ('13', '3', '5', '9');
-- ----------------------------
-- Table structure for students
-- ----------------------------
CREATE TABLE `students` (
`student_id` int(10) NOT NULL auto_increment,
`student_name` varchar(50) NOT NULL,
PRIMARY KEY (`student_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `students` VALUES ('1', 'John');
INSERT INTO `students` VALUES ('2', 'Mark');
INSERT INTO `students` VALUES ('3', 'Donny');
-
- -- ----------------------------
- -- Table structure for exam
- -- ----------------------------
- CREATE TABLE `exam` (
- `exam_id` int(10) NOT NULL auto_increment,
- `exam_name` varchar(50) NOT NULL,
- PRIMARY KEY (`exam_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-
- -- ----------------------------
- -- Records
- -- ----------------------------
- INSERT INTO `exam` VALUES ('1', 'Biology');
- INSERT INTO `exam` VALUES ('2', 'Math');
- INSERT INTO `exam` VALUES ('3', 'Physics');
- INSERT INTO `exam` VALUES ('4', 'Chemist');
- INSERT INTO `exam` VALUES ('5', 'Sociology');
- -- ----------------------------
- -- Table structure for exam_results
- -- ----------------------------
- CREATE TABLE `exam_results` (
- `id` int(10) NOT NULL auto_increment,
- `student_id` int(10) NOT NULL,
- `exam_id` int(10) NOT NULL,
- `result` int(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-
- -- ----------------------------
- -- Records
- -- ----------------------------
- INSERT INTO `exam_results` VALUES ('1', '1', '1', '8');
- INSERT INTO `exam_results` VALUES ('2', '1', '2', '7');
- INSERT INTO `exam_results` VALUES ('3', '1', '4', '6');
- INSERT INTO `exam_results` VALUES ('4', '1', '5', '9');
- INSERT INTO `exam_results` VALUES ('5', '2', '1', '5');
- INSERT INTO `exam_results` VALUES ('6', '2', '2', '8');
- INSERT INTO `exam_results` VALUES ('7', '2', '3', '8');
- INSERT INTO `exam_results` VALUES ('8', '2', '4', '6');
- INSERT INTO `exam_results` VALUES ('9', '2', '5', '7');
- INSERT INTO `exam_results` VALUES ('10', '3', '1', '7');
- INSERT INTO `exam_results` VALUES ('11', '3', '2', '9');
- INSERT INTO `exam_results` VALUES ('12', '3', '3', '7');
- INSERT INTO `exam_results` VALUES ('13', '3', '5', '9');
- -- ----------------------------
- -- Table structure for students
- -- ----------------------------
- CREATE TABLE `students` (
- `student_id` int(10) NOT NULL auto_increment,
- `student_name` varchar(50) NOT NULL,
- PRIMARY KEY (`student_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-
- -- ----------------------------
- -- Records
- -- ----------------------------
- INSERT INTO `students` VALUES ('1', 'John');
- INSERT INTO `students` VALUES ('2', 'Mark');
- INSERT INTO `students` VALUES ('3', 'Donny');
-
1. Some data was stored in wrong columns:
John should have no "Physics" exam result, not "Sociology".
So do Donny, he has no "Chemist" exam result, not "Sociology"
<table border="1px" style="width: 100%;">
<tr>
<td>
<p>Student</p>
</td>
<td>
<p>Biology</p>
</td>
<td>
<p>Math</p>
</td>
<td>
<p>Physics</p>
</td>
<td>
<p>Chemist</p>
</td>
<td>
<p>Sociology</p>
</td>
</tr>
<tr>
<td>
John
</td>
<td>
8
</td>
<td>
7
</td>
<td>
6
</td>
<td>
9
</td>
</tr>
<tr>
<td>
Mark
</td>
<td>
5
</td>
<td>
8
</td>
<td>
8
</td>
<td>
6
</td>
<td>
7
</td>
</tr>
<tr>
<td>
Donny
</td>
<td>
7
</td>
<td>
9
</td>
<td>
7
</td>
<td>
9
</td>
</tr>
</table>
- <table border="1px" style="width: 100%;">
- <tr>
- <td>
- <p>Student</p>
- </td>
- <td>
- <p>Biology</p>
- </td>
- <td>
- <p>Math</p>
- </td>
- <td>
- <p>Physics</p>
- </td>
- <td>
- <p>Chemist</p>
- </td>
- <td>
- <p>Sociology</p>
- </td>
- </tr>
- <tr>
- <td>
- John
- </td>
- <td>
- 8
- </td>
- <td>
- 7
- </td>
- <td>
- 6
- </td>
- <td>
- 9
- </td>
- </tr>
- <tr>
- <td>
- Mark
- </td>
- <td>
- 5
- </td>
- <td>
- 8
- </td>
- <td>
- 8
- </td>
- <td>
- 6
- </td>
- <td>
- 7
- </td>
- </tr>
- <tr>
- <td>
- Donny
- </td>
- <td>
- 7
- </td>
- <td>
- 9
- </td>
- <td>
- 7
- </td>
- <td>
- 9
- </td>
- </tr>
- </table>
-
2. Your code needs too much MySQL queries. If we work w/ big-size data, our server will be "timed out || not responding || data will be loaded uncompletely".
For example; we have 1,000,000,000 students, and 50 exams.
we need to request exam_result.result for 50,000,000,000 times
(but I wont to try this

)
You use looping inside a looping (while in while).
$students = mysql_query("SELECT * FROM students");
while($student = mysql_fetch_assoc($students))
{
...
$process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
$table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
while($row = mysql_fetch_assoc($process))
{
...
-
- $students = mysql_query("SELECT * FROM students");
- while($student = mysql_fetch_assoc($students))
- {
- ...
- $process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
- $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
- while($row = mysql_fetch_assoc($process))
- {
- ...
-
Would you write the data in array to make it simple?
$line[1]="$row[John] </td><td>$row[John][Biology]</td><td>$row[John][Math]...";
I mean, combining (w/ some modification, offcourse) MySQL Queries w/ script like this:
<?
$array = array(
array("App Name 1", "App Name 2", "App Name 3", "App Name 4", "App Name 5", "App Name 6"),
array("department 1", "App Name 2", "App Name 5", "App Name 6"),
array("department 2", "App Name 1", "App Name 2", "App Name 4"),
array("department 3", "App Name 1", "App Name 2", "App Name 3", "App Name 5", "App Name 6"),
);
//headers:
$headers = $array[0];
echo "<table border=1>
<tr>
<td>Department</td>\n";
foreach ( $headers AS $header ) {
echo "\t\t<td>$header</td>\n";
}
echo "\t</tr>\n";
//loop through the remainder of the array
$end = count($array);
for ( $p = 1; $p < $end; $p++ ) {
//echo the department:
echo "\t<tr>
<td>" . $array[$p][0] . "</td>\n";
//loop through the headers, print an X in the td if present in $array[$p]
foreach ( $headers AS $header ) {
echo "\t\t<td>";
if ( in_array($header, $array[$p]) ) {
echo "X";
} else {
echo " ";
}
echo "</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>";
?>
-
- <?
- $array = array(
- array("App Name 1", "App Name 2", "App Name 3", "App Name 4", "App Name 5", "App Name 6"),
- array("department 1", "App Name 2", "App Name 5", "App Name 6"),
- array("department 2", "App Name 1", "App Name 2", "App Name 4"),
- array("department 3", "App Name 1", "App Name 2", "App Name 3", "App Name 5", "App Name 6"),
- );
-
- //headers:
- $headers = $array[0];
- echo "<table border=1>
- <tr>
- <td>Department</td>\n";
- foreach ( $headers AS $header ) {
- echo "\t\t<td>$header</td>\n";
- }
- echo "\t</tr>\n";
-
- //loop through the remainder of the array
- $end = count($array);
- for ( $p = 1; $p < $end; $p++ ) {
- //echo the department:
- echo "\t<tr>
- <td>" . $array[$p][0] . "</td>\n";
- //loop through the headers, print an X in the td if present in $array[$p]
- foreach ( $headers AS $header ) {
- echo "\t\t<td>";
- if ( in_array($header, $array[$p]) ) {
- echo "X";
- } else {
- echo " ";
- }
- echo "</td>\n";
- }
- echo "\t</tr>\n";
- }
- echo "</table>";
- ?>
-
Thanks brothers!