PHP-MySQL: Need Help to Create Multidimensional HTML Table

  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

friends, i wanna print datas from MySQL into html with PHP like this:

===============================================================================
student | Biology | Math | Physics | Chemist | Sociology | average
===============================================================================
John | 8 | 7 | - | 6 | 9 |
Mark | 5 | 8 | 8 | 6 | 7 |
Donny | 7 | 9 | 7 | - | 9 |
================================================================================
Total | | | | | |
================================================================================

where the datas are taken from 3 tables (MySQL):

`students` table:
---------
student_id, student_name
---------------------------
1 John
2 Mark
3 Donny

`exam` table:
-----------
exam_id, exam_name
-------------------
1. Biology
2. Math
3. Physics
4. Chemist
5. Sociology

ada

`exam_result` table:
------------
student_name, exam_name, exam_result
--------------------------------------
John Biology 8
...


How can i do this whith php_array?

I tried to display the exam_result using:

$exam_result[student][exam]

but it fails.

Please help, bros...

Thx


oom Donny yang Guanteng
From Jakarta w/ love & peace
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Okay phplover, I think you could set up the database way better to make life easier for you ... here's how I would set up that Database and the code I would use to get the results ...
Code: [ Select ]
students
student_id - BIGINT,auto-increment
student_name - VARCHAR (100)
exam
exam_id - BIGINT auto-increment
exam name - VARCHAR (100)
exam_results
student_id - BIGINT
exam_id - BIGINT
result - INT(3)
  1. students
  2. student_id - BIGINT,auto-increment
  3. student_name - VARCHAR (100)
  4. exam
  5. exam_id - BIGINT auto-increment
  6. exam name - VARCHAR (100)
  7. exam_results
  8. student_id - BIGINT
  9. exam_id - BIGINT
  10. result - INT(3)


and a query to get the results would be:
Code: [ Select ]
SELECT student.student_name,exam.exam_name,exam_results.result FROM students LEFT JOIN exam_result ON student.student_id = exam_result.student_id LEFT JOIN exam ON exam.exam_id = exam_result.exam_id ORDER BY student.student_id,exam.exam_name

This way you'll get something like the following:
John - English - 80
John - Math - 72
Mary - English - 60
Mary - English - 23

hope this helped a little ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

Thanks, righteous_trespasser.
But it's not easy as joining query from one or more table only.
I mean, the headers (exam names) are data in `exam_name` table, and i need to place the exam_result datas to be match to their columns and rows.
The Column headers are: `exam_name` data
The Row headers Are: `student_name` data


So, the table structure should be displayed like:
Code: [ Select ]
 
----------------------------------------------------------
student | Biology | Math | Physics | Chemist | Sociology |
==========================================================
John    | 8       | 7    | -       | 6       | 9         |
Mark    | 5       | 8    | 8       | 6       | 7         |
Donny   | 7       | 9    | 7       | -       | 9         |
==========================================================
Total   | 20      | 24   | 15      | 12      | 25        |
==========================================================
// `exam_name` will be columns header
// `student_name` will be rows header
// `exam_result` will be data that is match to its column and rows.
 
  1.  
  2. ----------------------------------------------------------
  3. student | Biology | Math | Physics | Chemist | Sociology |
  4. ==========================================================
  5. John    | 8       | 7    | -       | 6       | 9         |
  6. Mark    | 5       | 8    | 8       | 6       | 7         |
  7. Donny   | 7       | 9    | 7       | -       | 9         |
  8. ==========================================================
  9. Total   | 20      | 24   | 15      | 12      | 25        |
  10. ==========================================================
  11. // `exam_name` will be columns header
  12. // `student_name` will be rows header
  13. // `exam_result` will be data that is match to its column and rows.
  14.  

How to make table structure like this w/ PHP?
Thanks for stay helping.

PhpLover
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

I'd let php worry about that actually ... And not try to get the table exactly like that because as far as my knowledge stretches that isn't possible ... here's a simple example ...
Code: [ Select ]
$con = mysql_connect("localhost","username","password");
mysql_select_db("db_name");
$sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_result.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY user_id, exam_id");
$temp1 = "";
$temp2 = "";
echo "student | Biology | Math | Physics | Chemist | Sociology |";
while($row = mysql_fetch_array($sql))
{
$temp1 = $row['user_id'];
if($temp1 == temp2)
{
echo $row['exam_result'] . " |";
}
else
{
echo $row['user_name'] . " |" . $row['exam_result']
}
$temp2 = $row['user_id'];
}
mysql_close($con);
  1. $con = mysql_connect("localhost","username","password");
  2. mysql_select_db("db_name");
  3. $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_result.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY user_id, exam_id");
  4. $temp1 = "";
  5. $temp2 = "";
  6. echo "student | Biology | Math | Physics | Chemist | Sociology |";
  7. while($row = mysql_fetch_array($sql))
  8. {
  9. $temp1 = $row['user_id'];
  10. if($temp1 == temp2)
  11. {
  12. echo $row['exam_result'] . " |";
  13. }
  14. else
  15. {
  16. echo $row['user_name'] . " |" . $row['exam_result']
  17. }
  18. $temp2 = $row['user_id'];
  19. }
  20. mysql_close($con);


Only thing that's missing here is working out the totals ... If you don't quite get what I did please ask and I'll explain in more detail ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

The totals values is not too difficult. But printing results regarding its column and rows i ever heard before, it could be done with creating multidimensional array like this:
Code: [ Select ]
 
<?
 $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 "&nbsp;";
        }
        echo "</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>";  
?>
 
  1.  
  2. <?
  3.  $array = array(
  4.     array("App Name 1", "App Name 2", "App Name 3", "App Name 4", "App Name 5", "App Name 6"),
  5.     array("department 1", "App Name 2", "App Name 5", "App Name 6"),
  6.     array("department 2", "App Name 1", "App Name 2", "App Name 4"),
  7.     array("department 3", "App Name 1", "App Name 2", "App Name 3", "App Name 5", "App Name 6"),
  8.     );
  9.      
  10. //headers:
  11. $headers = $array[0];
  12. echo "<table border=1>
  13.     <tr>
  14.         <td>Department</td>\n";
  15. foreach ( $headers AS $header ) {
  16.     echo "\t\t<td>$header</td>\n";
  17. }
  18. echo "\t</tr>\n";
  19.  
  20. //loop through the remainder of the array
  21. $end = count($array);
  22. for ( $p = 1; $p < $end; $p++ ) {
  23.     //echo the department:
  24.     echo "\t<tr>
  25.         <td>" . $array[$p][0] . "</td>\n";
  26.     //loop through the headers, print an X in the td if present in $array[$p]
  27.     foreach ( $headers AS $header ) {
  28.         echo "\t\t<td>";
  29.         if ( in_array($header, $array[$p]) ) {
  30.             echo "X";
  31.         } else {
  32.             echo "&nbsp;";
  33.         }
  34.         echo "</td>\n";
  35.     }
  36.     echo "\t</tr>\n";
  37. }
  38. echo "</table>";  
  39. ?>
  40.  

The above code produces:
Code: [ Select ]
 
<table border=1>
    <tr>
        <td>Department</td>
        <td>App Name 1</td>
        <td>App Name 2</td>
        <td>App Name 3</td>
        <td>App Name 4</td>
        <td>App Name 5</td>
        <td>App Name 6</td>
    </tr>
    <tr>
        <td>department 1</td>
        <td>&nbsp;</td>
        <td>X</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>X</td>
        <td>X</td>
    </tr>
    <tr>
        <td>department 2</td>
        <td>X</td>
        <td>X</td>
        <td>&nbsp;</td>
        <td>X</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>department 3</td>
        <td>X</td>
        <td>X</td>
        <td>X</td>
        <td>&nbsp;</td>
        <td>X</td>
        <td>X</td>
    </tr>
</table>
 
  1.  
  2. <table border=1>
  3.     <tr>
  4.         <td>Department</td>
  5.         <td>App Name 1</td>
  6.         <td>App Name 2</td>
  7.         <td>App Name 3</td>
  8.         <td>App Name 4</td>
  9.         <td>App Name 5</td>
  10.         <td>App Name 6</td>
  11.     </tr>
  12.     <tr>
  13.         <td>department 1</td>
  14.         <td>&nbsp;</td>
  15.         <td>X</td>
  16.         <td>&nbsp;</td>
  17.         <td>&nbsp;</td>
  18.         <td>X</td>
  19.         <td>X</td>
  20.     </tr>
  21.     <tr>
  22.         <td>department 2</td>
  23.         <td>X</td>
  24.         <td>X</td>
  25.         <td>&nbsp;</td>
  26.         <td>X</td>
  27.         <td>&nbsp;</td>
  28.         <td>&nbsp;</td>
  29.     </tr>
  30.     <tr>
  31.         <td>department 3</td>
  32.         <td>X</td>
  33.         <td>X</td>
  34.         <td>X</td>
  35.         <td>&nbsp;</td>
  36.         <td>X</td>
  37.         <td>X</td>
  38.     </tr>
  39. </table>
  40.  

I only need to load the datas from 3 mysql tables into the table. But all i need is to make the following (minimum):
Code: [ Select ]
 
