Foros del Web » Programando para Internet » PHP »

Galeria Fotos |||Directorios|||

Estas en el tema de Galeria Fotos |||Directorios||| en el foro de PHP en Foros del Web. Hola, estoy intentando hacer una especie de galería, mediante directorios. Pero me he quedado estancado a la hora de mostrar las imágenes en una <table>. ...
  #1 (permalink)  
Antiguo 07/05/2004, 10:25
 
Fecha de Ingreso: octubre-2003
Mensajes: 46
Antigüedad: 20 años, 6 meses
Puntos: 0
Galeria Fotos |||Directorios|||

Hola, estoy intentando hacer una especie de galería, mediante directorios. Pero me he quedado estancado a la hora de mostrar las imágenes en una <table>. La quiero mostrar en una <table> de 3 columnas por X filas, las que sean necesarias para que se muestren todas las fotos de la carpeta, no quiero paginar los resultados por que en las carpetas habrá pocas fotos. Tal y como lo tengo ahora, 3 columnas y 7 filas, me muestra 21 fotos, pero si pongo una carpeta con 15 fotos, el bucle condicional “for” me sigue mostrando que hay 21 fotos, con lo que 6 de las 21 fotos aparecen con una cruz roja de imagen no encontrada, ya que no existen, solo hay 15. Así que la pregunta es: ¿Cómo hago para crear las filas necesarias para mostrar las fotos de cada carpeta? ¿Y que no me muestre la cruz roja? Pongo el código que tengo hecho hasta hora:
Código PHP:
<?php
$thumb
="grafics/galeria/".$carpeta."/thumb";
$foto="grafics/galeria/".$carpeta."";
$dire dir($thumb);
$dire->read();
$dire->read();
//Ficar el nom de les imatges amb un Array
while($llegir $dire->read()){
    
$a++;
    
$imagen[$a] = $llegir;
}
$dire->close();
//Mostrem el resultat amb una <table> de x files per 3 columnes
?>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td height="100" align="center">&nbsp;</td>
    <td height="100" align="center"><img src="grafics/imatges/galeria.jpg" width="200" height="70"></td>
    <td height="100" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td height="20" align="center" class="small">Fotos/<? echo "$carpeta"/*nom de la carpeta*/ ?></td>
    <td height="20" align="center">&nbsp;</td>
    <td height="20" align="center">&nbsp;</td>
  </tr>
<?
for ($f=1$f<=7$f++){
    echo 
"<tr>";
    for (
$c=1$c<=3$c++){
        
$i++;
        echo 
"<td align='center' valign='top' class='small'><a href='#'-->
-->onClick=\"foto('$foto/$imagen[$i]','Penya');return false\"><img src='$thumb/$imagen[$i]'\-->
-->"
name='image' width='94' height='70' border='1'></a></td>";
    }
    echo "
</tr>";
}

//menu desplegable per seleccionar carpeta
$path="
grafics/galeria/";
$dir = dir($path);
$dir->read();
$dir->read();
?>
<tr>
    <td height="
20" align="center" class="small">&nbsp;</td>
    <td height="
