Foros del Web » Programando para Internet » PHP »

Mejorar carga de foreach

Estas en el tema de Mejorar carga de foreach en el foro de PHP en Foros del Web. Hola a toda la gente del foro tengo el siguiente problema resulta qque tengo un foreach el cual contiene otros 2 foreach ya que este ...
  #1 (permalink)  
Antiguo 21/03/2014, 08:59
 
Fecha de Ingreso: febrero-2014
Ubicación: Cali
Mensajes: 20
Antigüedad: 10 años, 1 mes
Puntos: 0
Mejorar carga de foreach

Hola a toda la gente del foro tengo el siguiente problema resulta qque tengo un foreach el cual contiene otros 2 foreach ya que este tiene que pasarle una variable a esos otros dos foreach para poder que carguen los datos y lo que pasa es que se demora mucho en la carga de los datos y carga la pagina y al rato los datos como puedo mejorar esto aqui dejo algo de mi codigo.

Código PHP:
Ver original
  1. <body>
  2.    
  3.    
  4.             <?php $Ncabezera=0; $ps=": " ?>
  5.             <?php foreach($listado as $fila){
  6.              
  7.              $idComen= $fila['id'];
  8.              $Ncabezera= $Ncabezera + 1;
  9.              ?>
  10.             <form method="post">
  11.             <div style="float: left; border-bottom: 1px dotted #444; border-width: 2px; margin-top: 10px;">
  12.                  
  13.                  <div style="float: left; width: 100%; margin-bottom: 10px; ">
  14.                    
  15.                     <div style="float: left; width: 60%; margin-top: 5px;">
  16.                    
  17.                         <div style="float: left; width: 100%;">
  18.  
  19.                             <div style="float: left; width: 45%;">
  20.                              
  21.                               <div  style="float:left; width: 35%; height: auto;">
  22.                                   <ul style="font-family:sans-serif; font-size:11px; list-style:none; margin: 0; padding: 0;" align="left">
  23.                                     <li style="color: blue;"><?php echo $Ncabezera;  ?></li>
  24.                                     <li> <IMG SRC="<?php  echo $fila['campo24']; ?>" width=70% height=70px; /> </li>
  25.                                     <li>valor</li>
  26.                                   </ul>
  27.                                    
  28.                               </div>                    
  29.                              
  30.                               <div  style="float:left; width: 65%; height: auto;">
  31.                                  
  32.                                   <ul style="font-family:sans-serif; font-size:12px; list-style:none; margin: 0; padding: 0;" align="left">
  33.                                     <div style="margin-left: 10px;">      
  34.                                        <li><b><?php echo $fila['nombres'] ?></b> </li>
  35.                            
  36.                                        <li><?php echo $fila['campo3'] ?> </li>
  37.        
  38.                                        <li><IMG SRC="images/tel.png"/> <?php echo $fila['campo5'] ?> </li>
  39.                                
  40.                                        <li><IMG SRC="images/cel.png"> <?php echo $fila['campo6'] ?> </li>
  41.                                
  42.                                        <li><IMG SRC="images/dir.png"> <?php echo $fila['campo4'] ?> </li>
  43.                            
  44.                                        <li><IMG SRC="images/mail.png"> <?php echo $fila['campo7'] ?> </li>
  45.                                     </div>
  46.                                   </ul>
  47.                                                                          
  48.                               </div>    
  49.                                              
  50.                             </div>
  #2 (permalink)  
Antiguo 21/03/2014, 09:41
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 11 años, 11 meses
Puntos: 320
Respuesta: Mejorar carga de foreach

Yo veo un solo foreach ahi, si no pones el codigo completo con los 3 no podemos ayudarte en nada
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios
  #3 (permalink)  
Antiguo 21/03/2014, 09:52
 
Fecha de Ingreso: febrero-2014
Ubicación: Cali
Mensajes: 20
Antigüedad: 10 años, 1 mes
Puntos: 0
Respuesta: Mejorar carga de foreach

lo colocare por partes ya que no lo deja cargar todo
Código PHP:
Ver original
  1. <body>
  2.    
  3.    
  4.             <?php $Ncabezera=0; $ps=": " ?>
  5.             <?php foreach($listado as $fila){
  6.              
  7.              $idComen= $fila['id'];
  8.              $Ncabezera= $Ncabezera + 1;
  9.              ?>
  10.             <form method="post">
  11.             <div style="float: left; border-bottom: 1px dotted #444; border-width: 2px; margin-top: 10px;">
  12.                  
  13.                  <div style="float: left; width: 100%; margin-bottom: 10px; ">
  14.                    
  15.                     <div style="float: left; width: 60%; margin-top: 5px;">
  16.                    
  17.                         <div style="float: left; width: 100%;">
  18.  
  19.                             <div style="float: left; width: 45%;">
  20.                              
  21.                               <div  style="float:left; width: 35%; height: auto;">
  22.                                   <ul style="font-family:sans-serif; font-size:11px; list-style:none; margin: 0; padding: 0;" align="left">
  23.                                     <li style="color: blue;"><?php echo $Ncabezera;  ?></li>
  24.                                     <li> <IMG SRC="<?php  echo $fila['campo24']; ?>" width=70% height=70px; /> </li>
  25.                                     <li>valor</li>
  26.                                   </ul>
  27.                                    
  28.                               </div>                    
  29.                              
  30.                               <div  style="float:left; width: 65%; height: auto;">
  31.                                  
  32.                                   <ul style="font-family:sans-serif; font-size:12px; list-style:none; margin: 0; padding: 0;" align="left">
  33.                                     <div style="margin-left: 10px;">      
  34.                                        <li><b><?php echo $fila['nombres'] ?></b> </li>
  35.                            
  36.                                        <li><?php echo $fila['campo3'] ?> </li>
  37.        
  38.                                        <li><IMG SRC="images/tel.png"/> <?php echo $fila['campo5'] ?> </li>
  39.                                
  40.                                        <li><IMG SRC="images/cel.png"> <?php echo $fila['campo6'] ?> </li>
  41.                                
  42.                                        <li><IMG SRC="images/dir.png"> <?php echo $fila['campo4'] ?> </li>
  43.                            
  44.                                        <li><IMG SRC="images/mail.png"> <?php echo $fila['campo7'] ?> </li>
  45.                                     </div>
  46.                                   </ul>
  47.                                                                          
  48.                               </div>    
  49.                                              
  50.                             </div>
  51.                                    
  52.                             <div style="float: left; width: 50%;">
  53.                                
  54.                                 <div style="float: left; width: 100%;">
  55.        
  56.                                     <div style="float: left;" >
  57.                                         <IMG SRC="images/referido.png" class="Adias" title="<?php echo "Referido"; ?>"/>
  58.                                     </div>
  59.                        
  60.                                     <div style="float: right; ">
  61.                                         <IMG SRC="images/recordatorio.png" class="Arecordatorio" title="<?php echo "Recordatorio"; ?>"/>
  62.                                     </div>
  63.                                    
  64.                                 </div>
  65.                                
  66.                                 <div style="float: left; width: 100%;">
  67.                                     <iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" width="100%" src="https://maps.google.es/?ie=UTF8&amp;ll=3.444883,-76.437378&amp;spn=0.834815,1.352692&amp;t=m&amp;z=10&amp;output=embed"></iframe><br />
  68.                                 </div>
  69.                        
  70.                             </div>
  71.                        
  72.                         </div>
  73.                        
  74.                         <form method="post">
  75.                         <div style="float: left; width: 95%;">
  76.                        
  77.                             <label>Seguimiento</label> <label style="float: right;" > <a href="#">Ver mas comentarios</a></label>
  #4 (permalink)  
Antiguo 21/03/2014, 09:52
 
Fecha de Ingreso: febrero-2014
Ubicación: Cali
Mensajes: 20
Antigüedad: 10 años, 1 mes
Puntos: 0
Respuesta: Mejorar carga de foreach

Código PHP:
Ver original
  1. <?php  
  2.                             $comentarios= new Prospecto();
  3.                             $lista = $comentarios->comentarios($idComen);
  4.                            
  5.                             ?>
  6.                             <?php foreach($lista as $campo) {
  7.                                
  8.                                
  9.                                 $fechaCom = $campo['fecha'];
  10.                                 $horaCom = $campo['hora'];
  11.                                 date_default_timezone_set('UTC');
  12.                                 date_default_timezone_set("America/Bogota");
  13.                                 $fechaSis = date("Y-m-d");
  14.                                 $horaSis = date ("H:i:s",time());
  15.                                 //echo $prueba = date("Y-m-d", date());
  16.                                 $dias= new Prospecto();
  17.                                 $diasImp = $dias->dateDif($fechaCom,$fechaSis);
  18.                                 $horas = new Prospecto();
  19.                                 $horasImp = $horas->RestarHoras($horaCom,$horaSis);
  20.                                 ?>
  21.                                
  22.                                 <div style="border:1px solid #ffffff;   background-color: #edeff4; margin-top: 2px;" >
  23.                                 <li style="list-style: none;"> <label style="font-family: sans-serif; color: blue; font-size: 14px;"> <?php print_r($_SESSION['NOMBRE']);echo $ps;?></label> <label style="font-size: 13px;"> <?php echo $campo['comentario']; ?></label> </li>
  24.                                 <?php
  25.                                
  26.                                 if($diasImp==1 or $diasImp > 1){
  27.                                    
  28.                                 ?>
  29.                                 <li style="list-style: none;"> <label style="font-family: sans-serif; color: gray; font-size: 10px;" >hace: <?php echo $diasImp; ?> dia(s) &nbsp;</label> </li>
  30.                                 <?php    
  31.                                 }elseif($diasImp < 1){
  32.                                     $horasVer= substr($horasImp,0,2);
  33.                                     if($horasVer < 1){
  34.                                         $minutos = substr($horasImp,3,2);
  35.                                 ?>
  36.                                 <li style="list-style: none;"> <label style="font-family: sans-serif; color: gray; font-size: 10px;" >hace: <?php echo $minutos; ?> minutos &nbsp;</label> </li>
  37.                                 <?php
  38.                                     }elseif($horasVer == 1 or $horasVer > 1){
  39.                                 ?>
  40.                                 <li style="list-style: none;"> <label style="font-family: sans-serif; color: gray; font-size: 10px;" >hace: <?php echo $horasVer; ?> hora(s) &nbsp;</label> </li>
  41.                                 <?php        
  42.                                     }
  43.                                    
  44.                                 }
  45.                                 ?>
  46.                                 </div>
  47.                             <?php } ?>
  48.                             <div style="border:1px solid #000000;   background-color: #edeff4; margin-top: 2px;">
  49.                                 <option style="visibility: hidden;" id="id_pros" value="<?php echo $campo['id_pros']; ?>"></option>
  50.                                 <input type="text" id="nuevoComentario" placeholder="Escriba aqui un nuevo comentario" style="width: 100%;" onkeypress="validar(event)" value=" " />
  51.                                 <li style="list-style: none; font-size: 11px; ">
  52.                                     <label>Recordatorio?</label>
  53.                                     <input type="checkbox" id="recordatorio" name="recordatorio" value='1' onchange="javascript:activar()"  />
  54.                                 </li>
  55.                                 <li style="list-style: none; font-size: 11px; "  >
  56.                                     &nbsp;
  57.                                     <label>Tipos: </label>
  58.                                     <select id="tipos" disabled="disabled"  >
  59.                                         <option value="1">Llamada</option>
  60.                                         <option value="2">Visita</option>
  61.                                         <option value="3">Confirmacion</option>
  62.                                     </select> &nbsp;
  63.                                     <label> Fecha</label>
  64.                                     <input type="date" name="fecha"  value="" disabled="disabled" id="fecha" />&nbsp;
  65.                                     <label>Hora</label>
  66.                                     <input type="time" name="hora" value=""  id="hora" disabled="false" /> &nbsp;
  67.                                     <input type="button" value="Guardar" id="guardarRecor" disabled="disabled"  />
  68.                                 </li>
  69.                              </div>
  70.                         </div>
  71.                         </form>
  72.                     </div>
  73.                    
  74.                     <div style="float: left; width: 35%;">
  75.                       <li style="list-style: none;">
  76.                        <label style="font-family: sans-serif; font-size: 13px; color: blue; padding: 10px 0 0 10px;">Documentos</label> <label style="float: right; color: blue;" > <a href="#">Ver mas Documentos</a></label>
  77.                       </li>
  78.                        <div style="float: left; width: 100%; padding: 10px 0 0 10px;">
  79.                                 <div style="float: left; width: 100%; border:1px solid #000000;  background-color: #edeff4;">
  80.                                     <li style="list-style: none;"> Cuadro 1</li>
  81.                                 </div>
  82.                                 <li style="list-style: none;">
  83.                                 <label style="font-family: sans-serif; font-size: 13px; color: blue; margin-top: 2px;">Recordatorios</label> <label style="float: right; color: blue;" > <a href="#">Ver mas Recordatorios</a></label>
  84.                                 </li>
  85.                                 <div style="float: left; width: 100%; border:1px solid #000000; margin-top: 2px; background-color: #edeff4;" >
  86.                          <?php
  87.                        
  88.                         $recordatorios= new Prospecto();
  89.                         $listaR = $recordatorios->cargarRecordatorios($idComen);
  90.                        
  91.                         foreach($listaR as $columna){
  92.                           ?>
  93.                         <option style="visibility: hidden;" id="id_prosp" value="<?php echo $campo['id_pros']; ?>"></option>
  94.                         <li style="list-style: none; font-family: sans-serif; font-size: 11px;">
  95.                         <?php
  96.                         echo $columna['nombre'].'-'.$columna['hora_recor'].': '.$columna['recordatorio'];
  97.                          ?>
  98.                          </li>                                
  99.                         <?php
  100.                          }
  101.                         ?>    
  102.                                 </div>                  
  103.                         </div>                        
  104.                        
  105.                     </div>
  106.                  
  107.                 </div>
  108.                  
  109.            </div>          
  110.  
  111.            
  112.          </form>
  113.           <?php } ?>
  114.        
  115.     </body>
  #5 (permalink)  
Antiguo 22/03/2014, 15:20
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 11 años, 11 meses
Puntos: 320
Respuesta: Mejorar carga de foreach

no hay mucho para cambiarle a ese codigo, lo que puedes hacer es paginar los dos foreach interiores.

lo que yo haria es un banchmark a ese codigo para ver donde esta el cuello de botella real. en modo local el webGrind es muy util ya que te identificara con precicion el codigo que mas demora, sino puedes optar por una clase asi (o una mas sofisticada):
Código PHP:
Ver original
  1. <?php
  2.     class simpleBench
  3.     {
  4.         private $tiempos = array();
  5.        
  6.         public function initPoint($nombre)
  7.         {
  8.             $this->tiempos[$nombre] = microtime(true);
  9.         }
  10.        
  11.         public function endPoint($nombre)
  12.         {
  13.             $this->tiempos[$nombre] = microtime(true)-$this->tiempos[$nombre];
  14.         }
  15.        
  16.         public function dumpBench($nombre = '')
  17.         {
  18.             var_dump($this->tiempos);
  19.         }
  20.     }
  21.    
  22.     $miBench = new simpleBench();
  23. ?>
para ver que es lo que demora, primero la usas sobre el primer foreach, luego sobre el segundo y luego sobre el tercero.
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios

Etiquetas: foreach, mejorar, variable
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




La zona horaria es GMT -6. Ahora son las 04:37.