By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,709 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,709 IT Pros & Developers. It's quick & easy.

Diplay multiple Mysql Arrays in 1 HTMl table

P: 22
Hey every1 , i have started coding in php and am a qucik learner (computer generation) i have ran into a problem and am cluless :(

ill start by showing you the mysql table
Expand|Select|Wrap|Line Numbers
  1. --
  2. -- Table structure for table `01234`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `01234` (
  6.   `ucode` char(50) NOT NULL,
  7.   `Day` char(50) NOT NULL,
  8.   `Period` char(50) NOT NULL,
  9.   `sub` char(50) NOT NULL,
  10.   `teacher` char(50) NOT NULL,
  11.   `room` char(50) NOT NULL,
  12.   `class` char(50) NOT NULL
  13. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  14.  
  15. --
  16. -- Dumping data for table `01234`
  17. --
  18.  
  19. INSERT INTO `01234` (`ucode`, `Day`, `Period`, `sub`, `teacher`, `room`, `class`) VALUES
  20. ('01234', '1', '1', 'PE', 'PETE', 'R16', '2.8'),
  21. ('01234', '1', '2', 'WHG', 'WHGT', 'R27', '2.8'),
  22. ('01234', '1', '3', 'COMM', 'COMMT', 'R12', '2.1'),
  23. ('01234', '1', '4', 'COMM', 'COMMT', 'R12', '2.1'),
  24. ('01234', '1', '5', 'SCIE', 'SCIET', 'SL1', '2.8'),
  25. ('01234', '1', '6', 'ENGL', 'ENGT', 'R29', '2.7'),
  26. ('01234', '2', '1', 'MATH', 'MATHT', 'R18', '2.5'),
  27. ('01234', '2', '2', 'ENGL', 'ENGT', 'R29', '2.7'),
  28. ('01234', '2', '3', 'RELI', 'RET', 'R15', '2.8'),
  29. ('01234', '2', '4', 'INST', 'PCT', 'CL1', '2.5'),
  30. ('01234', '2', '5', 'SPORTB', 'SPT', '', '2.2'),
  31. ('01234', '2', '6', 'SPORTB', 'SPT', '', '2.2'),
  32. ('01234', '3', '1', 'WHG', 'WHGT', 'R30', '2.8'),
  33. ('01234', '3', '2', 'PE', 'PETE', 'R23', '2.8'),
  34. ('01234', '3', '3', 'RELI', 'RET', 'R15', '2.8'),
  35. ('01234', '3', '4', 'SCIE', 'SCIET', 'SL1', '2.8'),
  36. ('01234', '3', '5', 'SCIE', 'SCIET', 'SL1', '2.8'),
  37. ('01234', '3', '6', 'COMM', 'COMMT', 'R12', '2.1'),
  38.  
i know i have to echo a html table in an array but i dont know how to make it look like this:
Expand|Select|Wrap|Line Numbers
  1. Period  Day1    Day2   Day3   
  2.   1     PE      MATH   WHG
  3.   2     WHG     ENG    PE
  4.   3     COMM    RELI   RELI
  5.   4     COMM    INST   SCIE
  6.  
i had a old code but it only handled basic rows that where already lyk the stated above.

any help is appreciated if u need more info just ask.

TY and have a very happy new year.
P.S sorry if i put the SQL code in wrong BBcode , didnt know what to put it as?
Dec 31 '08 #1
Share this Question
Share on Google+
6 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Rather than echo the results of the query directly, you need to set up an array so you can print the data in a more controlled way.

For example:
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_assoc($result)) {
  2.   $days[$row['day']][$row['period']] = $row['sub'];
  3. }
This would create an array of days, where each day contained every period for that day.
Which would look something like:
Expand|Select|Wrap|Line Numbers
  1. Array (
  2.   1 = Array(1 => "Subj", 2 => "Subj", 3 => "Subj"),
  3.   2 = Array(1 => "Subj", 2 => "Subj", 3 => "Subj")
  4.   /* etc... */
  5. )
And using that as a base, it shouldn't be hard to print in whatever format you need.
Dec 31 '08 #2

P: 22
Thankyou Atli and Happy new year

i used ur soloution and it can grab the data from the sql database.
i echod the varible "$days" and it resulted in "ArrayArrayArray..." so :S

however i did this code
Expand|Select|Wrap|Line Numbers
  1.  while($row = mysql_fetch_assoc($result)) {
  2.  $days[$row['day']][$row['period']] = $row['sub'];
  3.  
  4.   echo"<TR bgcolor=#86D5FF class=ts onMouseOver=this.bgColor='gold'; onMouseOut=this.bgColor='#86D5FF';><td><strong>" . $row['period'] . "</strong></td><td><div align=center>" . $row['sub'] . "</div></td><td><div align=center>" . $row['sub'] . "</div></td>";
  5.  
  6. }
  7.  
the subjects keep listing after one another, how can i make it that after 6 subjects , list in the column next to it.

again thankyou to any that took the tiume to read this and even more ty to those whu replied :)

