looping through a multi dimentional arrry

Discussion in 'PHP' started by assgar, Mar 30, 2008.

  1. #1
    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\">&nbsp;</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>";
    ?>
    
    PHP:
     
    assgar, Mar 30, 2008 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    First of all, could you explain in English what you would like the outcome of your code to do.

    Secondly, would it not be possible to put the $key in the multi-dimensional array like the other variables so you have;

    $all_avail_days[$wk_day] = array(key => $key, time_id => $time_id) etc

    then you would just need to do a foreach loop;

    foreach($all_avail_days as $day => $vars){

    echo '<td width=\"13%\" height=\"12\" bgcolor=\"$vars["colour"]\">
    $vars["start_time"]</td>';
    }


    I've probably made a few blips with the syntax so please forgive me if it doesn't work when you copy and paste but the generally idea is there.

     
    Weirfire, Mar 31, 2008 IP
  3. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.

    It appears


    
    <?
    
    
     //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>";
    ?>
    
    PHP:
     
    assgar, Apr 9, 2008 IP
  4. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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'])
     
    PHP:

    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>";
    ?>
    
    PHP:
     
    assgar, Apr 13, 2008 IP
  5. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.

    
     <?
     //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>";
    ?>
    
    PHP:
     
    assgar, Apr 20, 2008 IP