PHP-MySQL: Need Help to Create Multidimensional HTML Table

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 12:14 am

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 ...
Let's leave all our *plum* where it is and go live in the jungle ...
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 9th, 2008, 12:14 am

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 12:23 am

Updated ... now look at the difference when I add the zeros ... there
Let's leave all our *plum* where it is and go live in the jungle ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 12:27 am

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
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 12:31 am

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
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 12:39 am

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?
Let's leave all our *plum* where it is and go live in the jungle ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 12:40 am

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
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 12:52 am

That's exactly the code ... That's the one that works ...
Let's leave all our *plum* where it is and go live in the jungle ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 1:06 am

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
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 1:17 am

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');
Let's leave all our *plum* where it is and go live in the jungle ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 1:35 am

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
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 1:42 am

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 ...
Let's leave all our *plum* where it is and go live in the jungle ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 2:31 am

yeah, whatever you said, thanks for your helps :))
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 3:28 am

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
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post July 9th, 2008, 5:20 am

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 ...
Let's leave all our *plum* where it is and go live in the jungle ...
  • phplover
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 03, 2008
  • Posts: 15
  • Loc: INDONESIA
  • Status: Offline

Post July 9th, 2008, 9:28 am

i hope. peace, bro!
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 9th, 2008, 9:28 am

Post Information

  • Total Posts in this topic: 61 posts
  • Users browsing this forum: No registered users and 279 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.