PHP href with conditional

0

I need to create a code for image gallery with a conditional href link. I've tried it, but since it's a php file everything is entangled.

//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;

The error is:

  

Parse error: syntax error, unexpected 'if' (T_IF), expecting ',' or ';'
  in C: ... php on line 26

Line 26 is:

echo '<a href= "'if ($miArray[$i] == $miArray[5]) { echo $miArray[5] } else if ($miArray[$i] == $miArray[$n-1]) { echo $miArray[$i-1] }'">' .
    
asked by virtual8870 11.11.2016 в 21:00
source

5 answers

1

You are wrongly executing both the if and the echo within it, you can not do a echo within another, therefore you have to close the tag of php to render the HTML as such and already inside the <a> element you can proceed to do the conditional, I leave here your corrected code:

<!DOCTYPE HTML>
<html lang="">
<head>
    <meta charset="UTF-8">
    <title>galeria</title>
</head>
<body>

<?php
$pathA = "images/";

//creo un array
$ArrayA=Array();

foreach(glob($pathA . '*.jpg') as $imageA) {
    $ArrayA[]=$imageA;
}

//inicio bucle
for($n = 1; $n <= count($ArrayA); $n++):
?>

<a href="<?php if ($miArray[$i] == $miArray[5]) { echo $miArray[5]; } else if ($miArray[$i] == $miArray[$n-1]) { echo $miArray[$i-1]; } ?>">
    <img src="<?php echo $miArray[$i]; ?>">
</a>

<?php
endfor;
?>
</body>
</html>
    
answered by 11.11.2016 в 21:10
1

I would change line 26 leaving the code like this:

<!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   
$href = ($miArray[$i] == $miArray[5]) ? $miArray[5] : (($miArray[$i] == $miArray[$n-1]) ? $miArray[$i-1] : "");
echo '<a href= "'.$href.'">' .

//etiqueta img src dentro de etiqueta href para mostrar imágenes  
'<img src= "' . $miArray[$i] . '">' . '</a>' .

'</a>';


endfor;    
?>
</body>
</html>
    
answered by 12.11.2016 в 00:56
0

Change your code to the following:

<!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   
<a href= "<?php 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= "<?php $miArray[$i] ?>"></a>

</a>   
<?php
endfor;    
?>
</body>
</html>
    
answered by 11.11.2016 в 21:11
0

I would do it in the following way, avoid putting conditions and other blocks of code inside the values of an html attribute.

<?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 ciclo
    for($n = 1; $n <= count($ArrayA); $n++){

    //href dada una imagen se va a la misma
    //sino se va a la imagen anterior 

    if ($miArray[$i] == $miArray[5]) {
        $enlace = $miArray[5];
    }elseif($miArray[$i] == $miArray[$n-1]){
        $enlace = $miArray[$i-1];
    }

    ?>

    <a href="<?=$enlace?>"> 
       <img src="<?=$miArray[$i]?>">
    </a>

    <?php  
    }    
    ?>

</body>
</html>
    
answered by 11.11.2016 в 21:19
0

According to the code you added, I would put it like this:

BEFORE

//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;

NOW

//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   
    if ($miArray[$i] == $miArray[5]) {
        echo '<a href= "' . $miArray[5] . '">';
    } 
    else if ($miArray[$i] == $miArray[$n-1]) { 
        echo '<a href= "' . $miArray[$i-1] . '">';
    }
    //etiqueta img src dentro de etiqueta href para mostrar imágenes  
    echo '<img src= "' . $miArray[$i] . '"></a>';

endfor;
    
answered by 12.11.2016 в 09:40