943,929 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1169
  • PHP RSS
Mar 31st, 2008
0

looping through a multi dimentional arrry

Expand Post »
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.

|------|
|------|-------|
|------|-------|-------|
|------|-------|-------|-------|
|------|-------|-------|-------|-------|
|------|-------|-------|-------|-------|-------|
|------|-------|-------|-------|-------|-------|--------|
|------|-------|-------|-------|-------|-------|--------|



PHP Syntax (Toggle Plain Text)
  1. <?
  2. echo"<table>";
  3.  
  4.  
  5. //get availability nfo
  6. $query = "SELECT DISTINCT(a.time_id), a.start_time, a.end_time, c.colour
  7. FROM available a, type_display c
  8. WHERE a.type_code = c.type_code
  9. AND '$event_date' BETWEEN a.start_date AND a.end_date
  10. AND a.org_pro_id = '$org_pro_id'
  11. AND a.week_day = '$day'
  12. GROUP BY a.start_time";
  13. $result = mysqli_query($mysqli, $query) or die('Error, query failed');
  14. while($row = mysqli_fetch_array($result))
  15. {
  16.  
  17. $avail_day1[] = $row; //this only shows one of the seven days to save on clutter
  18. }
  19.  
  20.  
  21. /**Note:all contain table data time_id, start_time, end_time, colour**/
  22. $all_avail_days = array($avail_day1, $avail_day2, $avail_day3, $avail_day4,
  23. $avail_day5, $avail_day6, $avail_day7);
  24.  
  25.  
  26. for($wk_day =0; $wk_day < count($all_avail_days); $wk_day++)//wk_day layer
  27. {
  28. $day = $wk_day + 1; echo":";
  29. foreach($all_avail_days as $key => $avail)//rows
  30. {
  31. for($j = 0; $j < count($all_avail_days[$key]); $j++)//column
  32. {
  33. if($day == 1)
  34. {
  35. $time_id1 = $all_avail_days[$wk_day][$key][time_id];
  36. $type_start_time1 = $all_avail_days[$wk_day][$key][start_time];
  37. $type_end_time1 = $all_avail_days[$wk_day][$key][end_time];
  38. $colour1 = "#".$all_avail_days[$wk_day][$key][colour]."</br>";
  39. }
  40.  
  41.  
  42. //Note: days 2 to 6 removed to limit clutter..........
  43.  
  44. if($day == 7)
  45. {
  46. $time_id7 = $all_avail_days[$wk_day][$key][time_id];
  47. $type_start_time7 = $all_avail_days[$wk_day][$key][start_time];
  48. $type_end_time7 = $all_avail_days[$wk_day][$key][end_time];
  49. $colour7 = "#".$all_avail_days[$wk_day][$key][colour]."</br>";
  50. }
  51.  
  52.  
  53. echo"<tr>
  54. <td width=\"8%\" height=\"12\">&nbsp;</td>
  55. <td width=\"13%\" height=\"12\" bgcolor=\"$colour1\">
  56. $type_start_time1</td>
  57. <td width=\"13%\" height=\"12\" bgcolor=\"$colour2\">
  58. $type_start_time2</td>
  59. <td width=\"13%\" height=\"12\" bgcolor=\"$colour3\">
  60. $type_start_time3</td>
  61. <td width=\"13%\" height=\"12\" bgcolor=\"$colour4\">
  62. $type_start_time4</td>
  63. <td width=\"13%\" height=\"12\" bgcolor=\"$colour5\">
  64. $type_start_time5</td>
  65. <td width=\"13%\" height=\"12\" bgcolor=\"$colour6\">
  66. $type_start_time6</td>
  67. <td width=\"13%\" height=\"12\" bgcolor=\"$colour7\">
  68. $type_start_time7</td>";
  69. echo"</tr>\n";
  70.  
  71.  
  72. }
  73. }
  74. }
  75.  
  76. echo"</table>";
  77. ?>
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Apr 1st, 2008
1

