Jump to content

Recommended Posts

looping through a multi dimentional arrry

 

 

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.

 

|------|

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

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

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

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

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

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

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

 

 

 

<?
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>";
?>

  • 2 weeks later...

Hi

 

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.

 

 

 

<?


//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 = 1;
	   	  $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>";
?>

1) try loading the page in your web browser and then see exactly what kind of HTML code is sent to the browser.  Copy paste that here.

 

2) you can make your code a little easier to read by using single quotes for your echo statements if you need to have double quotes in the echo'd portion.

 

This:

    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>";

 

would become:

    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>';

an extension to doni's post, it would become:

 

	    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>';

as you need to concatenate variables in literal strings.

Actually,  you DON'T need to concatenate the vars separately (although I personally prefer to because I find it easier to see where my vars are).  The following is an example copy/pasted from www.php.net/echo.

 

echo "foo is $foo"; // foo is foobar

Sorry--I just re-read the example a little more closely.

 


// Using single quotes will print the variable name, not the value  <--I missed this part
echo 'foo is $foo'; // foo is $foo

// You can use variables inside of an echo statement
$foo = "foobar";
$bar = "barbaz";

echo "foo is $foo"; // foo is foobar

 

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?

    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.

 

<?
//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 listed 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>";
?>

pok i think i understand. is this all your code?

 

i would suggest using unix timestamps for this code:

 

   if($interval_24hr >= $avail_day['type_start_time'] && 
       $interval_24hr <= $avail_day['type_end_time'])

 

then it will work. to get a unix timestamp you need the exact date/time, then by using strtotime() you can get the timestamp.

 

eg: 16-06-2001 05:00:00 should return a unix timestamp like so:

 

$stamp = strtotime("16-06-2001 05:00:00");

 

then you can treat it as a pure number (integer) - which will allow you to do greater than or less than statements.

----

 

for me to explain this better, what are the values/array values for these variables:

  $time, $add_time, $avail_days, $events.

 

use print_r() or var_dump() and tell me what exactly it says in the ouput source (html). thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.