:S is the symbol of confusion for those who dont know
Jan 1 '09 #3

P: 22
had a thought.

is there a way that you can set an array to grab results from 2 queries. and then load them as seperat varibles.

Expand|Select|Wrap|Line Numbers
  1. $day1 = mysql_query("SELECT sub FROM `01234` WHERE `Day` = 1");
  2. $day2 = mysql_query("SELECT sub FROM `01234` WHERE `Day` = 2");
  3.  
  4. while($d1 = mysql_fetch_assoc($day1) || ($d2 = mysql_fetch_assoc($day2)))
  5. {
  6.  $day1[$d1['day']][$d1['period']] = $d1['sub'];
  7.  $day2[$d2['day']][$d2['period']] = $d2['sub'];
  8.  
  9.   echo"<TR bgcolor=#86D5FF class=ts onMouseOver=this.bgColor='gold'; onMouseOut=this.bgColor='#86D5FF';><td><strong>" . $d1['period'] . "</strong></td><td><div align=center>" . $d1['sub'] . "</div></td><td><div align=center>" . $d2['sub'] . "</div></td>";
  10.  
  11. }
  12.  
this code ignoers day1 for sum reason

again thank you
Jan 1 '09 #4

Atli
Expert 5K+
P: 5,058
@GhOsTTeCh
I think it's the multi-dimensional arrays that are confusing you.
Let me try to explain.

Consider a typical one-dimensional array, like this:
Expand|Select|Wrap|Line Numbers
  1. $array = array(
  2.  1 => "John Doe",
  3.  2 => "Jane Smith",
  4.  3 => "James Bond"
  5. );
To print the values of that array, we would do:
Expand|Select|Wrap|Line Numbers
  1. foreach($array as $_index => $_value) {
  2.   echo "$_index = $_value\n";
  3. }
A multidimensional array is simply an array that contains other arrays.
Like:
Expand|Select|Wrap|Line Numbers
  1. $array = array(
  2.   1 => array("First" => "John", "Last" => "Doe"),
  3.   2 => array("First" => "Jane", "Last" => "Smith"),
  4.   3 => array("First" => "James", "Last" => "Bond"),
  5. );
So, when we loop through it, like we did with our one-dimensional array, the $_value becomes an array:
Expand|Select|Wrap|Line Numbers
  1. foreach($array as $_index => $_value) {
  2.   echo "$_index = {$_value['Last']}, {$_value['First']}";
  3. }
So, in the code I posted before, I was essentially creating an array of days, where each day had a value that was an array of periods, where each period had a subject as it's value.

To print that you would have to loop through the days, and loop through each period of each day so you could print the subject.

Like:
Expand|Select|Wrap|Line Numbers
  1. echo "Period\t1\t2\t3\t4\t5\t6";
  2. foreach($days as $_dayNo => $_day) {
  3.   echo "\nDay $_dayNo:\t";
  4.   foreach($_day as $_period) {
  5.    echo "$_period\t";
  6.  }
  7. }
Does that make sense?

@GhOsTTeCh
Yes. The problem in the code you posted wast that you used or (||).
When the left-hand condition of an or is true, the right-hand condition won't be executed.
So:
Expand|Select|Wrap|Line Numbers
  1. if($a = 1 || $b = 1) {
  2.   // $a == 1
  3.   // $b == null
  4. }
To do what you are trying, you could try a do while loop:
Expand|Select|Wrap|Line Numbers
  1. do {
  2.   $row1 = @mysql_fetch_assoc($result1);
  3.   $row2 = @mysql_fetch_assoc($result2);
  4.   if($row1) {
  5.     // Do something with $row1
  6.   }
  7.   if($row2) {
  8.     // Do something with $row2
  9.   }
  10. } while ($row1 || $row2);
Jan 1 '09 #5

P: 22
Thank you Atli , although i didnt use ur exact soloution u certanley explaiend alot on sent me in the right direction

expect to see me more on the forums :)
Jan 3 '09 #6

Atli
Expert 5K+
P: 5,058
I'm glad I could help.
See you around :)
Jan 3 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.