----------------------------------------------------------
student | Biology | Math | Physics | Chemist | Sociology |
==========================================================
John    | 8       | 7    | -       | 6       | 9         |
Mark    | 5       | 8    | 8       | 6       | 7         |
Donny   | 7       | 9    | 7       | -       | 9         |
 
 
  1.  
  2. ----------------------------------------------------------
  3. student | Biology | Math | Physics | Chemist | Sociology |
  4. ==========================================================
  5. John    | 8       | 7    | -       | 6       | 9         |
  6. Mark    | 5       | 8    | 8       | 6       | 7         |
  7. Donny   | 7       | 9    | 7       | -       | 9         |
  8.  
  9.  

or
Code: [ Select ]
HOSTING PLANS
----------------------------------------------------
Features/Plan US-MICRO US-MINI US-STARTER US-BASIC
====================================================
Disk Space          1 MB 25 MB 50 MB 100 MB
Bandwidth / Bulan 150 MB 750 MB 1.5 GB     3 GB
Monthly             899,- 2.500,- 5.000,- 8.500,-
Setup Fee         FREE     FREE      FREE     FREE
=====================================================

where it comes from 3 tables like:
plan_name, features_name, and plan_features.
  1. HOSTING PLANS
  2. ----------------------------------------------------
  3. Features/Plan US-MICRO US-MINI US-STARTER US-BASIC
  4. ====================================================
  5. Disk Space          1 MB 25 MB 50 MB 100 MB
  6. Bandwidth / Bulan 150 MB 750 MB 1.5 GB     3 GB
  7. Monthly             899,- 2.500,- 5.000,- 8.500,-
  8. Setup Fee         FREE     FREE      FREE     FREE
  9. =====================================================
  10. where it comes from 3 tables like:
  11. plan_name, features_name, and plan_features.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

The code I showed you will return this:
Code: [ Select ]
 
----------------------------------------------------------
student | Biology | Math | Physics | Chemist | Sociology |
==========================================================
John    | 8       | 7    | -       | 6       | 9         |
Mark    | 5       | 8    | 8       | 6       | 7         |
Donny   | 7       | 9    | 7       | -       | 9         |
 
 
  1.  
  2. ----------------------------------------------------------
  3. student | Biology | Math | Physics | Chemist | Sociology |
  4. ==========================================================
  5. John    | 8       | 7    | -       | 6       | 9         |
  6. Mark    | 5       | 8    | 8       | 6       | 7         |
  7. Donny   | 7       | 9    | 7       | -       | 9         |
  8.  
  9.  

It just needs some fine tuning (placement) ... but I know it'll work ... I have done it before ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

Thanks for your help, brother.
I have tried to use your code but it doesn't work although I've modified some aparts.

Your code will not display data from a new `exam_name`.
I mean, we need always change our php & html code if a new `exam_name` is registered.
These are the tables related I used to run your code:
Code: [ Select ]
 
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 of `students` :
-- ----------------------------
INSERT INTO `students` VALUES ('1', 'John');
INSERT INTO `students` VALUES ('2', 'Mark');
INSERT INTO `students` VALUES ('3', 'Donny');
 
-- =========================================
 
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 of `exam`:
-- ----------------------------
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');
 
