Ver Mensaje Individual
  #8 (permalink)  
Antiguo 08/04/2013, 13:19
cyberjas2001
 
Fecha de Ingreso: marzo-2013
Mensajes: 26
Antigüedad: 11 años, 1 mes
Puntos: 0
Respuesta: Crear un menu en PHP a partir de un directorio

Cita:
Iniciado por Triby Ver Mensaje
Podría ser más fácil crear un arreglo del tipo:

Código PHP:
Ver original
  1. $menu = array(
  2.        2012 => array('pdf1', 'pdf2', 'etc'),
  3.        2013 => array('pdf1', 'pdf2', 'etc'),
  4. );

Más o menos así:
Código PHP:
Ver original
  1. $menu = array();
  2. $files = glob(/* filtro */);
  3. foreach($files as $pathToYear) {
  4.     // Obtienes nombre del archivo, supongamos $file
  5.     // Y año, supongamos $year, ya sea con substr() o alguna expresión regular
  6.  
  7.     // Si no existe el año en $menu, lo creas como array
  8.     if(!isset($menu[$year])) {
  9.         $menu[$year] = array();
  10.     }
  11.     // Agregas el archivo
  12.     $menu[$year][] = $file;
  13. }

Al final recorres ese arreglo para crear el menú.
Hola Triby y gracias por tu respuesta, puse tu recomendacion en mi codigo pero solo me imprime el ultimo archivo? tal vez lo estoy ubicando mal? Me ayudas?
Código PHP:
$menu = array();
 
$counter 0// Set counter to 0
    
foreach (glob("/usr/apps/webdata/backend/assets/newsletters/temp/*.pdf") as $pathToYear) { // Grab files from the newsletter folder and save them to variable
        
$th_filename basename($pathToYear); // Strip the newsletter filename from the path
        
$filename str_replace('newsletter_'''$th_filename); // Strip newsletter_ from the filename and save it to a different variable
        
$pathToFull '/usr/apps/webdata/backend/assets/newsletters/temp/' $filename// Rebuild the path to the full-size image
        
        //Function to  get Year & Issue#
        
$string $filename;                
                    if(
preg_match_all("#(\d{1,})#"$string$matchesPREG_SET_ORDER)) 
                    {
                            foreach(
$matches as $match
                              {
                                  
                            
$issue $match[0];
                            
                                while (list(
$key$value) = each($match)) 
                                {
                                
//YEAR
                                    
if (strlen ($value) == 4)
                                    {
                                    
$uyear $value;                                    
                                    }
                                
// ISSUE#
                                    
elseif (strlen ($value) == 1)
                                    {
                                    
$uissue $value;
                                    }
                                 }            
                           }
                     }    
        
//
        // Si no existe el año en $menu, lo creas como array
        
if(!isset($menu[$uyear])) 
            {
            
$menu[$uyear] = array();
            }
    
// Agregas el archivo
            
$menu[$uyear][] = $uissue;
            
        
$counter++; // Increment counter by 1 until no file exists
        
    
}

        echo 
'<div id=cssmenu>';
        echo 
'<ul>';
        echo 
'   <li class=has-sub last><a href=#><span>Please Select a Year:</span></a>';
        echo 
'      <ul>';
        echo 
"<li class='has-sub'><a href='#'><span>".$uyear." </span></a>";
        echo 
'            <ul>';
        echo 
"<li class=last><a href=# onclick=myPDF('".$filename."')> Issue# ".$uissue."</a></li>";
        echo 
'            </ul>';
        echo 
'         </li>';
        echo 
'      </ul>';
        echo 
'   </li>';
        echo 
'</ul>';
        echo 
'</div>'