Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: RAM47 en 11 de Noviembre 2016, 21:07
-
Necesito crear un código en php que muestre un grupo de imágenes y que aplique un vínculo condionado según la imagen:
<!DOCTYPE HTML>
<html lang="">
<head>
<meta charset="UTF-8">
<title>galeria</title>
</head>
<body>
<?php
//defino un directorio
$pathA = "images/";
//creo un array
$ArrayA=Array();
//abro función glob para incluir imágenes en array
foreach(glob($pathA . '*.jpg') as $imageA) {
$ArrayA[]=$imageA;
}
//inicio bucle
for($n = 1; $n <= count($ArrayA); $n++):
//href dada una imagen se va a la misma
//sino se va a la imagen anterior
echo '<a href= "'if ($miArray[$i] == $miArray[5]) { echo $miArray[5] } else if ($miArray[$i] == $miArray[$n-1]) { echo $miArray[$i-1] }'">' .
//etiqueta img src dentro de etiqueta href para mostrar imágenes
'<img src= "' . $miArray[$i] . '">' . '</a>' .
'</a>';
endfor;
?>
</body>
</html>
como es href dentro de php se enreda todo. El código me indica el siguiente error:
Parse error: syntax error, unexpected 'if' (T_IF), expecting ',' or ';' in C:...php on line 26
la línes 26 es
echo '<a href= "'if ($miArray[$i] == $miArray[5]) { echo $miArray[5] } else if ($miArray[$i] == $miArray[$n-1]) { echo $miArray[$i-1] }'">' .
Agredezco cualquier ayuda.
-
Madre mia que lio tienes ahi montado jjjjj
Voy a suponer que tu lógica es correcta para no ponerme a mirarla, eso de array es = a array[5]... tu sabras el motivo :)
Código PHP
<?php
//defino un directorio
$pathA = "images/";
//creo un array
$ArrayA=Array();
//abro función glob para incluir imágenes en array
foreach(glob($pathA . '*.jpg') as $imageA) {
$ArrayA[]=$imageA;
}
$imagenesHtml = '';
//inicio bucle
for($n = 1; $n <= count($ArrayA); $n++){
$imagenesHtml .= ($miArray[$i] == $miArray[5]) ? '<a href= "'.$miArray[5].'">'.'</a>' :
($miArray[$i] == $miArray[$n-1] ?
'<a href="'.$miArray[$i-1].'">'.'<img src= "' . $miArray[$i] . '">' . '</a>' : '' );
}
?>
Codigo HTML
<!DOCTYPE HTML>
<html lang="">
<head>
<meta charset="UTF-8">
<title>galeria</title>
</head>
<body>
<?= $imagenesHtml ?>
</body>
</html>
No tengo ni idea de porque usas $i como indice porque no lo has declarado antes, creo que deberias pensar hacer el curso de la web de PHP, te irá bien. Saludos
https://www.aprenderaprogramar.com/index.php?option=com_content&view=category&id=70&Itemid=193