20" align="center"><form action=index2.php method=get>
Selecciona una galeria:<br> <SELECT name="
carpeta">
<?
while($carpeta = $dir->read()){
    echo "
$carpeta";
    echo "
<OPTION VALUE=".$carpeta.">".$carpeta."</OPTION>n";
}
echo "
</SELECT>";
echo("
<input type=submit value=Buscar>n");
echo("
<input type=hidden name=id value=3>n");
echo ("
</form>n");
$dir->close();
?>
</td>
 <td height="
20" align="center">&nbsp;</td>
  </tr>
</table>
Gracias.
Saludos.

Última edición por Yukas; 07/05/2004 a las 10:30
  #2 (permalink)  
Antiguo 07/05/2004, 11:03
 
Fecha de Ingreso: enero-2004
Ubicación: Santiago - Chile
Mensajes: 45
Antigüedad: 20 años, 3 meses
Puntos: 0
De acuerdo Una ayudadita:

Hola, por lo que puedo apreciar esto es lo que quieres:

Quieres tener 3 columnas y ene filas para las fotos o imagenes que contengas en tu directorio. ok siguiendo esto te puedo decir que al analizar tu codigo podrias hacer esto ( es una idea y espero te funcione);

1.- mediante una funcion CUENTA el numero total de fotos de tu directorio, ejemplo: 20

2.- DIVIDE la cantidad que mostraste por 3 ( que es la cantidad de columnas que quieres ), en este caso el resultado seria 6 y te sobrarian 2, entonces

3.- Mediante el primer FOR dejas de indice el numero entero de la division o sea el 6, ejemplo:

<?
for ($f=1; $f<=$VARIABLE_ENTERA_DE_LA_DIVISION( EN ESTE CASO 6 ); $f++){
echo "<tr>"; //*** esto te generará 6 filas con 3 columnas o sea 18.
for ($c=1; $c<=3; $c++){
$i++;
echo "<td align='center' valign='top' class='small'><a href='#'-->
-->onClick=\"foto('$foto/$imagen[$i]','Penya');return false\"><img src='$thumb/$imagen[$i]'-->
-->"name='image' width='94' height='70' border='1'></a></td>";
};


4.- el resto que te quedo de la division tambien la guardas en una variable y haces algo asi despues del los dos FOR anteriores

if ($VARIABLE DEL RESTO DE LA DIVISION( en este caso 2) > 0)
{
echo"<tr>";
for $x=1; $x<=$VARIABLE DEL RESTO DE LA DIVISON( en este caso 2); $x++)
{
$i++;
echo "<td align='center' valign='top' class='small'><a href='#'-->
-->onClick=\"foto('$foto/$imagen[$i]','Penya');return false\"><img src='$thumb/$imagen[$i]'-->
-->"name='image' width='94' height='70' border='1'></a></td>";
}
echo "</tr>"; //PARA QUE CIERRE LA UNICA FILA,YA QUE NO PODRAN HABER NUNCA UN RESTO SUPERIOR A 3. POR LO QUE NECESITAS SOLO UNA NUEVA FILA.
} // FIN SI.
else
{echo "</tr>"; }// PARA QUE CIERRE LA FILA SI ES QUE NO TIENE RESTO LA DIVISION


Es lo que se me ocurre y si lo entiendes seguro que funciona. Espero haberte ayudado un poco.

Saludos.

DaMMsteiN

PD: codigo completo.

primero cuentas las fotos, luego division por 3, guardas resto y entero y....

<?
for ($f=1; $f<=$VARIABLE_ENTERA_DE_LA_DIVISION( EN ESTE CASO 6 ); $f++){
echo "<tr>"; //*** esto te generará 6 filas con 3 columnas o sea 18 celdas, para 18 fotos.
for ($c=1; $c<=3; $c++){
$i++;
echo "<td align='center' valign='top' class='small'><a href='#'-->
-->onClick=\"foto('$foto/$imagen[$i]','Penya');return false\"><img src='$thumb/$imagen[$i]'-->
-->"name='image' width='94' height='70' border='1'></a></td>";
} // fin for
} // fin for

if ($VARIABLE DEL RESTO DE LA DIVISION( en este caso 2) > 0)
{
echo"<tr>"; // genero nueva fila si resto superior a 0
for $x=1; $x<=$VARIABLE DEL RESTO DE LA DIVISON( en este caso 2); $x++)
{
$i++;
echo "<td align='center' valign='top' class='small'><a href='#'-->
-->onClick=\"foto('$foto/$imagen[$i]','Penya');return false\"><img src='$thumb/$imagen[$i]'-->
-- >"name='image' width='94' height='70' border='1'></a></td>";
}// fin for.
echo "</tr>"; //PARA QUE CIERRE LA UNICA FILA,YA QUE NO PODRAN HABER NUNCA UN RESTO SUPERIOR A 3. POR LO QUE NECESITAS SOLO UNA NUEVA FILA.
} // FIN SI.
else // sino no hay resto...
{echo "</tr>"; }// PARA QUE CIERRE LA FILA SI ES QUE NO TIENE RESTO LA DIVISION
__________________
Solo sé que nada sé.
  #3 (permalink)  
Antiguo 07/05/2004, 11:30
 
Fecha de Ingreso: octubre-2003
Mensajes: 46
Antigüedad: 20 años, 6 meses
Puntos: 0
Muchas gracias DaMMsteiN, muy bueno. Ahora no puedo probarlo, luego lo pruebo y te digo si supe hacerlo.
  #4 (permalink)  
Antiguo 07/05/2004, 15:51
 
Fecha de Ingreso: octubre-2003
Mensajes: 46
Antigüedad: 20 años, 6 meses
Puntos: 0
De acuerdo

Perfecto, ya funciona, muchas gracias por la ayuda tiu.

Así queda el código:
Código PHP:
<?php
$thumb
="grafics/galeria/".$carpeta."/thumb";
$foto="grafics/galeria/".$carpeta."";

//contar el numero de fotos, dividiu per les columes (3). i guardar la resta
$n_fotos 0;
$direc dir($thumb); 
while (
$file $direc->read()) 

if (
eregi("\.jpg",$file)) 

$n_fotos++; 


$direc->close(); 

$n_col 3;
$files $n_fotos $n_col;
$resta $n_fotos $n_col;
$n_files floor($files);
$n_resta floor($resta);
//----------------------------------
//Ficar el nom de les imatges amb un Array
$dire dir($thumb);
$dire->read();
$dire->read();
while(
$llegir $dire->read()){
if (
eregi("\.jpg",$llegir)){
    
$a++;
    
$imagen[$a] = $llegir;
}
}
$dire->close();
//Mostrem el resultat amb una <table> de X files per X columnes
?>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td height="100" align="center">&nbsp;</td>
    <td height="100" align="center"><img src="grafics/imatges/galeria.jpg" width="200" height="70"></td>
    <td height="100" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td height="20" align="center" class="small">Fotos/<? echo "$carpeta"/*nom de la carpeta*/ ?></td>
    <td height="20" align="center">&nbsp;</td>
    <td height="20" align="center">&nbsp;</td>
  </tr>
<?
for ($f=1$f<=$n_files$f++){
    echo 
"<tr>";
    for (
$c=1$c<=3$c++){
        
$i++;
        echo 
"<td align='center' valign='top' class='small'><a href='#'-->
 onClick=\"foto('$foto/$imagen[$i]','Penya');return false\">-->
<img src='$thumb/$imagen[$i]'\"name='image' width='94'-->
height='70' border='1'></a></td>"
;
    }
    }
if (
$n_resta 0)
{
    echo 
"<tr>";
for (
$x=1$x<=$n_resta$x++)
{
$i++;
echo 
"<td align='center' valign='top' class='small'><a href='#'-->
 onClick=\"foto('$foto/$imagen[$i]','Penya');return false\">-->
<img src='$thumb/$imagen[$i]'\"name='image' width='94'-->
height='70' border='1'></a></td>"
;
// las --> se tienen que quitar y ponerlo todo junto.
}
echo 
"</tr>";
}
else {echo 
"</tr>"; }
//menu desplegable 
$path="grafics/galeria/";
$dir dir($path);
$dir->read();
$dir->read();
?>
<tr>
    <td height="20" align="center" class="small">&nbsp;</td>
    <td height="20" align="center"><form action=index2.php method=get>
Selecciona una galeria:<br> <SELECT name="carpeta">
<?
while($carpeta $dir->read()){
if (
$carpeta != "." && $carpeta != ".." && !eregi("\.",$carpeta)){
    echo 
"$carpeta";
    echo 
"<OPTION VALUE=".$carpeta.">".$carpeta."</OPTION>\n";
}
}
echo 
"</SELECT>";
echo(
"<input type=submit value=Buscar>\n");
echo(
"<input type=hidden name=id value=3>\n");
echo (
"</form>\n");
$dir->close();
?>
</td>
 <td height="20" align="center">&nbsp;</td>
  </tr>
</table><br>
¿De que otra manera lo podría hacer para que solo me mostrara las carpetas? Yo lo he hecho con && !eregi("\.",$carpeta) pero si pongo alguna carpeta que contenga un punto no se me mostrara en la lista.
PD: Retoqué algunos errores del código. Si veis alguno o veis alguna otra manera de hacerlo para optimizar el código comentarlo.
Saludos.

Última edición por Yukas; 07/05/2004 a las 18:28
  #5 (permalink)  
Antiguo 07/05/2004, 16:05
 
Fecha de Ingreso: enero-2004
Ubicación: Santiago - Chile
Mensajes: 45
Antigüedad: 20 años, 3 meses
Puntos: 0
De acuerdo Ok !

Hola:

Que bueno que te funcionó, como verás era solo un juego de matemáticas.

Saludos.

DaMMsteiN
__________________
Solo sé que nada sé.
  #6 (permalink)  
Antiguo 08/05/2004, 07:31
 
Fecha de Ingreso: noviembre-2003
Mensajes: 114
Antigüedad: 20 años, 5 meses
Puntos: 0
function cmp ($a, $b) {
$tmp[0]=strtoupper($a);
$tmp[1]=strtoupper($b);
sort($tmp);
return (strcmp(strtoupper($tmp[1]) , strtoupper($b))) ? 1 : -1;
}

function getDirByLetter ($dirName) {
static $result_array=array();
$d = dir($dirName);
while($entry = $d->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($dirName."/".$entry)){
array_push($result_array,$entry);
}
}
}
$d->close();

usort($result_array, "cmp");
return $result_array;
}
//AQUI PONES EL DIRECTORIO
$AllTheDirectories = getDirByLetter("./modules/Galeria/galleries/");
$array_quotient=count($AllTheDirectories) / 2; //AQUILA CANTIDAD DE COLUMNAS
$array_quotient=ceil($array_quotient);
$Column = 0;
$FirstCol = "no";
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\"><td valign=\"top\" nowrap>";

for ($i=0;$i<count($AllTheDirectories);$i++) {
if($Column == $array_quotient) {
$Column = 0;
if ($FirstCol = "yes") {
echo "</td>";
}
echo "<td valign=\"top\" nowrap>";
$FirstCol = "yes";
}
$Column++;

echo "AQUI EL CODIGO";

EJEMPLO: = "<b><a href=ARCHIVO.PHP?dir=".rawurlencode($AllTheDirecto ries[$i])."><img border=0 src=\"modules/My_eGallery/galleries/".$AllTheDirectories[$i].".gif\" alt=\"".$AllTheDirectories[$i]."\"><br></a><a href=modules.php?name=My_eGallery&show=categories& dir=".rawurlencode($AllTheDirectories[$i])." title=\"".$AllTheDirectories[$i]."\">".str_replace(".", " ",$AllTheDirectories[$i])."</a></b><br><br>";

}
echo "</font></table></td></tr></table>";
}









LO COMENTE A VELOCIDAD LUZ Y EN EL CUANDRO DE RESPUESTA RAPIDA TIENE UN MONTON DE CODIGO PERO FUNCIONA, ESO ESPERO A MI ME FUNCIONA.
__________________
Soporte y Creaciones PHP-Nuke:
NukeProjects.Net

if($Necesitas=="Ayuda"){
echo "No dudes en pedirla";
}
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 10:07.