-- =========================================
 
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 of `exam_results`:
-- ----------------------------
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');
 
 
  1.  
  2. CREATE TABLE `students` (
  3.   `student_id` int(10) NOT NULL auto_increment,
  4.   `student_name` varchar(50) NOT NULL,
  5.   PRIMARY KEY  (`student_id`)
  6. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  7.  
  8. -- ----------------------------
  9. -- Records of `students` :
  10. -- ----------------------------
  11. INSERT INTO `students` VALUES ('1', 'John');
  12. INSERT INTO `students` VALUES ('2', 'Mark');
  13. INSERT INTO `students` VALUES ('3', 'Donny');
  14.  
  15. -- =========================================
  16.  
  17. CREATE TABLE `exam` (
  18.   `exam_id` int(10) NOT NULL auto_increment,
  19.   `exam_name` varchar(50) NOT NULL,
  20.   PRIMARY KEY  (`exam_id`)
  21. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  22.  
  23. -- ----------------------------
  24. -- Records of `exam`:
  25. -- ----------------------------
  26. INSERT INTO `exam` VALUES ('1', 'Biology');
  27. INSERT INTO `exam` VALUES ('2', 'Math');
  28. INSERT INTO `exam` VALUES ('3', 'Physics');
  29. INSERT INTO `exam` VALUES ('4', 'Chemist');
  30. INSERT INTO `exam` VALUES ('5', 'Sociology');
  31.  
  32. -- =========================================
  33.  
  34. CREATE TABLE `exam_results` (
  35.   `id` int(10) NOT NULL auto_increment,
  36.   `student_id` int(10) NOT NULL,
  37.   `exam_id` int(10) NOT NULL,
  38.   `result` int(10) NOT NULL,
  39.   PRIMARY KEY  (`id`)
  40. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  41.  
  42. -- ----------------------------
  43. -- Records of `exam_results`:
  44. -- ----------------------------
  45. INSERT INTO `exam_results` VALUES ('1', '1', '1', '8');
  46. INSERT INTO `exam_results` VALUES ('2', '1', '2', '7');
  47. INSERT INTO `exam_results` VALUES ('3', '1', '4', '6');
  48. INSERT INTO `exam_results` VALUES ('4', '1', '5', '9');
  49. INSERT INTO `exam_results` VALUES ('5', '2', '1', '5');
  50. INSERT INTO `exam_results` VALUES ('6', '2', '2', '8');
  51. INSERT INTO `exam_results` VALUES ('7', '2', '3', '8');
  52. INSERT INTO `exam_results` VALUES ('8', '2', '4', '6');
  53. INSERT INTO `exam_results` VALUES ('9', '2', '5', '7');
  54. INSERT INTO `exam_results` VALUES ('10', '3', '1', '7');
  55. INSERT INTO `exam_results` VALUES ('11', '3', '2', '9');
  56. INSERT INTO `exam_results` VALUES ('12', '3', '3', '7');
  57. INSERT INTO `exam_results` VALUES ('13', '3', '5', '9');
  58.  
  59.  

Then, I run Your scripts (some has been modified, please tell me if i do some wrong modification):
Code: [ Select ]
 
<?php
$con = mysql_connect("localhost","root","mypassword");
mysql_select_db("exam_db");
$sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY students.student_id, exam.exam_id")or die(mysql_error());
$temp1 = "";
$temp2 = "";
echo "student | Biology | Math | Physics | Chemist | Sociology |";
    while($row = mysql_fetch_array($sql))
    {
        $temp1 = $row['student_id'];
        if($temp1 == temp2)
        {
            echo $row['result'] . "   |<br />";
        }
        else
        {
            echo $row['student_name'] . "   |" . $row['result']."<br />";
        }
        $temp2 = $row['student_id'];
    }
mysql_close($con);
 
?>
 
  1.  
  2. <?php
  3. $con = mysql_connect("localhost","root","mypassword");
  4. mysql_select_db("exam_db");
  5. $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY students.student_id, exam.exam_id")or die(mysql_error());
  6. $temp1 = "";
  7. $temp2 = "";
  8. echo "student | Biology | Math | Physics | Chemist | Sociology |";
  9.     while($row = mysql_fetch_array($sql))
  10.     {
  11.         $temp1 = $row['student_id'];
  12.         if($temp1 == temp2)
  13.         {
  14.             echo $row['result'] . "   |<br />";
  15.         }
  16.         else
  17.         {
  18.             echo $row['student_name'] . "   |" . $row['result']."<br />";
  19.         }
  20.         $temp2 = $row['student_id'];
  21.     }
  22. mysql_close($con);
  23.  
  24. ?>
  25.  

the above code produce the following:
Code: [ Select ]
 
student | Biology | Math | Physics | Chemist | Sociology |John |8John |7John |6John |9Mark |5Mark |8Mark |8Mark |6Mark |7Donny |7Donny |9Donny |7Donny |9
 
 
  1.  
  2. student | Biology | Math | Physics | Chemist | Sociology |John |8John |7John |6John |9Mark |5Mark |8Mark |8Mark |6Mark |7Donny |7Donny |9Donny |7Donny |9
  3.  
  4.  

"The table header and all datas are in one row."
You said that it returned:
Code: [ Select ]
 
----------------------------------------------------------
student | Biology | Math | Physics | Chemist | Sociology |
==========================================================
John    | 8       | 7    | -       | 6       | 9         |
Mark    | 5       | 8    | 8       | 6       | 7         |
Donny   | 7       | 9    | 7       | -       | 9         |
----------------------------------------------------------
 
  1.  
  2. ----------------------------------------------------------
  3. student | Biology | Math | Physics | Chemist | Sociology |
  4. ==========================================================
  5. John    | 8       | 7    | -       | 6       | 9         |
  6. Mark    | 5       | 8    | 8       | 6       | 7         |
  7. Donny   | 7       | 9    | 7       | -       | 9         |
  8. ----------------------------------------------------------
  9.  

Sorry, but I think it doesn't.
 
1st reason:
------------
John[Physics] is empty, and (your) John[Chemist] value will place (your) empty John[Physics]'s column. It's mean, some datas aren't placedin the right columns (and rows).
 
 
2nd reason:
-----------
Your table headers (Biology, Math, Physics, Chemist, Sociology) is written in static HTML, it's not data from `exam` table.
This will force you work hard if any additional on `exam` table.
You have to place the exam_result value matching its column.
=========
 
There's only one method to place the result into its correct row and column, using array in array (multidimensional array):
Code: [ Select ]
 
echo "$result[student_name] | $result[student_name][Biology] | $result[student_name][Math] ...and so on";
 
  1.  
  2. echo "$result[student_name] | $result[student_name][Biology] | $result[student_name][Math] ...and so on";
  3.  

It's almost like the code i posted before. The data will be placed in the right place.

The question is:
How to make that like data using MySQL Data (3 tables)?

PhpLover
Donny Tjandra
http://www.webprogrammer.asia
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Okay, I think you didn't quite understand my code so here we go ... I am going to stop quickly and write it for you ... here we go ...

Code: [ Select ]
<?php
$con = mysql_connect("localhost","username","password");
mysql_select_db("db_name");
$sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_result.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY user_id, exam_id");
$sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
$temp1 = "";
$temp2 = "";
$count = 0;
echo "<table><tr><td>Students</td>";
while($row = mysql_fetch_array($sql2))
{
echo "<td>" . $row['exam_name'] . "</td>";
}
echo "</tr>";
while($row = mysql_fetch_array($sql))
{
$temp1 = $row['user_id'];
if($temp1 == temp2)
{
echo "<td>" . $row['exam_result'] . "</td>";
}
else
{
if ($count == 0)
{
echo "<tr><td>" . $row['user_name'] . "</td><td>" . $row['exam_result'] . "</td>";
$count ++;
}
else
{
echo "</tr><tr><td>" . $row['user_name'] . "</td><td>" . $row['exam_result'] . "</td>";
}
}
$temp2 = $row['user_id'];
}
echo "</table>";
mysql_close($con);
?>
  1. <?php
  2. $con = mysql_connect("localhost","username","password");
  3. mysql_select_db("db_name");
  4. $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_result.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY user_id, exam_id");
  5. $sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
  6. $temp1 = "";
  7. $temp2 = "";
  8. $count = 0;
  9. echo "<table><tr><td>Students</td>";
  10. while($row = mysql_fetch_array($sql2))
  11. {
  12. echo "<td>" . $row['exam_name'] . "</td>";
  13. }
  14. echo "</tr>";
  15. while($row = mysql_fetch_array($sql))
  16. {
  17. $temp1 = $row['user_id'];
  18. if($temp1 == temp2)
  19. {
  20. echo "<td>" . $row['exam_result'] . "</td>";
  21. }
  22. else
  23. {
  24. if ($count == 0)
  25. {
  26. echo "<tr><td>" . $row['user_name'] . "</td><td>" . $row['exam_result'] . "</td>";
  27. $count ++;
  28. }
  29. else
  30. {
  31. echo "</tr><tr><td>" . $row['user_name'] . "</td><td>" . $row['exam_result'] . "</td>";
  32. }
  33. }
  34. $temp2 = $row['user_id'];
  35. }
  36. echo "</table>";
  37. mysql_close($con);
  38. ?>

Do you need me to explain to you what I did?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

:lol: Alright... I decided to barge in here and interrupt your conversation with R_T and phplover.

Is OOP (Object Oriented Programming)...(A class in this case) a too much approach for this? Is that an overkill for something like this?

I mean, I started typing a class for this and I'm half-way done but now that I think of it... is it too much?

Or would you still try it? :lol:

[EDIT:] Actually I'm done with it...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

Forget CLASSES... it's not worth it for this case... I redid it into one function without the class but the damn thing doesn't want to work :lol:

Shows the score for all of them for only 1 exam and the rest don't get a chance haha can't figure out the problem as the problem is a bit more advanced that I am... (Error on line 30... )
Quote:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\test_exams\exams.class.php on line 30

and line 29+ is...
Quote:
$score = mysql_query("SELECT * FROM exam_results WHERE student_id = '{$e_st}' AND exam_id = '{$e_id}'");
while($row = mysql_fetch_assoc($score))
{

Anyway... here is the function... I'm giving up on it now :lol: Spent more than a freaking hour on it :shock: Should get started on my own project :D Maybe R_T would look over it...
PHP Code: [ Select ]
<?php
 function exams() {
   $con = mysql_connect('localhost','user','pass') or die(mysql_error());
   $select = mysql_select_db('database') 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}'");
      $e = mysql_fetch_assoc($process);
      $e_id = $e['exam_id'];
      $e_st = $e['student_id'];
      $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
      $score = mysql_query("SELECT * FROM exam_results WHERE student_id = '{$e_st}' AND exam_id = '{$e_id}'");
      while($row = mysql_fetch_assoc($score))
      {
       $score = $row['result'];
       if(empty($score))
       {
         $score = "--";
       }
       $table .= "\t\t<td>\n\t\t\t{$score}\n\t\t</td>\n";
      }
      $table .= "\t</tr>\n";
    }
    $table .= "</table>";
 
   echo $table;
   }
 }
?>
  1. <?php
  2.  function exams() {
  3.    $con = mysql_connect('localhost','user','pass') or die(mysql_error());
  4.    $select = mysql_select_db('database') or die(mysql_error());
  5.    if($con && $select)
  6.    {
  7.     $sql = "SELECT `exam_name` FROM exam ORDER BY exam_id";
  8.     $exams = mysql_query($sql);
  9.     // Get the table started
  10.     $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";
  11.     while($exam = mysql_fetch_assoc($exams))
  12.     {
  13.       $e_name = $exam['exam_name'];
  14.       $table .= "\t\t<td>\n\t\t\t<p>{$e_name}</p>\n\t\t</td>\n";
  15.     }
  16.     $table .= "\t</tr>\n";
  17.  
  18.     $students = mysql_query("SELECT * FROM students");
  19.     while($student = mysql_fetch_assoc($students))
  20.     {
  21.       $st_id = $student['student_id'];
  22.       $st_name = $student['student_name'];
  23.  
  24.       $process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
  25.       $e = mysql_fetch_assoc($process);
  26.       $e_id = $e['exam_id'];
  27.       $e_st = $e['student_id'];
  28.       $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
  29.       $score = mysql_query("SELECT * FROM exam_results WHERE student_id = '{$e_st}' AND exam_id = '{$e_id}'");
  30.       while($row = mysql_fetch_assoc($score))
  31.       {
  32.        $score = $row['result'];
  33.        if(empty($score))
  34.        {
  35.          $score = "--";
  36.        }
  37.        $table .= "\t\t<td>\n\t\t\t{$score}\n\t\t</td>\n";
  38.       }
  39.       $table .= "\t</tr>\n";
  40.     }
  41.     $table .= "</table>";
  42.  
  43.    echo $table;
  44.    }
  45.  }
  46. ?>

(The tabs (\t) between the table tags [e.g: <table>,<tr>,<td>...] are to make the source look cleaner...)

Have fun and hope you get it to work...

@R_T: I tried your code and it gives me the same forsaken error that I get...

BTW phplover: Thanks for that example database thing... helped me test the script(s).
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

That error usually shows up when you use an incorrect query ... I didn't have time to go set up that database and actually test my code ... there may just be a typo or something ...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

Alright... I fixed that problem and made another one... hang on as I fix it...

[EDIT:] Here is the fixed and completely working version (The fix was actually an easy one... I actually had everything correct, just that I didn't put the right SQL process into that while(); loop and had a little too much coding...)

PHP Code: [ Select ]
<?php
 function exams() {
   $con = mysql_connect('localhost','user','pass') or die(mysql_error());
   $select = mysql_select_db('database') 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;
   }
 }
?>
  1. <?php
  2.  function exams() {
  3.    $con = mysql_connect('localhost','user','pass') or die(mysql_error());
  4.    $select = mysql_select_db('database') or die(mysql_error());
  5.    if($con && $select)
  6.    {
  7.     $sql = "SELECT `exam_name` FROM exam ORDER BY exam_id";
  8.     $exams = mysql_query($sql);
  9.     // Get the table started
  10.     $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";
  11.     while($exam = mysql_fetch_assoc($exams))
  12.     {
  13.       $e_name = $exam['exam_name'];
  14.       $table .= "\t\t<td>\n\t\t\t<p>{$e_name}</p>\n\t\t</td>\n";
  15.     }
  16.     $table .= "\t</tr>\n";
  17.  
  18.     $students = mysql_query("SELECT * FROM students");
  19.     while($student = mysql_fetch_assoc($students))
  20.     {
  21.       $st_id = $student['student_id'];
  22.       $st_name = $student['student_name'];
  23.  
  24.       $process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
  25.       $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
  26.       while($row = mysql_fetch_assoc($process))
  27.       {
  28.        $score = $row['result'];
  29.        $table .= "\t\t<td>\n\t\t\t{$score}\n\t\t</td>\n";
  30.       }
  31.       $table .= "\t</tr>\n";
  32.     }
  33.     $table .= "</table>";
  34.  
  35.    echo $table;
  36.    }
  37.  }
  38. ?>


Enjoy :D I know for sure that this works as I have tested it... Just one drawback to it that I'm trying to fix...

That drawback is that it doesn't do a <td>...</td> when the score is empty...
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

So Bogey what's the difference between your code and mine ... ? Because mine will actually still add a <td></td> if the exam result is empty ...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

righteous_trespasser wrote:
So Bogey what's the difference between your code and mine ... ? Because mine will actually still add a <td></td> if the exam result is empty ...

Probably because mine works? :lol: I tested your script and tried to fix the error...

Not only that this is my version :D Probably no real difference...

And those missing <td>...</td> don't make the code invalid... it's still xhtml strict valid code... the only reason I called it a drawback because I didn't knew it was valid... until now :)
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Yeah, but if you don't show the empty <td>'s you'll end up showing the wrong data for the wrong columns ... for example
Code: [ Select ]
<tr><td>Math</td><td>Biol</td></tr>
<tr><td>0090</td><td>0050</td></tr>
<tr><td>0021</td></tr>
  1. <tr><td>Math</td><td>Biol</td></tr>
  2. <tr><td>0090</td><td>0050</td></tr>
  3. <tr><td>0021</td></tr>

The second person only has a mark for Biol (Biology) but it will show under math because the code didn't add the empty TD ... see where the problem comes in, and then also that code won't validate because the second user's row doesn't contain enough TDs ... get what I'm saying ... ?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

I edited my code a tiny bit and fixed that error, and came up with this ... it's not quite right yet, as you can see, John doesn't have Physics but it doesn't skip that row, and the same goes for Donny that doesn't have Chemist, so that row doesn't get skipped ... the only way to get around this without adding heaps of code is to actually just insert a 0 for each exam that wasn't taken ... then that data would display correctly ...
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Updated ... now look at the difference when I add the zeros ... there
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

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:
Code: [ Select ]
 
<?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();
?>
 
 
  1.  
  2. <?php
  3.   function exams() {
  4.     $con = mysql_connect('localhost','root','myrootpassword') or die(mysql_error());
  5.     $select = mysql_select_db('dtbs') or die(mysql_error());
  6.     if($con && $select)
  7.     {
  8.       $sql = "SELECT `exam_name` FROM exam ORDER BY exam_id";
  9.       $exams = mysql_query($sql);
  10.       // Get the table started
  11.       $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";
  12.       while($exam = mysql_fetch_assoc($exams))
  13.       {
  14.         $e_name = $exam['exam_name'];
  15.         $table .= "\t\t<td>\n\t\t\t<p>{$e_name}</p>\n\t\t</td>\n";
  16.       }
  17.       $table .= "\t</tr>\n";
  18.  
  19.       $students = mysql_query("SELECT * FROM students");
  20.       while($student = mysql_fetch_assoc($students))
  21.       {
  22.         $st_id = $student['student_id'];
  23.         $st_name = $student['student_name'];
  24.  
  25.         $process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
  26.         $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
  27.         while($row = mysql_fetch_assoc($process))
  28.         {
  29.           $score = $row['result'];
  30.           $table .= "\t\t<td>\n\t\t\t{$score}\n\t\t</td>\n";
  31.         }
  32.         $table .= "\t</tr>\n";
  33.       }
  34.       $table .= "</table>";
  35.  
  36.     echo $table;
  37.     }
  38.   }
  39.     // Call the function you have made above :)
  40.     exams();
  41. ?>
  42.  
  43.  

and calling my database:
Code: [ Select ]
 
-- ----------------------------
-- 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.  
  2. -- ----------------------------
  3. -- Table structure for exam
  4. -- ----------------------------
  5. CREATE TABLE `exam` (
  6.   `exam_id` int(10) NOT NULL auto_increment,
  7.   `exam_name` varchar(50) NOT NULL,
  8.   PRIMARY KEY  (`exam_id`)
  9. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  10.  
  11. -- ----------------------------
  12. -- Records
  13. -- ----------------------------
  14. INSERT INTO `exam` VALUES ('1', 'Biology');
  15. INSERT INTO `exam` VALUES ('2', 'Math');
  16. INSERT INTO `exam` VALUES ('3', 'Physics');
  17. INSERT INTO `exam` VALUES ('4', 'Chemist');
  18. INSERT INTO `exam` VALUES ('5', 'Sociology');
  19. -- ----------------------------
  20. -- Table structure for exam_results
  21. -- ----------------------------
  22. CREATE TABLE `exam_results` (
  23.   `id` int(10) NOT NULL auto_increment,
  24.   `student_id` int(10) NOT NULL,
  25.   `exam_id` int(10) NOT NULL,
  26.   `result` int(10) NOT NULL,
  27.   PRIMARY KEY  (`id`)
  28. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  29.  
  30. -- ----------------------------
  31. -- Records
  32. -- ----------------------------
  33. INSERT INTO `exam_results` VALUES ('1', '1', '1', '8');
  34. INSERT INTO `exam_results` VALUES ('2', '1', '2', '7');
  35. INSERT INTO `exam_results` VALUES ('3', '1', '4', '6');
  36. INSERT INTO `exam_results` VALUES ('4', '1', '5', '9');
  37. INSERT INTO `exam_results` VALUES ('5', '2', '1', '5');
  38. INSERT INTO `exam_results` VALUES ('6', '2', '2', '8');
  39. INSERT INTO `exam_results` VALUES ('7', '2', '3', '8');
  40. INSERT INTO `exam_results` VALUES ('8', '2', '4', '6');
  41. INSERT INTO `exam_results` VALUES ('9', '2', '5', '7');
  42. INSERT INTO `exam_results` VALUES ('10', '3', '1', '7');
  43. INSERT INTO `exam_results` VALUES ('11', '3', '2', '9');
  44. INSERT INTO `exam_results` VALUES ('12', '3', '3', '7');
  45. INSERT INTO `exam_results` VALUES ('13', '3', '5', '9');
  46. -- ----------------------------
  47. -- Table structure for students
  48. -- ----------------------------
  49. CREATE TABLE `students` (
  50.   `student_id` int(10) NOT NULL auto_increment,
  51.   `student_name` varchar(50) NOT NULL,
  52.   PRIMARY KEY  (`student_id`)
  53. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  54.  
  55. -- ----------------------------
  56. -- Records
  57. -- ----------------------------
  58. INSERT INTO `students` VALUES ('1', 'John');
  59. INSERT INTO `students` VALUES ('2', 'Mark');
  60. INSERT INTO `students` VALUES ('3', 'Donny');
  61.  


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"
Code: [ Select ]
<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>
 
  1. <table border="1px" style="width: 100%;">
  2.     <tr>
  3.         <td>
  4.             <p>Student</p>
  5.         </td>
  6.         <td>
  7.             <p>Biology</p>
  8.         </td>
  9.         <td>
  10.             <p>Math</p>
  11.         </td>
  12.         <td>
  13.             <p>Physics</p>
  14.         </td>
  15.         <td>
  16.             <p>Chemist</p>
  17.         </td>
  18.         <td>
  19.             <p>Sociology</p>
  20.         </td>
  21.     </tr>
  22.     <tr>
  23.         <td>
  24.             John
  25.         </td>
  26.         <td>
  27.             8
  28.         </td>
  29.         <td>
  30.             7
  31.         </td>
  32.         <td>
  33.             6
  34.         </td>
  35.         <td>
  36.             9
  37.         </td>
  38.     </tr>
  39.     <tr>
  40.         <td>
  41.             Mark
  42.         </td>
  43.         <td>
  44.             5
  45.         </td>
  46.         <td>
  47.             8
  48.         </td>
  49.         <td>
  50.             8
  51.         </td>
  52.         <td>
  53.             6
  54.         </td>
  55.         <td>
  56.             7
  57.         </td>
  58.     </tr>
  59.     <tr>
  60.         <td>
  61.             Donny
  62.         </td>
  63.         <td>
  64.             7
  65.         </td>
  66.         <td>
  67.             9
  68.         </td>
  69.         <td>
  70.             7
  71.         </td>
  72.         <td>
  73.             9
  74.         </td>
  75.     </tr>
  76. </table>
  77.  

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).
Code: [ Select ]
 
$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))
        {
          ...
 
  1.  
  2. $students = mysql_query("SELECT * FROM students");
  3.       while($student = mysql_fetch_assoc($students))
  4.       {
  5.         ...
  6.        $process = mysql_query("SELECT * FROM exam_results JOIN exam ON exam_results.exam_id = exam.exam_id WHERE student_id = '{$st_id}'");
  7.         $table .= "\t<tr>\n\t\t<td>\n\t\t\t{$st_name}\n\t\t</td>\n";
  8.         while($row = mysql_fetch_assoc($process))
  9.         {
  10.           ...
  11.  

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:
Code: [ Select ]
 
<?
 $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 "&nbsp;";
        }
        echo "</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>";  
?>
 
  1.  
  2. <?
  3.  $array = array(
  4.     array("App Name 1", "App Name 2", "App Name 3", "App Name 4", "App Name 5", "App Name 6"),
  5.     array("department 1", "App Name 2", "App Name 5", "App Name 6"),
  6.     array("department 2", "App Name 1", "App Name 2", "App Name 4"),
  7.     array("department 3", "App Name 1", "App Name 2", "App Name 3", "App Name 5", "App Name 6"),
  8.     );
  9.      
  10. //headers:
  11. $headers = $array[0];
  12. echo "<table border=1>
  13.     <tr>
  14.         <td>Department</td>\n";
  15. foreach ( $headers AS $header ) {
  16.     echo "\t\t<td>$header</td>\n";
  17. }
  18. echo "\t</tr>\n";
  19.  
  20. //loop through the remainder of the array
  21. $end = count($array);
  22. for ( $p = 1; $p < $end; $p++ ) {
  23.     //echo the department:
  24.     echo "\t<tr>
  25.         <td>" . $array[$p][0] . "</td>\n";
  26.     //loop through the headers, print an X in the td if present in $array[$p]
  27.     foreach ( $headers AS $header ) {
  28.         echo "\t\t<td>";
  29.         if ( in_array($header, $array[$p]) ) {
  30.             echo "X";
  31.         } else {
  32.             echo "&nbsp;";
  33.         }
  34.         echo "</td>\n";
  35.     }
  36.     echo "\t</tr>\n";
  37. }
  38. echo "</table>";  
  39. ?>
  40.  

Thanks brothers!
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

r_t, your latest code displayed CORRECT RESULT!
Code: [ Select ]
Attempt two - With Zeros
Students Biology Math Physics Chemist Sociology
John 8 7 0 6 9
Mark 5 8 8 6 7
Donny 7 9 7 0 9
  1. Attempt two - With Zeros
  2. Students Biology Math Physics Chemist Sociology
  3. John 8 7 0 6 9
  4. Mark 5 8 8 6 7
  5. Donny 7 9 7 0 9

That's I need.
But, where's the php code? :)
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Code: [ Select ]
<?php
$con = mysql_connect("localhost","ohdesign_root","Hennie@OhDesignX.com");
mysql_select_db("ohdesign_M4MobileTest");
$sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY student_id, exam_id");
$sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
$temp1 = "";
$temp2 = "";
$count = 0;
echo "<table><tr><td>Students</td>";
while($row = mysql_fetch_array($sql2))
{
echo "<td>" . $row['exam_name'] . "</td>";
}
echo "</tr>";
while($row = mysql_fetch_array($sql))
{
$temp1 = $row['student_id'];
if($temp1 == $temp2)
{
echo "<td>" . $row['result'] . "</td>";
}
else
{
if ($count == 0)
{
echo "<tr><td>" . $row['student_name'] . "</td><td>" . $row['result'] . "</td>";
$count ++;
}
else
{
echo "</tr><tr><td>" . $row['student_name'] . "</td><td>" . $row['result'] . "</td>";
}
}
$temp2 = $row['student_id'];
}
echo "</table>";
mysql_close($con);
?>
  1. <?php
  2. $con = mysql_connect("localhost","ohdesign_root","Hennie@OhDesignX.com");
  3. mysql_select_db("ohdesign_M4MobileTest");
  4. $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY student_id, exam_id");
  5. $sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
  6. $temp1 = "";
  7. $temp2 = "";
  8. $count = 0;
  9. echo "<table><tr><td>Students</td>";
  10. while($row = mysql_fetch_array($sql2))
  11. {
  12. echo "<td>" . $row['exam_name'] . "</td>";
  13. }
  14. echo "</tr>";
  15. while($row = mysql_fetch_array($sql))
  16. {
  17. $temp1 = $row['student_id'];
  18. if($temp1 == $temp2)
  19. {
  20. echo "<td>" . $row['result'] . "</td>";
  21. }
  22. else
  23. {
  24. if ($count == 0)
  25. {
  26. echo "<tr><td>" . $row['student_name'] . "</td><td>" . $row['result'] . "</td>";
  27. $count ++;
  28. }
  29. else
  30. {
  31. echo "</tr><tr><td>" . $row['student_name'] . "</td><td>" . $row['result'] . "</td>";
  32. }
  33. }
  34. $temp2 = $row['student_id'];
  35. }
  36. echo "</table>";
  37. mysql_close($con);
  38. ?>

There you go ... now just remember that you must add zeros(0) where the person hasn't taken the test ... got it?
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

I mean, the "I edited my code a tiny bit and fixed that error.... + ... when I add the zeros..."
Sorry, i use dial up connection, so i'm not quickly updated.
Internet cost is not cheap in Indonesia. :)
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

That's exactly the code ... That's the one that works ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

Sorry... It's my STUPID question:
where did you insert zero?
i'll try this but still not work:
Code: [ Select ]
 
<?php
$con = mysql_connect("localhost","root","mypass");
mysql_select_db("mydb");
$sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY student_id, exam_id");
$sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
$temp1 = "";
$temp2 = "";
$count = 0;
echo "<table><tr><td>Students</td>";
while($row = mysql_fetch_array($sql2))
{
    echo "<td>" . $row['exam_name'] . "</td>";
}
echo "</tr>";
while($row = mysql_fetch_array($sql))
{
    $temp1 = $row['student_id'];
    if($temp1 == $temp2)
    {
        echo "<td>" . $row['result'] . "</td>";
    }
    else
    {
        if ($count == 0)
        {
        echo "<tr><td>" . $row['student_name'] . "</td><td>" ;
        if(!$row[result])
        {
            echo 0;
        }
        else
        {
            echo $row['result'] ;
        }
        echo "</td>";
        $count ++;
        }
        else
        {
        echo "</tr><tr><td>" . $row['student_name'] . "</td><td>" ;
        if(!$row[result])
        {
            echo 0;
        }
        else
        {
            echo $row['result'] ;
        }
        echo "</td>";
        }
    }
    $temp2 = $row['student_id'];
}
echo "</table>";
mysql_close($con);
?>
  1.  
  2. <?php
  3. $con = mysql_connect("localhost","root","mypass");
  4. mysql_select_db("mydb");
  5. $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY student_id, exam_id");
  6. $sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
  7. $temp1 = "";
  8. $temp2 = "";
  9. $count = 0;
  10. echo "<table><tr><td>Students</td>";
  11. while($row = mysql_fetch_array($sql2))
  12. {
  13.     echo "<td>" . $row['exam_name'] . "</td>";
  14. }
  15. echo "</tr>";
  16. while($row = mysql_fetch_array($sql))
  17. {
  18.     $temp1 = $row['student_id'];
  19.     if($temp1 == $temp2)
  20.     {
  21.         echo "<td>" . $row['result'] . "</td>";
  22.     }
  23.     else
  24.     {
  25.         if ($count == 0)
  26.         {
  27.         echo "<tr><td>" . $row['student_name'] . "</td><td>" ;
  28.         if(!$row[result])
  29.         {
  30.             echo 0;
  31.         }
  32.         else
  33.         {
  34.             echo $row['result'] ;
  35.         }
  36.         echo "</td>";
  37.         $count ++;
  38.         }
  39.         else
  40.         {
  41.         echo "</tr><tr><td>" . $row['student_name'] . "</td><td>" ;
  42.         if(!$row[result])
  43.         {
  44.             echo 0;
  45.         }
  46.         else
  47.         {
  48.             echo $row['result'] ;
  49.         }
  50.         echo "</td>";
  51.         }
  52.     }
  53.     $temp2 = $row['student_id'];
  54. }
  55. echo "</table>";
  56. mysql_close($con);
  57. ?>

it results:
Code: [ Select ]
Students Biology Math Physics Chemist Sociology
John 8 7 6 9
Mark 5 8 8 6 7
Donny 7 9 7 9
 
 
  1. Students Biology Math Physics Chemist Sociology
  2. John 8 7 6 9
  3. Mark 5 8 8 6 7
  4. Donny 7 9 7 9
  5.  
  6.  

again...
:?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

I didn't insert the 0 anywhere in the code I added this to the Database
Code: [ Select ]
INSERT INTO exam_results (student_id,exam_id,result) VALUES ('1','3','0');
INSERT INTO exam_results (student_id,exam_id,result) VALUES ('3','4','0');
  1. INSERT INTO exam_results (student_id,exam_id,result) VALUES ('1','3','0');
  2. INSERT INTO exam_results (student_id,exam_id,result) VALUES ('3','4','0');
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

According me, that's not a good idea for inserting 0 data.
If I was an EDP staff, I wouldn't insert blank exam_result (per student, per exam)... It's a hard job for me if we have a large number of students and exams ( smile ).
We will have database with so many 0 value.
It should be generated automatically by the programmer, not the EDP Staff.
Btw, thx for your method.
It could fix my problem too.
Other methods will be awarded too :)
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

look, there is no other way that I can see to do this ... so take it or leave it ... Unfortunately I don't have time to code your whole site for you ... I gave all the help I can ... It seems to me that you are taking on a job that you do not have the necessary skills for, I'm not saying that this is a bad thing, you can learn a lot from a project like this, but it almost feels as if you're expecting me to code the whole site for you ... And at the end of the day that's not quite fair seeing as you're the one getting paid for this job and I'm neglecting my job to help out ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

yeah, whatever you said, thanks for your helps :))
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