Re: looping through a multi dimentional arrry

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:
  • 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 else for each if($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 with printf()
  • 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.
Reputation Points: 27
Solved Threads: 16
Junior Poster
petr.pavel is offline Offline
116 posts
since Mar 2008
Apr 9th, 2008
0

Re: looping through a multi dimentional arrry

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

PHP Syntax (Toggle Plain Text)
  1. <?
  2.  
  3.  
  4. //work hours
  5. $start_time = "09:00:00";
  6. $end_time = "05:00:00";
  7.  
  8.  
  9. //get availability nfo
  10. /** selected data time_id, week_day, start_date, end_date, start_time, end_time, colour
  11.   in array**/
  12. $avail_day; //availaibility
  13.  
  14.  
  15. /**selected data event_id, event_name, event_date, event_time in array**/
  16. $events;//appointment
  17.  
  18.  
  19. echo"<table>";
  20.  
  21. //Loop over the hours
  22. for($time = $start_time; $time <= $end_time; $time += $add_time)
  23. {
  24. //format 24 hour time interval for passing via url
  25. $interval_24hr = date("H:i:s", $time);
  26.  
  27.  
  28. echo "<tr>";
  29. //Output the time interval label
  30. echo"<td width=\"8%\" height=\"15\" bgcolor=\"\" align=\"center\">
  31. <ul>
  32. <li>".date("h:i A", $time)."</li>
  33. </ul>
  34. </td>";
  35.  
  36. //loop to display days of the week
  37. foreach($avail_day as $key => $avail)//rows
  38. {
  39. //diaplay event type colour and labeling to event end time
  40. $seg_code = $avail_day['event_type_code'];
  41. $time_id = $avail_day['time_id'];
  42. $seg_day = $avail_day['week_day'];
  43.  
  44. //monday
  45. if($seg_day == 1 && $interval_24hr >= $avail_day['start_time']&&
  46. $interval_24hr <= $avail_day['end_time'])
  47. {
  48. $colour1 = "#".$avail_day['colour'];
  49. }
  50. elseif($seg_day == 1)
  51. {
  52. $colour1 = "#ebeae0";
  53. }
  54.  
  55. //note tuesday to sunday not listed for space reasons
  56.  
  57.  
  58. foreach($events as $key => $event)
  59. {
  60. //determine day of week
  61. $week_day = date('l',strtotime($event['event_date']));
  62. //appointment or name description
  63. if($week_day == "Monday" && $event_time >= $time &&
  64. $event_time < ($time + $add_time))
  65. {
  66. $day = 550;
  67. $event_name1 = $event['last_name'];
  68. }
  69. elseif
  70. {
  71. /**tuesday to sunday not listed for space reasons**/
  72. }
  73. }
  74. }//end foreach postion#1
  75.  
  76.  
  77. echo"<td width=\"13%\" height=\"12\" bgcolor=\"$colour1\">
  78. $event_name1</td>
  79. <td width=\"13%\" height=\"12\" bgcolor=\"$colour2\">
  80. $event_name2</td>
  81. <td width=\"13%\" height=\"12\" bgcolor=\"$colour3\">
  82. $event_name3</td>
  83. <td width=\"13%\" height=\"12\" bgcolor=\"$colour4\">
  84. $event_name4</td>
  85. <td width=\"13%\" height=\"12\" bgcolor=\"$colour5\">
  86. $event_name5</td>
  87. <td width=\"13%\" height=\"12\" bgcolor=\"$colour6\">
  88. $event_name6</td>
  89. <td width=\"13%\" height=\"12\" bgcolor=\"$colour7\">
  90. $event_name7</td>";
  91. echo"</tr>\n";
  92.  
  93.  
  94. // }//end foreach postion#2
  95. }//for
  96.  
  97.  
  98.  
  99. echo"</table>";
  100. ?>
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Apr 13th, 2008
0

Re: looping through a multi dimentional arrry

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?
PHP Syntax (Toggle Plain Text)
  1. if($interval_24hr >= $avail_day['type_start_time'] &&
  2. $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)
  1. <?
  2. //work hours
  3. $start_time = "09:00:00";
  4. $end_time = "05:00:00";
  5.  
  6.  
  7. //get availability nfo
  8. /** selected data time_id, week_day, start_date, end_date, start_time, end_time, colour
  9.   in array**/
  10. $avail_days; //availaibility
  11.  
  12.  
  13. /**selected data event_id, event_name, event_date, event_time in array**/
  14. $events;//appointment
  15.  
  16.  
  17. echo"<table>";
  18.  
  19.  
  20. //Loop over the hours
  21. for ($time = $start_time; $time <= $end_time; $time += $add_time)
  22. {
  23. //format 24 hour time interval for passing via url
  24. $interval_24hr = date("H:i:s", $time);
  25.  
  26. /**-----------------event time listing and event type--------------**/
  27. echo "<tr>";
  28. //Output the time interval label
  29. echo"<td width=\"8%\" height=\"15\" bgcolor=\"\" align=\"center\">
  30. <ul>
  31. <li>".date("h:i A", $time)."</li>
  32. </ul>
  33. </td>";
  34.  
  35.  
  36. /**----------------------event type display------------------------------**/
  37. //loop through array to diaplay event type colour and labeling
  38. foreach ($avail_days as $avail_day)
  39. {
  40. //diaplay event type colour and labeling to event end time
  41. if($interval_24hr >= $avail_day['type_start_time'] &&
  42. $interval_24hr <= $avail_day['type_end_time'])
  43. {
  44. $seg_colour = "#".$avail_day['colour'];
  45. $time_id = $avail_day['time_id'];
  46. $seg_day = $avail_day['week_day'];
  47.  
  48. //monday
  49. if($seg_day == 1)
  50. {
  51. $colour1 = $seg_colour;
  52. }
  53. //tuesday
  54. elseif($seg_day == 2)
  55. {
  56. /**note elseif for tuesday to sunday
  57.   not isted for space reasons**/ }
  58.  
  59. /**----------------------event--------------------**/
  60. /*loop to provide appointment linked to appropriate event time
  61.   slot goes here*/
  62.  
  63. /**---------------dynamic looping rows and columns----------**/
  64. echo'<td width="13%" height="12" bgcolor="$colour1">
  65. $event_name1</td>
  66. <td width="13%" height="12" bgcolor="$colour2">
  67. $event_name2</td>
  68. <td width="13%" height="12" bgcolor="$colour3">
  69. $event_name3</td>
  70. <td width="13%" height="12" bgcolor="$colour4">
  71. $event_name4</td>
  72. <td width="13%" height="12" bgcolor="$colour5">
  73. $event_name5</td>
  74. <td width="13%" height="12" bgcolor="$colour6">
  75. $event_name6</td>
  76. <td width="13%" height="12" bgcolor="$colour7">
  77. $event_name7</td>";
  78. echo"</tr>\n";
  79. }
  80.  
  81. echo"</table>";
  82. ?>
  83.  
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Apr 20th, 2008
0

Re: looping through a multi dimentional arrry

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.

PHP Syntax (Toggle Plain Text)
  1. <?
  2. //work hours
  3. $min_start1 = "09:30:00";//earliest appointment
  4. $start_time = "09:00:00";//office hours
  5. $end_time = "05:00:00";
  6.  
  7.  
  8. //get availability nfo
  9. /** selected data time_id, week_day, start_date, end_date,
  10.   start_time, end_time, colour in array**/
  11. $avail_days; //availaibility
  12.  
  13.  
  14. /**selected data event_id, event_name, event_date, event_time in array**/
  15. $events;//appointment
  16.  
  17.  
  18. echo"<table>";
  19.  
  20.  
  21. //Loop over the hours
  22. for ($time = $start_time; $time <= $end_time; $time += $add_time)
  23. {
  24. //format 24 hour time interval for passing via url
  25. $interval_24hr = date("H:i:s", $time);
  26.  
  27. /**-----------------------event time listing and event type------------------**/
  28. echo "<tr>";
  29. //Output the time interval label
  30. echo"<td width=\"8%\" height=\"15\" bgcolor=\"\" align=\"center\">
  31. <ul>
  32. <li>".date("h:i A", $time)."</li>
  33. </ul>
  34. </td>";
  35.  
  36.  
  37. /**----------------------event type display------------------------------**/
  38. //loop through array to diaplay event type colour and labeling
  39. foreach ($avail_days as $avail_day)
  40. {
  41. //day of the week Mon to sun
  42. $seg_day = $avail_day['week_day'];
  43.  
  44. //diaplay event type colour and labeling to event end time
  45. //monday
  46. if($seg_day == 1 && $interval_24hr >= $avail_day['start_time'] &&
  47. $interval_24hr <= $end_time)
  48. {
  49. $colour1 = "#".$avail_day['colour'];
  50. }
  51. elseif($seg_day == 1 && $interval_24hr > $end_time ||
  52. $seg_day == 1 && $interval_24hr < $min_start1)
  53. {
  54. $colour1 = "#ebeae0";//default background colour
  55. }
  56. //tuesday
  57. if($seg_day == 2)
  58. {
  59. /*note: for tuesday to sunday not listed for space reasons
  60. it is the same as for monday except $colour2 etc is used*/
  61. .....
  62. }
  63.  
  64. /**----------------------event--------------------**/
  65. /*loop to provide appointment linked to appropriate event time slot
  66. goes here*/
  67.  
  68. /**------------------dynamic looping rows and columns--------------**/
  69. echo'<td width="13%" height="12" bgcolor="$colour1">
  70. $event_name1</td>
  71. <td width="13%" height="12" bgcolor="$colour2">
  72. $event_name2</td>
  73. <td width="13%" height="12" bgcolor="$colour3">
  74. $event_name3</td>
  75. <td width="13%" height="12" bgcolor="$colour4">
  76. $event_name4</td>
  77. <td width="13%" height="12" bgcolor="$colour5">
  78. $event_name5</td>
  79. <td width="13%" height="12" bgcolor="$colour6">
  80. $event_name6</td>
  81. <td width="13%" height="12" bgcolor="$colour7">
  82. $event_name7</td>";
  83. echo"</tr>\n';
  84. }
  85.  
  86. echo"</table>";
  87. ?>
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Extracting select option fields from mySQL
Next Thread in PHP Forum Timeline: You don't have permission to access /.shtml on this server.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC