Foros del Web » Programando para Internet » PHP »

Graficar Horario en tabla

Estas en el tema de Graficar Horario en tabla en el foro de PHP en Foros del Web. Les comento estimados , estoy tratando de mostrar horarios de diferentes cursos. Pero sucede que puedo mostrar solo la hora que inicia y la hora ...
  #1 (permalink)  
Antiguo 05/04/2015, 12:18
Avatar de nexus44  
Fecha de Ingreso: octubre-2012
Ubicación: Piura
Mensajes: 108
Antigüedad: 11 años, 5 meses
Puntos: 1
Exclamación Graficar Horario en tabla

Les comento estimados , estoy tratando de mostrar horarios de diferentes cursos.
Pero sucede que puedo mostrar solo la hora que inicia y la hora que termina , mientras tanto no muestra los intervalos que hay entre ese rango de horas:

Ejemplo
Inicia : 07:00
Finaliza: 10:00



Codigo:
Código PHP:
Ver original
  1. <?php
  2. mysql_connect("localhost","root","");
  3. mysql_select_db("horario");
  4. ?>
  5. <?php
  6. $busco = mysql_query("SELECT * FROM horario WHERE cedula_personal ='12345678' ") or die(mysql_error());
  7. $dias = array('1','2','3','4','5');
  8. $num=mysql_num_rows($busco);
  9. if ($num > 0) {
  10.     while ($de = mysql_fetch_assoc($busco)) {
  11.         $h_inicio[] = $de['h_entrada'];
  12.         $h_fin[] = $de['h_salida'];
  13.         $materias[] = $de['materia'];
  14.         $diasf[] = $de['dia'];
  15.     echo    $total[]=$de['h_salida']-$de['h_entrada'];
  16.     }
  17. }  
  18. ?>
  19. <table width="800" border="1" align="center">
  20.   <tr >
  21.     <td width="126" bgcolor="#FFCC00"><div align="center"><strong>Horas Academicas</strong></div></td>
  22.     <td width="126" bgcolor="#FFCC00"><div align="center"><strong>Lunes</strong></div></td>
  23.     <td width="126" bgcolor="#FFCC00"><div align="center"><strong>Martes</strong></div></td>
  24.     <td width="126" bgcolor="#FFCC00"><div align="center"><strong>Miercoles</strong></div></td>
  25.     <td width="126" bgcolor="#FFCC00"><div align="center"><strong>Jueves</strong></div></td>
  26.     <td width="130" bgcolor="#FFCC00"><div align="center"><strong>Viernes</strong></div></td>
  27.   </tr>
  28.   <tr>
  29.     <td height="34" bgcolor="#66CCFF">07:00-07:40 </td>
  30.     <td colspan="5" rowspan="17"  height="35">
  31.     <?php
  32.     $to=0;
  33.     echo "<table width='800' border='1'>";
  34.         for ($i = 1; $i <= 17; $i++) {
  35.             echo "<tr width='98' height='30'>";
  36.             for ($j = 1; $j <= 5; $j++) {
  37.                 echo "<td width='100'>";
  38.                 for ($k = 0; $k < $num; $k++) {
  39.                      
  40.                     if ($diasf[$k] == $j && ($h_inicio[$k] == $i || $h_fin[$k] == $i)) {
  41.                         echo '<div style="font-size:10px">'.$materias[$k].'</div>';
  42.                                    
  43.                     }
  44.                      
  45.                 }
  46.                
  47.                  
  48.             echo "</td>";
  49.             }
  50.             echo "</tr>";
  51.         }
  52.     echo "</table>";
  53.     ?>    </td>
  54.   </tr>
  55.   <tr bgcolor="#66CCFF">
  56.     <td height="30">07:40-08:20 </td>
  57.   </tr>
  58.   <tr bgcolor="#66CCFF">
  59.     <td height="30">08:20-09:00 </td>
  60.   </tr>
  61.   <tr bgcolor="#66CCFF">
  62.     <td height="30" bgcolor="#6699FF">09:00-09:20 </td>
  63.   </tr>
  64.   <tr bgcolor="#66CCFF">
  65.     <td height="30">09:20-10:00</td>
  66.   </tr>
  67.   <tr bgcolor="#66CCFF">
  68.     <td height="30">10:00-10:40 </td>
  69.   </tr>
  70.   <tr bgcolor="#66CCFF">
  71.     <td height="30">10:40-11:20 </td>
  72.   </tr>
  73.   <tr bgcolor="#66CCFF">
  74.     <td height="28">11:20-12:00 </td>
  75.   </tr>
  76.   <tr bgcolor="#66CCFF">
  77.     <td height="30">12:00-12:40 </td>
  78.   </tr>
  79.   <tr bgcolor="#66CCFF">
  80.     <td height="30">01:00-01:40 </td>
  81.   </tr>
  82.   <tr bgcolor="#66CCFF">
  83.     <td height="30">01:40-02:20 </td>
  84.   </tr>
  85.   <tr bgcolor="#66CCFF">
  86.     <td height="28">02:20-03:00 </td>
  87.   </tr>
  88.   <tr bgcolor="#66CCFF">
  89.     <td height="30">03:00-03:40 </td>
  90.   </tr>
  91.   <tr bgcolor="#66CCFF">
  92.     <td height="30" bgcolor="#6699FF">03:40-04:00 </td>
  93.   </tr>
  94.   <tr bgcolor="#66CCFF">
  95.     <td height="30">04:00-04:40 </td>
  96.   </tr>
  97.   <tr bgcolor="#66CCFF">
  98.     <td height="30">04:40-05:20 </td>
  99.   </tr>
  100.   <tr bgcolor="#66CCFF">
  101.     <td height="30">05:20-06:00 </td>
  102.   </tr>
  103. </table>

La base de datos:
Código MySQL:
Ver original
  1. -- Estructura de tabla para la tabla `horario`
  2. --
  3.  
  4. CREATE TABLE IF NOT EXISTS `horario` (
  5.   `id` int(11) NOT NULL AUTO_INCREMENT,
  6.   `cedula_personal` int(8) NOT NULL,
  7.   `grado` varchar(10) NOT NULL,
  8.   `seccion` int(2) NOT NULL,
  9.   `aula` varchar(20) NOT NULL,
  10.   `materia` varchar(60) NOT NULL,
  11.   `dia` int(1) NOT NULL,
  12.   `h_entrada` varchar(20) NOT NULL,
  13.   `h_salida` varchar(20) NOT NULL,
  14.   `f_creado` date NOT NULL,
  15.   PRIMARY KEY (`id`)
  16. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
  17.  
  18. --
  19. -- Volcado de datos para la tabla `horario`
  20. --
  21.  
  22. INSERT INTO `horario` (`id`, `cedula_personal`, `grado`, `seccion`, `aula`, `materia`, `dia`, `h_entrada`, `h_salida`, `f_creado`) VALUES
  23. (1, 12345678, '1', 1, 'AULA 01', 'Informatica', 1, '1', '3', '2012-07-13'),
  24. (2, 12345678, '2', 3, 'AULA 02', 'Educacion para la Salud', 1, '5', '7', '2012-07-13'),
  25. (3, 12345678, '2', 1, 'AULA 01', 'Dibujo tecnico', 1, '8', '10', '2012-07-13'),
  26. (4, 12345678, '1', 1, 'AULA 01', 'Dibujo tecnico', 2, '1', '3', '2012-07-13'),
  27. (5, 12345678, '2', 1, 'AULA 02', 'Informatica', 3, '3', '6', '2012-07-13'),
  28. (16, 12345678, '1', 3, 'AULA 01', 'Informatica', 4, '6', '9', '2012-07-16');
__________________

"Nuestra capacidad se aleja de la Humanidad"
  #2 (permalink)  
Antiguo 05/04/2015, 12:26
Avatar de Djoaq  
Fecha de Ingreso: septiembre-2012
Ubicación: Barcelona
Mensajes: 271
Antigüedad: 11 años, 6 meses
Puntos: 38
Respuesta: Graficar Horario en tabla

Simplemente ... debes recorrer entre la fecha y fín ... googleando un poco he visto :

Código PHP:
function createDateRangeArray($strDateFrom,$strDateTo) {
  
// takes two dates formatted as YYYY-MM-DD and creates an
  // inclusive array of the dates between the from and to dates.

  // could test validity of dates here but I'm already doing
  // that in the main script

  
$aryRange=array();

  
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
  
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

  if (
$iDateTo>=$iDateFrom) {
    
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

    
while ($iDateFrom<$iDateTo) {
      
$iDateFrom+=86400// add 24 hours
      
array_push($aryRange,date('Y-m-d',$iDateFrom));
    }
  }
  return 
$aryRange;

o

Código PHP:

$begin 
= new DateTime'2007-12-31' );
$end = new DateTime'2009-12-31 23:59:59' );

$interval DateInterval::createFromDateString('last thursday of next month');
$period = new DatePeriod($begin$interval$endDatePeriod::EXCLUDE_START_DATE);

foreach ( 
$period as $dt )
  echo 
$dt->format"l Y-m-d H:i:s\n" ); 
http://php.net/manual/es/class.dateperiod.php
http://boonedocks.net/mike/archives/...-with-PHP.html


Por cierto usa mysqli en vez de mysql..
Un saludo!
  #3 (permalink)  
Antiguo 05/04/2015, 12:31
Avatar de nexus44  
Fecha de Ingreso: octubre-2012
Ubicación: Piura
Mensajes: 108
Antigüedad: 11 años, 5 meses
Puntos: 1
Respuesta: Graficar Horario en tabla