No... no...
You are miss understood.
I realy award Your helps and Bogey's helps.
But if I could not take it after i make a comparison, or I found a bad side of Your method, should you angry?
Please note that NOT all users marked as "newbie" in this forum are realy "newbie" or "plagiator" that will sell your code totaly with no permission at all and saying "Hahaha! I'm rich now by my self, not the Superhero at Ozzu".
Your idea is smart but I, like I was written in the title: "PHP-MySQL: Need Help to Create Multidimensional HTML Table" alway thinking that "Multidimensional Array" is the best way.
You are a "Superhero" aren't You?
Some of phpGuru that "real Guru" I knew, recommend me to use this method (Php Multidimensional array).
I tried, but it always fails. It's not mean I only want Your code, FREEly.
Thanks for your smart :) idea.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

lolz. I'm no superhero ... I started learning php a month ago ... And no pleas do not think I am angry ... I am just saying that this is all that I know ... I don't know more ... hopefully someone else could help you here ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

i hope. peace, bro!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

righteous_trespasser wrote:
Yeah, but if you don't show the empty <td>'s you'll end up showing the wrong data for the wrong columns ... for example
Code: [ Select ]
<tr><td>Math</td><td>Biol</td></tr>
<tr><td>0090</td><td>0050</td></tr>
<tr><td>0021</td></tr>
  1. <tr><td>Math</td><td>Biol</td></tr>
  2. <tr><td>0090</td><td>0050</td></tr>
  3. <tr><td>0021</td></tr>

