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 August 15th, 2008, 4:05 am

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. ?>
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 August 15th, 2008, 4:05 am

  • eprompt
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 13, 2008
  • Posts: 11
  • Status: Offline

Post August 15th, 2008, 6:02 am

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

Post August 15th, 2008, 6:07 am

exactly ... It's a pleasure ...
Let's leave all our *plum* where it is and go live in the jungle ...
  • eprompt
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 13, 2008
  • Posts: 11
  • Status: Offline

Post August 15th, 2008, 6:31 am

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

Post August 15th, 2008, 6:38 am

If there is no result then add a 0 to that place ... or is that impossible ... ?
Let's leave all our *plum* where it is and go live in the jungle ...
  • eprompt
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 13, 2008
  • Posts: 11
  • Status: Offline

Post August 15th, 2008, 6:55 am

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

Post August 15th, 2008, 7:02 am

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. ?>
Let's leave all our *plum* where it is and go live in the jungle ...
  • eprompt
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 13, 2008
  • Posts: 11
  • Status: Offline

Post August 15th, 2008, 6:41 pm

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

Post August 17th, 2008, 9:58 am

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 ...
Let's leave all our *plum* where it is and go live in the jungle ...
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post January 20th, 2009, 12:00 am

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:
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • syca22
  • Born
  • Born
  • No Avatar
  • Joined: Jul 03, 2009
  • Posts: 1
  • Status: Offline

Post July 3rd, 2009, 9:01 am

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
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post July 3rd, 2009, 9:58 am

syca22, please read the post right above yours. That would answer your question.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 3rd, 2009, 3:05 pm

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.
Watch me grow
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post July 3rd, 2009, 3:10 pm

lol thanks Rabid Dog. I didn't know any better then... won't happen again. Sorry :lol:
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 3rd, 2009, 3:17 pm

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
Watch me grow
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 3rd, 2009, 3:17 pm

Post Information

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