| | |
looping through a multi dimentional arrry
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
Hi
I have 7 arrays for all containing the same data:time_id,
start_time, end_time and colour.
Each array represent a day of the week.
There are a total of seven columns to display.
I have combined them into a three multi dimentional to
display data form the 7 layers which are the days.
Is this the best way to accomplish my goal?
The results are not what displaying properly.
The columns are all not starting at the top of the screen and they should.
note: This is the result, each line represents the top of a column.
The columns should be at the same level.
|------|
|------|-------|
|------|-------|-------|
|------|-------|-------|-------|
|------|-------|-------|-------|-------|
|------|-------|-------|-------|-------|-------|
|------|-------|-------|-------|-------|-------|--------|
|------|-------|-------|-------|-------|-------|--------|
I have 7 arrays for all containing the same data:time_id,
start_time, end_time and colour.
Each array represent a day of the week.
There are a total of seven columns to display.
I have combined them into a three multi dimentional to
display data form the 7 layers which are the days.
Is this the best way to accomplish my goal?
The results are not what displaying properly.
The columns are all not starting at the top of the screen and they should.
note: This is the result, each line represents the top of a column.
The columns should be at the same level.
|------|
|------|-------|
|------|-------|-------|
|------|-------|-------|-------|
|------|-------|-------|-------|-------|
|------|-------|-------|-------|-------|-------|
|------|-------|-------|-------|-------|-------|--------|
|------|-------|-------|-------|-------|-------|--------|
PHP Syntax (Toggle Plain Text)
<? echo"<table>"; //get availability nfo $query = "SELECT DISTINCT(a.time_id), a.start_time, a.end_time, c.colour FROM available a, type_display c WHERE a.type_code = c.type_code AND '$event_date' BETWEEN a.start_date AND a.end_date AND a.org_pro_id = '$org_pro_id' AND a.week_day = '$day' GROUP BY a.start_time"; $result = mysqli_query($mysqli, $query) or die('Error, query failed'); while($row = mysqli_fetch_array($result)) { $avail_day1[] = $row; //this only shows one of the seven days to save on clutter } /**Note:all contain table data time_id, start_time, end_time, colour**/ $all_avail_days = array($avail_day1, $avail_day2, $avail_day3, $avail_day4, $avail_day5, $avail_day6, $avail_day7); for($wk_day =0; $wk_day < count($all_avail_days); $wk_day++)//wk_day layer { $day = $wk_day + 1; echo":"; foreach($all_avail_days as $key => $avail)//rows { for($j = 0; $j < count($all_avail_days[$key]); $j++)//column { if($day == 1) { $time_id1 = $all_avail_days[$wk_day][$key][time_id]; $type_start_time1 = $all_avail_days[$wk_day][$key][start_time]; $type_end_time1 = $all_avail_days[$wk_day][$key][end_time]; $colour1 = "#".$all_avail_days[$wk_day][$key][colour]."</br>"; } //Note: days 2 to 6 removed to limit clutter.......... if($day == 7) { $time_id7 = $all_avail_days[$wk_day][$key][time_id]; $type_start_time7 = $all_avail_days[$wk_day][$key][start_time]; $type_end_time7 = $all_avail_days[$wk_day][$key][end_time]; $colour7 = "#".$all_avail_days[$wk_day][$key][colour]."</br>"; } echo"<tr> <td width=\"8%\" height=\"12\"> </td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour1\"> $type_start_time1</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour2\"> $type_start_time2</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour3\"> $type_start_time3</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour4\"> $type_start_time4</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour5\"> $type_start_time5</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour6\"> $type_start_time6</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour7\"> $type_start_time7</td>"; echo"</tr>\n"; } } } echo"</table>"; ?>
Hi, first let me suggest that you should think of some less complicated way of displaying the data. I guess you could achieve the same result with just about twenty lines of code.
But to answer you questions:
Let me know if you have any questions.
But to answer you questions:
- The main problem probably is that you have
echo '<tr>...inside of all your three loops. So it prints a row of all seven days for each day and term - which isn't what you want. There's no simple solution for this. I suggest you rewrite it into a single query for all days using SQL construct week_day IN (0,1,2,3,4,5,6) and then convert the results into two-dimmensional array:$data[$orderNumber][$row->weekDay]. Then traverse $data in two loops: one for each row of the array and second for each day of the week. -
for($wk_day =0; $wk_day < count($all_avail_days);and
foreach($all_avail_days as $key => $avail)does the same only using a different construct. So you are basically looping the same thing twice. - maybe you're referencing the array items wrongly - use
$all_avail_days["$wk_day"][$key]["start_time"]instead of
$all_avail_days[$wk_day][$key][start_time]Reasoning: $all_avail_days["1"] is something else than $all_avail_days[1]. - $type_start_time7 -like variables aren't cleared after being used so you may be printing old values from previous loops. You should have
elsefor eachif($day == ...)where you clear the variables:
$time_id7 = $type_start_time7 = $type_end_time7 = $colour7 = NULL;Or you can get rid of them completely and use the directly$all_avail_days["$wk_day"][$key]["start_time"]stuff withprintf() - You may also want to use ORDER BY in your query to have the records sorted by time
- I don't think there's any such thing as </br>. <br> is ok though.
Let me know if you have any questions.
Petr 'PePa' Pavel
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
Hi
Thanks for the reply.
The days in column format for the week is displaying correctly.
The current problem is when the foreach loop ends in position # 1
only the last iteration of the loop is displayed.
When the foreach loop ends in position # 2 to try and display
all the iteration of the array. The event time displays but the
event colour ($colour1 ... $colour7) does not.
It appears
Thanks for the reply.
The days in column format for the week is displaying correctly.
The current problem is when the foreach loop ends in position # 1
only the last iteration of the loop is displayed.
When the foreach loop ends in position # 2 to try and display
all the iteration of the array. The event time displays but the
event colour ($colour1 ... $colour7) does not.
It appears
PHP Syntax (Toggle Plain Text)
<? //work hours $start_time = "09:00:00"; $end_time = "05:00:00"; //get availability nfo /** selected data time_id, week_day, start_date, end_date, start_time, end_time, colour in array**/ $avail_day; //availaibility /**selected data event_id, event_name, event_date, event_time in array**/ $events;//appointment echo"<table>"; //Loop over the hours for($time = $start_time; $time <= $end_time; $time += $add_time) { //format 24 hour time interval for passing via url $interval_24hr = date("H:i:s", $time); echo "<tr>"; //Output the time interval label echo"<td width=\"8%\" height=\"15\" bgcolor=\"\" align=\"center\"> <ul> <li>".date("h:i A", $time)."</li> </ul> </td>"; //loop to display days of the week foreach($avail_day as $key => $avail)//rows { //diaplay event type colour and labeling to event end time $seg_code = $avail_day['event_type_code']; $time_id = $avail_day['time_id']; $seg_day = $avail_day['week_day']; //monday if($seg_day == 1 && $interval_24hr >= $avail_day['start_time']&& $interval_24hr <= $avail_day['end_time']) { $colour1 = "#".$avail_day['colour']; } elseif($seg_day == 1) { $colour1 = "#ebeae0"; } //note tuesday to sunday not listed for space reasons foreach($events as $key => $event) { //determine day of week $week_day = date('l',strtotime($event['event_date'])); //appointment or name description if($week_day == "Monday" && $event_time >= $time && $event_time < ($time + $add_time)) { $day = 550; $event_name1 = $event['last_name']; } elseif { /**tuesday to sunday not listed for space reasons**/ } } }//end foreach postion#1 echo"<td width=\"13%\" height=\"12\" bgcolor=\"$colour1\"> $event_name1</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour2\"> $event_name2</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour3\"> $event_name3</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour4\"> $event_name4</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour5\"> $event_name5</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour6\"> $event_name6</td> <td width=\"13%\" height=\"12\" bgcolor=\"$colour7\"> $event_name7</td>"; echo"</tr>\n"; // }//end foreach postion#2 }//for echo"</table>"; ?>
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
Hi thanks for responding
I made the rows easier to read. But that is not my problem it’s the looping.
I have worked the code and have all the event types showing for each day column.
Which is what I am trying to accomplish.
The problem is the if the person is working a short day i.e. 9:00AM to 12:00PM the last
type colour for the short day fills the time slots to 5:00PM instead of stopping at 12:00pm.
I have tried else statement to empty the $seg_colour array with no luck.
This if statement is to match the event type time to the listed time of day.
Should this not prevent unrelated event types from showing?
NOTE:I have temporarily removed the appointments loop to focus on the event type looping.
I made the rows easier to read. But that is not my problem it’s the looping.
I have worked the code and have all the event types showing for each day column.
Which is what I am trying to accomplish.
The problem is the if the person is working a short day i.e. 9:00AM to 12:00PM the last
type colour for the short day fills the time slots to 5:00PM instead of stopping at 12:00pm.
I have tried else statement to empty the $seg_colour array with no luck.
This if statement is to match the event type time to the listed time of day.
Should this not prevent unrelated event types from showing?
PHP Syntax (Toggle Plain Text)
if($interval_24hr >= $avail_day['type_start_time'] && $interval_24hr <= $avail_day['type_end_time'])
NOTE:I have temporarily removed the appointments loop to focus on the event type looping.
PHP Syntax (Toggle Plain Text)
<? //work hours $start_time = "09:00:00"; $end_time = "05:00:00"; //get availability nfo /** selected data time_id, week_day, start_date, end_date, start_time, end_time, colour in array**/ $avail_days; //availaibility /**selected data event_id, event_name, event_date, event_time in array**/ $events;//appointment echo"<table>"; //Loop over the hours for ($time = $start_time; $time <= $end_time; $time += $add_time) { //format 24 hour time interval for passing via url $interval_24hr = date("H:i:s", $time); /**-----------------event time listing and event type--------------**/ echo "<tr>"; //Output the time interval label echo"<td width=\"8%\" height=\"15\" bgcolor=\"\" align=\"center\"> <ul> <li>".date("h:i A", $time)."</li> </ul> </td>"; /**----------------------event type display------------------------------**/ //loop through array to diaplay event type colour and labeling foreach ($avail_days as $avail_day) { //diaplay event type colour and labeling to event end time if($interval_24hr >= $avail_day['type_start_time'] && $interval_24hr <= $avail_day['type_end_time']) { $seg_colour = "#".$avail_day['colour']; $time_id = $avail_day['time_id']; $seg_day = $avail_day['week_day']; //monday if($seg_day == 1) { $colour1 = $seg_colour; } //tuesday elseif($seg_day == 2) { /**note elseif for tuesday to sunday not isted for space reasons**/ } /**----------------------event--------------------**/ /*loop to provide appointment linked to appropriate event time slot goes here*/ /**---------------dynamic looping rows and columns----------**/ echo'<td width="13%" height="12" bgcolor="$colour1"> $event_name1</td> <td width="13%" height="12" bgcolor="$colour2"> $event_name2</td> <td width="13%" height="12" bgcolor="$colour3"> $event_name3</td> <td width="13%" height="12" bgcolor="$colour4"> $event_name4</td> <td width="13%" height="12" bgcolor="$colour5"> $event_name5</td> <td width="13%" height="12" bgcolor="$colour6"> $event_name6</td> <td width="13%" height="12" bgcolor="$colour7"> $event_name7</td>"; echo"</tr>\n"; } echo"</table>"; ?>
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
Hi thanks for responding
After various approaches that worked partially this is the solution.
I am open to suggestions on improving on it.
NOTE:I have temporarily removed the appointments loop to focus on the event type looping.
After various approaches that worked partially this is the solution.
I am open to suggestions on improving on it.
NOTE:I have temporarily removed the appointments loop to focus on the event type looping.
PHP Syntax (Toggle Plain Text)
<? //work hours $min_start1 = "09:30:00";//earliest appointment $start_time = "09:00:00";//office hours $end_time = "05:00:00"; //get availability nfo /** selected data time_id, week_day, start_date, end_date, start_time, end_time, colour in array**/ $avail_days; //availaibility /**selected data event_id, event_name, event_date, event_time in array**/ $events;//appointment echo"<table>"; //Loop over the hours for ($time = $start_time; $time <= $end_time; $time += $add_time) { //format 24 hour time interval for passing via url $interval_24hr = date("H:i:s", $time); /**-----------------------event time listing and event type------------------**/ echo "<tr>"; //Output the time interval label echo"<td width=\"8%\" height=\"15\" bgcolor=\"\" align=\"center\"> <ul> <li>".date("h:i A", $time)."</li> </ul> </td>"; /**----------------------event type display------------------------------**/ //loop through array to diaplay event type colour and labeling foreach ($avail_days as $avail_day) { //day of the week Mon to sun $seg_day = $avail_day['week_day']; //diaplay event type colour and labeling to event end time //monday if($seg_day == 1 && $interval_24hr >= $avail_day['start_time'] && $interval_24hr <= $end_time) { $colour1 = "#".$avail_day['colour']; } elseif($seg_day == 1 && $interval_24hr > $end_time || $seg_day == 1 && $interval_24hr < $min_start1) { $colour1 = "#ebeae0";//default background colour } //tuesday if($seg_day == 2) { /*note: for tuesday to sunday not listed for space reasons it is the same as for monday except $colour2 etc is used*/ ..... } /**----------------------event--------------------**/ /*loop to provide appointment linked to appropriate event time slot goes here*/ /**------------------dynamic looping rows and columns--------------**/ echo'<td width="13%" height="12" bgcolor="$colour1"> $event_name1</td> <td width="13%" height="12" bgcolor="$colour2"> $event_name2</td> <td width="13%" height="12" bgcolor="$colour3"> $event_name3</td> <td width="13%" height="12" bgcolor="$colour4"> $event_name4</td> <td width="13%" height="12" bgcolor="$colour5"> $event_name5</td> <td width="13%" height="12" bgcolor="$colour6"> $event_name6</td> <td width="13%" height="12" bgcolor="$colour7"> $event_name7</td>"; echo"</tr>\n'; } echo"</table>"; ?>
![]() |
Other Threads in the PHP Forum
- Previous Thread: Extracting select option fields from mySQL
- Next Thread: You don't have permission to access /.shtml on this server.
| Thread Tools | Search this Thread |
# .htaccess 5.2.10 ajax apache api array beginner binary broken cakephp checkbox class clean cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link local login loop mail menu mlm mod_rewrite multiple mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote script search server sessions sms soap sockets source space spam sql syntax system table tutorial update upload url validation validator variable video web xml youtube