The second person only has a mark for Biol (Biology) but it will show under math because the code didn't add the empty TD ... see where the problem comes in, and then also that code won't validate because the second user's row doesn't contain enough TDs ... get what I'm saying ... ?

Not unless you specify a <tr></tr> the tds won't go to the empty <td> and that is what your first attempt did and my works almost the same way... if I add 0's it makes everything correct...

Even though, phplover doesn't want that solution... no hard feelings R_T

Here is the array generator ;) Named the function because I used R_T's sqls (I actually edited his function to do this... just cleaned out the source a little bit :) )
PHP Code: [ Select ]
<?php
 function r_t_arrays() {
   $con = mysql_connect('localhost','root','40477810093');
   mysql_select_db('test_exams');
   $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY student_id, exam_id");
   $sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
   $sql3 = mysql_query("SELECT student_id FROM students");
   $temp1 = "";
   $temp2 = "";
   $count = 0;
   $exams = array();
   // Exam names
   while($row = mysql_fetch_array($sql2))
   {
    $e_val .= $row['exam_name'] .", ";
   }
   // All of students' ids
   while($row = mysql_fetch_array($sql3))
   {
    $s_val .= $row['student_id'] .", ";
   }
   // Scores
   while($row = mysql_fetch_array($sql))
   {
    $sc_val .= $row['result'] .", ";
   }
   $sc_val2 = $sc_val .'"';
   $s_val2 = $s_val .'"';
   $e_val2 = $e_val .'"';
   $exams = array(
             "exam_names" => array(
                            trim($e_val2,', "')
                            ),
             "all_students" => array(
                            trim($s_val2,', "')
                            ),
             "scores" => array(
                            trim($sc_val2,', "')
                            )
             );
   echo "<xmp>";
   print_r($exams);
   echo "</xmp>";
   mysql_close($con);
 }
