Diplay multiple Mysql Arrays in 1 HTMl table – PHP

  php

Q(Question):

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


--
-- Table structure for table `01234`
--
CREATE TABLE IF NOT EXISTS `01234` (
`ucode` char(50) NOT NULL,
`Day` char(50) NOT NULL,
`Period` char(50) NOT NULL,
`sub` char(50) NOT NULL,
`teacher` char(50) NOT NULL,
`room` char(50) NOT NULL,
`class` char(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `01234`
--
INSERT INTO `01234` (`ucode`, `Day`, `Period`, `sub`, `teacher`, `room`, `class`) VALUES
('01234', '1', '1', 'PE', 'PETE', 'R16', '2.8'),
('01234', '1', '2', 'WHG', 'WHGT', 'R27', '2.8'),
('01234', '1', '3', 'COMM', 'COMMT', 'R12', '2.1'),
('01234', '1', '4', 'COMM', 'COMMT', 'R12', '2.1'),
('01234', '1', '5', 'SCIE', 'SCIET', 'SL1', '2.8'),
('01234', '1', '6', 'ENGL', 'ENGT', 'R29', '2.7'),
('01234', '2', '1', 'MATH', 'MATHT', 'R18', '2.5'),
('01234', '2', '2', 'ENGL', 'ENGT', 'R29', '2.7'),
('01234', '2', '3', 'RELI', 'RET', 'R15', '2.8'),
('01234', '2', '4', 'INST', 'PCT', 'CL1', '2.5'),
('01234', '2', '5', 'SPORTB', 'SPT', '', '2.2'),
('01234', '2', '6', 'SPORTB', 'SPT', '', '2.2'),
('01234', '3', '1', 'WHG', 'WHGT', 'R30', '2.8'),
('01234', '3', '2', 'PE', 'PETE', 'R23', '2.8'),
('01234', '3', '3', 'RELI', 'RET', 'R15', '2.8'),
('01234', '3', '4', 'SCIE', 'SCIET', 'SL1', '2.8'),
('01234', '3', '5', 'SCIE', 'SCIET', 'SL1', '2.8'),
('01234', '3', '6', 'COMM', 'COMMT', 'R12', '2.1'),

i know i have to echo a html table in an array but i dont know how to make it look like this:


Period Day1 Day2 Day3
1 PE MATH WHG
2 WHG ENG PE
3 COMM RELI RELI
4 COMM INST SCIE

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?

A(Answer):

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:


while($row = mysql_fetch_assoc($result)) {
$days[$row['day']][$row['period']] = $row['sub'];
}

This would create an array of days, where each day contained every period for that day.
Which would look something like:


Array (
1 = Array(1 => "Subj", 2 => "Subj", 3 => "Subj"),
2 = Array(1 => "Subj", 2 => "Subj", 3 => "Subj")
/* etc... */
)

And using that as a base, it shouldn’t be hard to print in whatever format you need.

A(Answer):

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


while($row = mysql_fetch_assoc($result)) {
$days[$row['day']][$row['period']] = $row['sub'];
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>";
}

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

A(Answer):

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.


$day1 = mysql_query("SELECT sub FROM `01234` WHERE `Day` = 1");
$day2 = mysql_query("SELECT sub FROM `01234` WHERE `Day` = 2");
while($d1 = mysql_fetch_assoc($day1) || ($d2 = mysql_fetch_assoc($day2)))
{
$day1[$d1['day']][$d1['period']] = $d1['sub'];
$day2[$d2['day']][$d2['period']] = $d2['sub'];
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>";
}

this code ignoers day1 for sum reason

again thank you

A(Answer):

@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:


$array = array(
1 => "John Doe",
2 => "Jane Smith",
3 => "James Bond"
);

To print the values of that array, we would do:


foreach($array as $_index => $_value) {
echo "$_index = $_value\n";
}

A multidimensional array is simply an array that contains other arrays.
Like:


$array = array(
1 => array("First" => "John", "Last" => "Doe"),
2 => array("First" => "Jane", "Last" => "Smith"),
3 => array("First" => "James", "Last" => "Bond"),
);

So, when we loop through it, like we did with our one-dimensional array, the $_value becomes an array:


foreach($array as $_index => $_value) {
echo "$_index = {$_value['Last']}, {$_value['First']}";
}

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:


echo "Period\t1\t2\t3\t4\t5\t6";
foreach($days as $_dayNo => $_day) {
echo "\nDay $_dayNo:\t";
foreach($_day as $_period) {
echo "$_period\t";
}
}

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:


if($a = 1 || $b = 1) {
// $a == 1
// $b == null
}

To do what you are trying, you could try a do while loop:


do {
$row1 = @mysql_fetch_assoc($result1);
$row2 = @mysql_fetch_assoc($result2);
if($row1) {
// Do something with $row1
}
if($row2) {
// Do something with $row2
}
} while ($row1 || $row2);

A(Answer):

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 🙂

A(Answer):

I’m glad I could help.
See you around 🙂

LEAVE A COMMENT