concatenation with php

0

The problem is the following attempt to concatenate within an echo the code is the following

while ($columna = mysqli_fetch_array( $resultado ))
 $dir ="assets/img/";
   {
    echo "<article class='124'>";
    echo "<a href="">";
    echo "<img src="$dir.$columna['foto']." alt="">";
    echo "</a> ";                                  
    echo "</article> ";

}
    
asked by Cesar 13.02.2018 в 20:43
source

3 answers

1

You must learn to escape the quotes and include array elements or objects in the strings, for example:

// Escapar comillas
echo "<img src=\"nombre_de_imagen\">";

// Agregar elementos de array u objetos entre llaves
echo "<img src=\"{$array['indice']}\">";

Then, your example would look like this:

$dir ="assets/img/";
while ($columna = mysqli_fetch_array( $resultado )) {
    echo "<article class=\"124\">";
    echo "<a href=\"\">";
    echo "<img src=\"$dir{$columna['foto']}\" alt=\"\">";
    echo "</a>";
    echo "</article> ";

}

I find it more convenient to use the HEREDOC structure, where it is not necessary to escape quotes, but to enclose array elements or objects between braces:

$dir ="assets/img/";
while ($columna = mysqli_fetch_array( $resultado )) {
    echo <<<EOT  // Tres "menor que" seguidos de un identificador
        <article class="124">
            <a href="">
                <img src="$dir{$columna['foto']}" alt="">
            </a>
        </article>
EOT; // Esta instrucción debe quedar en la primera columna
// Y es el mismo identificador, seguido de un punto y coma
}

Choose the form that best suits you to code, but be sure to escape the quotes when necessary and enclose things like $ row ['column'] or $ db- & gt ; row ('column') .

    
answered by 14.02.2018 / 05:07
source
0

Line $dir = ... must go before line while(... .

I suggest you review the function documentation while ; Look especially at the code in example 1.

    
answered by 13.02.2018 в 20:49
0

Try it like this so you do not do so many echos

  <?php
    $dir ="assets/img/"; 
    while ($columna = mysqli_fetch_array( $resultado )){ ?>
        <article class='124'><a href=""><img src="<?php echo $dir.$columna['foto']; ?>" alt=""></a></article>
    <?php
    }
    ?>
    
answered by 13.02.2018 в 20:52