?>
  1. <?php
  2.  function r_t_arrays() {
  3.    $con = mysql_connect('localhost','root','40477810093');
  4.    mysql_select_db('test_exams');
  5.    $sql = mysql_query("SELECT students.student_id, students.student_name, exam.exam_id, exam.exam_name, exam_results.result FROM students LEFT JOIN exam_results ON students.student_id = exam_results.student_id LEFT JOIN exam ON exam_results.exam_id = exam.exam_id ORDER BY student_id, exam_id");
  6.    $sql2 = mysql_query("SELECT DISTINCT exam_name FROM exam ORDER BY exam_id");
  7.    $sql3 = mysql_query("SELECT student_id FROM students");
  8.    $temp1 = "";
  9.    $temp2 = "";
  10.    $count = 0;
  11.    $exams = array();
  12.    // Exam names
  13.    while($row = mysql_fetch_array($sql2))
  14.    {
  15.     $e_val .= $row['exam_name'] .", ";
  16.    }
  17.    // All of students' ids
  18.    while($row = mysql_fetch_array($sql3))
  19.    {
  20.     $s_val .= $row['student_id'] .", ";
  21.    }
  22.    // Scores
  23.    while($row = mysql_fetch_array($sql))
  24.    {
  25.     $sc_val .= $row['result'] .", ";
  26.    }
  27.    $sc_val2 = $sc_val .'"';
  28.    $s_val2 = $s_val .'"';
  29.    $e_val2 = $e_val .'"';
  30.    $exams = array(
  31.              "exam_names" => array(
  32.                             trim($e_val2,', "')
  33.                             ),
  34.              "all_students" => array(
  35.                             trim($s_val2,', "')
  36.                             ),
  37.              "scores" => array(
  38.                             trim($sc_val2,', "')
  39.                             )
  40.              );
  41.    echo "<xmp>";
  42.    print_r($exams);
  43.    echo "</xmp>";
  44.    mysql_close($con);
  45.  }
  46. ?>


The only thing is... I'm not sure how you want the array to look like... there are two different types of info I can store into the "students" sub-array...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: INDONESIA

Post 3+ Months Ago

I found it, but it still uses some of the Bogey's first method. (while in while)
Code: [ Select ]
 
<?php
$host="localhost";
$myuser="root";
$mypass="caroline";
$mydb="student_exam";
mysql_connect($host,$myuser,$mypass);
mysql_select_db($mydb);
// this work only in win32, win64, or depending on php.ini setting:
// (mysql functions converted into a variable)
$sql=mysql_query;
$ros=mysql_fetch_array;
//------------------
 
//create headers:
echo "<table border=1 width=100%>
<tr valign=top bgcolor=#eeeeee>
<td>Student</td>";
$sq=$sql("SELECT * FROM exam ORDER by exam_id")or die(mysql_error());
while($headers=$ros($sq))
{
    echo "<td><div align=center>$headers[exam_name]</td></div>";
}
echo "</tr>";
$sq2=$sql("SELECT * FROM students ORDER BY student_id");
// This uses while(s) in while(s), not prefered :=((
while($students=$ros($sq2))
{
    echo "<tr valign=top><td bgcolor=#eeeeee>$students[student_name]</td>";
    // displaying exam result of selected student
    $meetresult=$sql("SELECT s.student_name AS name,e.exam_name AS exam,r.result FROM students s JOIN  exam e LEFT JOIN exam_results r ON r.student_id=s.student_id AND r.exam_id=e.exam_id WHERE s.student_id='$students[student_id]' ORDER BY e.exam_id");
    while($result=$ros($meetresult))
    {
        if(!$result[result])
        {
        // no need insert 0 into database:
            $result[result]=0;
        }
        else
        {
            $result[result]="<b>$result[result]</b>";
        }
        echo "<td><div align=right>$result[result]</div></td>";
    }
    echo "</tr>";
}
echo "</table>";
mysql_close();
?>
 
  1.  
  2. <?php
  3. $host="localhost";
  4. $myuser="root";
  5. $mypass="caroline";
  6. $mydb="student_exam";
  7. mysql_connect($host,$myuser,$mypass);
  8. mysql_select_db($mydb);
  9. // this work only in win32, win64, or depending on php.ini setting:
  10. // (mysql functions converted into a variable)
  11. $sql=mysql_query;
  12. $ros=mysql_fetch_array;
  13. //------------------
  14.  
  15. //create headers:
  16. echo "<table border=1 width=100%>
  17. <tr valign=top bgcolor=#eeeeee>
  18. <td>Student</td>";
  19. $sq=$sql("SELECT * FROM exam ORDER by exam_id")or die(mysql_error());
  20. while($headers=$ros($sq))
  21. {
  22.     echo "<td><div align=center>$headers[exam_name]</td></div>";
  23. }
  24. echo "</tr>";
  25. $sq2=$sql("SELECT * FROM students ORDER BY student_id");
  26. // This uses while(s) in while(s), not prefered :=((
  27. while($students=$ros($sq2))
  28. {
  29.     echo "<tr valign=top><td bgcolor=#eeeeee>$students[student_name]</td>";
  30.     // displaying exam result of selected student
  31.     $meetresult=$sql("SELECT s.student_name AS name,e.exam_name AS exam,r.result FROM students s JOIN  exam e LEFT JOIN exam_results r ON r.student_id=s.student_id AND r.exam_id=e.exam_id WHERE s.student_id='$students[student_id]' ORDER BY e.exam_id");
  32.     while($result=$ros($meetresult))
  33.     {
  34.         if(!$result[result])
  35.         {
  36.         // no need insert 0 into database:
  37.             $result[result]=0;
  38.         }
  39.         else
  40.         {
  41.             $result[result]="<b>$result[result]</b>";
  42.         }
  43.         echo "<td><div align=right>$result[result]</div></td>";
  44.     }
  45.     echo "</tr>";
  46. }
  47. echo "</table>";
  48. mysql_close();
  49. ?>
  50.  

I hope this could help anybody else (if any).
I still try the multidimensional array :)
Result:
Code: [ Select ]
Student Biology  Math  Physics  Chemist  Sociology  
John 8 7 0 6 9
Mark 5 8 8 6 7
Donny 7 9 7 0 9
 
  1. Student Biology  Math  Physics  Chemist  Sociology  
  2. John 8 7 0 6 9
  3. Mark 5 8 8 6 7
  4. Donny 7 9 7 0 9
  5.  
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

Hello,
I have learned all the code.

But I have some problem to print the table if there are only two table like this :

Code: [ Select ]
CREATE TABLE `students` (
`student_id` int(5) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`student_id`)
) ENGINE=MyISAM;


INSERT INTO `students` VALUES ('1', 'John');
INSERT INTO `students` VALUES ('2', 'Mark');
INSERT INTO `students` VALUES ('3', 'Donny');