Djoaq , gracias por responder pero lo que me brindas sincera mente no me ayuda mucho.
Lo que necesito es sacar los intervalos de horas , no de fechas , debería leer amigo jeje
__________________

"Nuestra capacidad se aleja de la Humanidad"
  #4 (permalink)  
Antiguo 05/04/2015, 14:11
Avatar de Djoaq  
Fecha de Ingreso: septiembre-2012
Ubicación: Barcelona
Mensajes: 271
Antigüedad: 11 años, 6 meses
Puntos: 38
Respuesta: Graficar Horario en tabla

Tienes razón , no leí bien .

Si usaras el tipo de dato time que lleva mysql podrías trabar mejor el tema de los rangos de horas ...

https://dev.mysql.com/doc/refman/5.0/en/time.html


Código PHP:
echo    $total[]=$de['h_salida']-$de['h_entrada']; 
esta mal porque total es un array y no se puede imprimir y asignar al mismo tiempo.
Aunque creo que ya lo sabes y simplemente se ha colado.

A ver si lo entiendo bién , necesitas sacar la diferencia respecto a 2 valores enteros que representan las horas.

Te pongo un sencillo ejemplo de como recorrer la diferencia:

Código PHP:
<?php

$x 
1;
$i=0;
$hora_entrada "3";
$hora_salida "6";
while (
$i <= $x) { 
        
        
$h_ini intval($hora_entrada);
        
$h_fin intval($hora_salida);

        if (
$h_ini<=$h_fin) {
            
$hora_inicio $h_ini;
            
$hora_fin $h_fin;
            for (
$i=$h_ini$i $h_fin$i++) { 
                    
                echo 
"Hora Entrada :".$hora_inicio." Hora Salida :".++$hora_inicio."<br>";
                
/* Esto Produce la siguiente salida : 
                Hora Entrada :3 Hora Salida :4
                Hora Entrada :4 Hora Salida :5
                Hora Entrada :5 Hora Salida :6
                */
            
}    
            
        }
        
$i++;
    }
Un saludo
Suerte!

Última edición por Djoaq; 05/04/2015 a las 14:29
  #5 (permalink)  
Antiguo 05/04/2015, 15:15
Avatar de nexus44  
Fecha de Ingreso: octubre-2012
Ubicación: Piura
Mensajes: 108
Antigüedad: 11 años, 5 meses
Puntos: 1
Respuesta: Graficar Horario en tabla

Muy amable , por responder Djoaq , estoy tratando de adaptarlo al código que tengo e aplicarlo junto con la bd , pero no logro obtener resultados favorables.
Cree que seria tan amable de ayudarme.
Le agradecería.
__________________

"Nuestra capacidad se aleja de la Humanidad"
  #6 (permalink)  
Antiguo 05/04/2015, 15:32
Avatar de Djoaq  
Fecha de Ingreso: septiembre-2012
Ubicación: Barcelona
Mensajes: 271
Antigüedad: 11 años, 6 meses
Puntos: 38
Respuesta: Graficar Horario en tabla

Código PHP:

if ($num 0) { 
    while (
$de mysql_fetch_assoc($busco)) { 

        
$materias[] = $de['materia']; 
        
$diasf[] = $de['dia']; 
        
$h_ini intval($de['h_entrada']);
        
$h_fin intval($de['h_salida']); 

           if (
$h_ini<=$h_fin) {
            
$hora_inicio $h_ini;
            
$hora_fin $h_fin;
            for (
$i=intval($de['h_entrada']); $i intval($de['h_salida']); $i++) { 
                    
                
$total[]= $hora_inicio.":00 - ".++$hora_inicio.":00 ";
            
            }
    
        } 
    }

Prueba a ver .
  #7 (permalink)  
Antiguo 05/04/2015, 15:55
Avatar de nexus44  
Fecha de Ingreso: octubre-2012
Ubicación: Piura
Mensajes: 108
Antigüedad: 11 años, 5 meses
Puntos: 1
Respuesta: Graficar Horario en tabla

Lo que sucede es que si genera los intervalos pero , no los ordena como en la imagen que esta al inicio del post quedan dentro del misma etiqueta td , estoy viendo la forma de que después de comprar el inicial y el final , compare los intervalos , si no fuera mucha molestia podría , verificarlo esta junto con la en la parte de arriba.
__________________

"Nuestra capacidad se aleja de la Humanidad"
  #8 (permalink)  
Antiguo 06/04/2015, 19:37
 
Fecha de Ingreso: marzo-2012
Ubicación: El Vigia Edo Merida
Mensajes: 147
Antigüedad: 12 años, 1 mes
Puntos: 1
Respuesta: Graficar Horario en tabla

ya has solucionado tu problema amigo

Etiquetas: horarios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 17:59.