CREATE TABLE `exam` (
`exam_id` int(5) NOT NULL auto_increment,
`student_id` int(5) NOT NULL,
`exam_name` 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', 'Phys', '8');
INSERT INTO `exam` VALUES ('', '1', 'Math', '8');
INSERT INTO `exam` VALUES ('', '2', 'Bio', '6');
INSERT INTO `exam` VALUES ('', '2', 'Phys', '7');
INSERT INTO `exam` VALUES ('', '2', 'Math', '8');
INSERT INTO `exam` VALUES ('', '3', 'Bio', '8');
INSERT INTO `exam` VALUES ('', '3', 'Phys', '6');
INSERT INTO `exam` VALUES ('', '3', 'Math', '7');
  1. CREATE TABLE `students` (
  2. `student_id` int(5) NOT NULL auto_increment,
  3. `name` varchar(50) NOT NULL,
  4. PRIMARY KEY (`student_id`)
  5. ) ENGINE=MyISAM;
  6. INSERT INTO `students` VALUES ('1', 'John');
  7. INSERT INTO `students` VALUES ('2', 'Mark');
  8. INSERT INTO `students` VALUES ('3', 'Donny');
  9. CREATE TABLE `exam` (
  10. `exam_id` int(5) NOT NULL auto_increment,
  11. `student_id` int(5) NOT NULL,
  12. `exam_name` varchar(10) NOT NULL,
  13. `result` int(5) NOT NULL,
  14. PRIMARY KEY (`id2`)
  15. ) ENGINE=MyISAM;
  16. INSERT INTO `exam` VALUES ('', '1', 'Bio', '7');
  17. INSERT INTO `exam` VALUES ('', '1', 'Phys', '8');
  18. INSERT INTO `exam` VALUES ('', '1', 'Math', '8');
  19. INSERT INTO `exam` VALUES ('', '2', 'Bio', '6');
  20. INSERT INTO `exam` VALUES ('', '2', 'Phys', '7');
  21. INSERT INTO `exam` VALUES ('', '2', 'Math', '8');
  22. INSERT INTO `exam` VALUES ('', '3', 'Bio', '8');
  23. INSERT INTO `exam` VALUES ('', '3', 'Phys', '6');
  24. INSERT INTO `exam` VALUES ('', '3', 'Math', '7');


I want to print the table like phplover want.
please help me...
Sorry if my question is too basic...i'm a newbie.

Thx before
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

In addition,
Format of the table :
name as column and exam_name as row.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Code: [ Select ]
$con = mysql_connect("localhost","root","mypass");
mysql_select_db("mydb");
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY student.name, exam.exam_name");
$old_user = 0;
$new_user = 0;
while ($row = mysql_fetch_array($sql))
{
$new_user = $row['student_id'];
if ($old_user == $new_user)
{
echo $row['result'];
}
else
{
echo $row['name'] . $row['result'];
}
$old_user = $row['student_id'];
}
  1. $con = mysql_connect("localhost","root","mypass");
  2. mysql_select_db("mydb");
  3. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY student.name, exam.exam_name");
  4. $old_user = 0;
  5. $new_user = 0;
  6. while ($row = mysql_fetch_array($sql))
  7. {
  8. $new_user = $row['student_id'];
  9. if ($old_user == $new_user)
  10. {
  11. echo $row['result'];
  12. }
  13. else
  14. {
  15. echo $row['name'] . $row['result'];
  16. }
  17. $old_user = $row['student_id'];
  18. }
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

Thx for reply.

I have added table tag in your code, R_T. But the result is like this :

Donny 8 6 7
John 7 8 8
Mark 6 7 8

What I mean how if the result like this :

Exam Donny John Mark
Bio 8 7 6
Phys 6 8 7
Math 7 8 8

This is your code that I have added table tag :

Code: [ Select ]
<?
$con = mysql_connect("localhost","root","mypass");
mysql_select_db("mydb");
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY student.name, exam.exam_name");
$old_user = 0;
$new_user = 0;
echo "<table><tr><td>Exam</td>";
while ($row = mysql_fetch_array($sql))
{
$new_user = $row['student_id'];
if ($old_user == $new_user)
{
echo "<td>" .$row['result']. "</td>";
}
else
{
echo "</tr><tr><td>" .$row['name']. "</td><td>" .$row['result']. "</td>";
}
$old_user = $row['student_id'];
}
echo "</table>";
?>
  1. <?
  2. $con = mysql_connect("localhost","root","mypass");
  3. mysql_select_db("mydb");
  4. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY student.name, exam.exam_name");
  5. $old_user = 0;
  6. $new_user = 0;
  7. echo "<table><tr><td>Exam</td>";
  8. while ($row = mysql_fetch_array($sql))
  9. {
  10. $new_user = $row['student_id'];
  11. if ($old_user == $new_user)
  12. {
  13. echo "<td>" .$row['result']. "</td>";
  14. }
  15. else
  16. {
  17. echo "</tr><tr><td>" .$row['name']. "</td><td>" .$row['result']. "</td>";
  18. }
  19. $old_user = $row['student_id'];
  20. }
  21. echo "</table>";
  22. ?>


please help. Thx.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Code: [ Select ]
$con = mysql_connect("localhost","root","mypass");
mysql_select_db("mydb");
$sql = mysql_query("SELECT DISTICT name FROM users");
echo "<table><tr><td></td>";
while ($row = mysql_fetch_array($sql))
{
echo "<td>" . $row['name'] . "</td>";
}
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY student.name, exam.exam_name");
$old_user = 0;
$new_user = 0;
echo "</tr><tr>";
while ($row = mysql_fetch_array($sql))
{
$new_user = $row['student_id'];
if ($old_user == $new_user)
{
echo "<td>" . $row['result'] . "</td>";
}
else
{
echo "</tr><tr><td>" $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
}
$old_user = $row['student_id'];
}
echo "</tr></table>";
  1. $con = mysql_connect("localhost","root","mypass");
  2. mysql_select_db("mydb");
  3. $sql = mysql_query("SELECT DISTICT name FROM users");
  4. echo "<table><tr><td></td>";
  5. while ($row = mysql_fetch_array($sql))
  6. {
  7. echo "<td>" . $row['name'] . "</td>";
  8. }
  9. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY student.name, exam.exam_name");
  10. $old_user = 0;
  11. $new_user = 0;
  12. echo "</tr><tr>";
  13. while ($row = mysql_fetch_array($sql))
  14. {
  15. $new_user = $row['student_id'];
  16. if ($old_user == $new_user)
  17. {
  18. echo "<td>" . $row['result'] . "</td>";
  19. }
  20. else
  21. {
  22. echo "</tr><tr><td>" $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
  23. }
  24. $old_user = $row['student_id'];
  25. }
  26. echo "</tr></table>";


That should work, now please note that this is written off the top of my head and there could be an error somewhere ... maybe ...
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

This is the result :

Exam John Mark Donny
bio 8 6 7
bio 7 8 8
bio 6 7 8

Exam name are all bio. And location of 'result' not correct.
I've tried to modify the code, but still dont get the correct result.
Or maybe should use another method for two table?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Code: [ Select ]
$con = mysql_connect("localhost","root","mypass");
mysql_select_db("mydb");
$sql = mysql_query("SELECT DISTICT name FROM users ORDER BY student.name");
echo "<table><tr><td></td>";
while ($row = mysql_fetch_array($sql))
{
echo "<td>" . $row['name'] . "</td>";
}
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, student.name");
$old_user = 0;
$new_user = 0;
echo "</tr><tr>";
while ($row = mysql_fetch_array($sql))
{
$new_user = $row['student_id'];
if ($old_user == $new_user)
{
echo "<td>" .  $row['result'] . "</td>";
}
else
{
echo "</tr><tr><td>" $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
}
$old_user = $row['student_id'];
}
echo "</tr></table>";
  1. $con = mysql_connect("localhost","root","mypass");
  2. mysql_select_db("mydb");
  3. $sql = mysql_query("SELECT DISTICT name FROM users ORDER BY student.name");
  4. echo "<table><tr><td></td>";
  5. while ($row = mysql_fetch_array($sql))
  6. {
  7. echo "<td>" . $row['name'] . "</td>";
  8. }
  9. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, student.name");
  10. $old_user = 0;
  11. $new_user = 0;
  12. echo "</tr><tr>";
  13. while ($row = mysql_fetch_array($sql))
  14. {
  15. $new_user = $row['student_id'];
  16. if ($old_user == $new_user)
  17. {
  18. echo "<td>" .  $row['result'] . "</td>";
  19. }
  20. else
  21. {
  22. echo "</tr><tr><td>" $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
  23. }
  24. $old_user = $row['student_id'];
  25. }
  26. echo "</tr></table>";


Try now ...
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

Sorry to bother you, but still dont work.

Donny John Mark
bio 8
bio 7
bio 6
phys 6
phys 8
phys 7
math 7
math 8
math 8
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Code: [ Select ]
$con = mysql_connect("localhost","root","mypass");
mysql_select_db("mydb");
$sql = mysql_query("SELECT DISTICT name FROM users ORDER BY student.name");
echo "<table><tr><td></td>";
while ($row = mysql_fetch_array($sql))
{
echo "<td>" . $row['name'] . "</td>";
}
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, student.name");
$old_exam = 0;
$new_exam = 0;
echo "</tr><tr>";
while ($row = mysql_fetch_array($sql))
{
$new_exam = $row['exam_name'];
if ($old_exam == $new_exam)
{
echo "<td>" .  $row['result'] . "</td>";
}
else
{
echo "</tr><tr><td>" $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
}
$old_exam = $row['exam_name'];
}
echo "</tr></table>";
  1. $con = mysql_connect("localhost","root","mypass");
  2. mysql_select_db("mydb");
  3. $sql = mysql_query("SELECT DISTICT name FROM users ORDER BY student.name");
  4. echo "<table><tr><td></td>";
  5. while ($row = mysql_fetch_array($sql))
  6. {
  7. echo "<td>" . $row['name'] . "</td>";
  8. }
  9. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, student.name");
  10. $old_exam = 0;
  11. $new_exam = 0;
  12. echo "</tr><tr>";
  13. while ($row = mysql_fetch_array($sql))
  14. {
  15. $new_exam = $row['exam_name'];
  16. if ($old_exam == $new_exam)
  17. {
  18. echo "<td>" .  $row['result'] . "</td>";
  19. }
  20. else
  21. {
  22. echo "</tr><tr><td>" $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
  23. }
  24. $old_exam = $row['exam_name'];
  25. }
  26. echo "</tr></table>";


There
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

Not yet.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Okay but what's wrong ... ? I don't have this live and running somewhere so I can't test it ...
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

This is the result :

Exam John Mark Donny
8 7 6
Phys 6 8 7
Math 7 8 8

Is the problem in the table tag or in the mysql join and array?

How to print the data with this sequences :

Exam | John | Mark | Donny
Bio | 1 | 4 | 7
Phys | 2 | 5 | 8
Math | 3 | 6 | 9

1, 2, 3, ... = print sequences

Thx for help.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

I don't know what you're talking about print sequences, but I'll have to try it my self later on, I just don't have time now ...
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

Okay I fixed it and now it DOES work ... here is the result, and below is the code:
Code: [ Select ]
<?php
$con = mysql_connect("localhost","ohdesign_root","Hennie@OhDesignX.com");
mysql_select_db("ohdesign_M4MobileTest");
$sql = mysql_query("SELECT students.name FROM students ORDER BY students.name");
echo "<table><tr><td></td>";
while ($row = mysql_fetch_array($sql))
{
echo "<td>" . $row['name'] . "</td>";
}
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, students.name");
$old_exam = '';
$new_exam = '';
echo "</tr><tr>";
while ($row = mysql_fetch_array($sql))
{
$new_exam = $row['exam_name'];
if ($old_exam == $new_exam)
{
echo "<td>" .  $row['result'] . "</td>";
}
else
{
echo "</tr><tr><td>" . $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
}
$old_exam = $row['exam_name'];
}
echo "</tr></table>";
?>
  1. <?php
  2. $con = mysql_connect("localhost","ohdesign_root","Hennie@OhDesignX.com");
  3. mysql_select_db("ohdesign_M4MobileTest");
  4. $sql = mysql_query("SELECT students.name FROM students ORDER BY students.name");
  5. echo "<table><tr><td></td>";
  6. while ($row = mysql_fetch_array($sql))
  7. {
  8. echo "<td>" . $row['name'] . "</td>";
  9. }
  10. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, students.name");
  11. $old_exam = '';
  12. $new_exam = '';
  13. echo "</tr><tr>";
  14. while ($row = mysql_fetch_array($sql))
  15. {
  16. $new_exam = $row['exam_name'];
  17. if ($old_exam == $new_exam)
  18. {
  19. echo "<td>" .  $row['result'] . "</td>";
  20. }
  21. else
  22. {
  23. echo "</tr><tr><td>" . $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
  24. }
  25. $old_exam = $row['exam_name'];
  26. }
  27. echo "</tr></table>";
  28. ?>
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

Wow that's work.
Thank you very much. Ur verry helpful.

Now my homework is to put zero automatically in 'result' field if there's empty in database.
Thx again.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

exactly ... It's a pleasure ...
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

Ups...sorry to bother you again, R_T.
I found some problem in the table result. If I delete one data i.e John's Math result....Mark's Math result will shift left, so Mark's Math result will in position of John's math result.
How to solve this?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

If there is no result then add a 0 to that place ... or is that impossible ... ?
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

That is possible, but if I have 100 name of student and only 10 that have exam result, then I must fill 0 to other 90 student. I think that is not efficient.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

I hear ya ... Maybe something like this could work ...
Code: [ Select ]
<?php
$con = mysql_connect("localhost","ohdesign_root","Hennie@OhDesignX.com");
mysql_select_db("ohdesign_M4MobileTest");
$sql = mysql_query("SELECT students.name FROM students ORDER BY students.name");
echo "<table><tr><td></td>";
while ($row = mysql_fetch_array($sql))
{
echo "<td>" . $row['name'] . "</td>";
}
$sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, students.name");
$old_exam = '';
$new_exam = '';
echo "</tr><tr>";
while ($row = mysql_fetch_array($sql))
{
$new_exam = $row['exam_name'];
if ($old_exam == $new_exam)
{
if (!$row['result'])
{
echo "<td></td>";
}
else
{
echo "<td>" .  $row['result'] . "</td>";
}
}
else
{
echo "</tr><tr><td>" . $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
}
$old_exam = $row['exam_name'];
}
echo "</tr></table>";
?>
  1. <?php
  2. $con = mysql_connect("localhost","ohdesign_root","Hennie@OhDesignX.com");
  3. mysql_select_db("ohdesign_M4MobileTest");
  4. $sql = mysql_query("SELECT students.name FROM students ORDER BY students.name");
  5. echo "<table><tr><td></td>";
  6. while ($row = mysql_fetch_array($sql))
  7. {
  8. echo "<td>" . $row['name'] . "</td>";
  9. }
  10. $sql = mysql_query("SELECT * FROM exam LEFT JOIN students ON exam.student_id = students.student_id ORDER BY exam.exam_name, students.name");
  11. $old_exam = '';
  12. $new_exam = '';
  13. echo "</tr><tr>";
  14. while ($row = mysql_fetch_array($sql))
  15. {
  16. $new_exam = $row['exam_name'];
  17. if ($old_exam == $new_exam)
  18. {
  19. if (!$row['result'])
  20. {
  21. echo "<td></td>";
  22. }
  23. else
  24. {
  25. echo "<td>" .  $row['result'] . "</td>";
  26. }
  27. }
  28. else
  29. {
  30. echo "</tr><tr><td>" . $row['exam_name'] . "</td><td>" . $row['result'] . "</td>";
  31. }
  32. $old_exam = $row['exam_name'];
  33. }
  34. echo "</tr></table>";
  35. ?>
  • eprompt
  • Newbie
  • Newbie
  • eprompt
  • Posts: 11

Post 3+ Months Ago

I have tried, and the result still the same as before.

I have modified that code...but still not work.
What I want ask you is why this part of code :

Code: [ Select ]
...
if (!$row['result'])
{
echo "<td></td>";
}
...
  1. ...
  2. if (!$row['result'])
  3. {
  4. echo "<td></td>";
  5. }
  6. ...


Not executed by program?
Or executed?

( sorry, I am a very newbie, this is first time I learn about programming language )
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6231
  • Loc: South-Africa

Post 3+ Months Ago

eprompt wrote:
( sorry, I am a very newbie, this is first time I learn about programming language )

I realized ... sorry but I feel it may be time for you to learn a bit of programming of your own instead of me just writing your whole website for you ... You should start here in your quest to learn PHP ... That would be my advice anyway ...

The code you asked about is supposed to execute only when a row has a null value inside it ... or no value ...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

For future reference, if you have an empty field that you still want to be printed... just check if it's false.

Normally, empty fields would be set to false. I tried numerous things, is_nul(); $field == "" and $field == false... and that works...

Don't do $field === false, because an empty field would be a string, and not a boolean.. it would be a string holding the value false.

If I knew this when phplover needed this, I would have done it... but I didn't test for this so, as I trusted Righteous_trespasser :lol:
  • syca22
  • Born
  • Born
  • syca22
  • Posts: 1

Post 3+ Months Ago

Phplover Hi, I found the same problem as yours, to find solutions? If so could you tell me how you resolved? .. thank you very much in advance ..

Greetings
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

syca22, please read the post right above yours. That would answer your question.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3243
  • Loc: South Africa

Post 3+ Months Ago

Umm I was reading through this an could not help but notice the immense flaws in the data model proposed here.

Below is what I class a closer model to the one needed for the system

Image

I also noted alot of embedded HTML. Might I propose a static string with place holders?

Code: [ Select ]
 
$cellTemplate = "<td>#VALUE#</td>";
$rowTemplate = "<tr>#CELL_DATA#</tr>";
 
// some loop here to construct the row
 
  1.  
  2. $cellTemplate = "<td>#VALUE#</td>";
  3. $rowTemplate = "<tr>#CELL_DATA#</tr>";
  4.  
  5. // some loop here to construct the row
  6.  


another option would be to have a function that could format it for you

Code: [ Select ]
 
function RenderRow(Student $student){
 //replace function here for cell to contain the name
 
 // Loop here to populate subsequent data
 
 //return formatted string
}
 
  1.  
  2. function RenderRow(Student $student){
  3.  //replace function here for cell to contain the name
  4.  
  5.  // Loop here to populate subsequent data
  6.  
  7.  //return formatted string
  8. }
  9.  


Anyways that is my two cents (as usual I am harping on about data structures) but I hope it helps someone.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

lol thanks Rabid Dog. I didn't know any better then... won't happen again. Sorry :lol:
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3243
  • Loc: South Africa

Post 3+ Months Ago

LOL nothing is wrong with what I see, just again trying to point something out to someone who wants to take the time and model it correctly. Besides I bet you learned something :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8488
  • Loc: USA

Post 3+ Months Ago

Rabid Dog wrote:
Besides I bet you learned something :D
Yup... kind of glad you returned to ozzu :D

Post Information

  • Total Posts in this topic: 61 posts
  • Users browsing this forum: No registered users and 38 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2016. Ozzu® is a registered trademark of Unmelted, LLC.