-1
<?php
if(ISSET($_POST['fecha'])){
    $query = mysqli_query($conn, "SELECT * FROM `fotos` ORDER BY `fecha` DESC") or die(mysqli_error());
    //$query = $conn->query("SELECT * FROM `fotos` order by fecha desc") or die(mysqli_errno()); (ORDENA POR FECHA)
    while($fetch = mysqli_fetch_array($query)){
      echo "<tr>
            <td>".$fetch['fecha']."</td>
            <td>".$fetch['nombre']."</td>
            <td>".$fetch['codigo']."</td>
            <td>".$fetch['correo']."</td>
            <td>".$fetch['img']."</td>
            <td>."<center><img src="<?php echo "upload/".$fetch['img']?>" width="165" height="165">."</center></td>
            <td><center><img src="<?php echo "upload/".$fetch['img']?>" width="165" height="165"></center></td>
        </tr>";

I want to print the image inside the php, but I don't know how to concatenate the route.

I want to concatenate the route with the image, to be able to visualize it.

while($fetch = mysqli_fetch_array($query)){
    echo "<tr>
        <td>".$fetch['fecha']."</td>
        <td>".$fetch['nombre']."</td>
        <td>".$fetch['codigo']."</td>
        <td>".$fetch['correo']."</td>
        <td>".$fetch['img']."</td>
    </tr>";
}

But I want to use this structure:

<td>
    <center>
        <img src="<?php echo "upload/".$fetch['img']?>" width="165" height="165">
    </center>
</td>
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
ANGEL JC
  • 3
  • 3

2 Answers2

1

Like @the_nuts mentioned, you're already echoing a value, so you don't need to include <?php tags inside of the string you're echoing.

This SO question explains a few different ways to output a string: What is the difference between single-quoted and double-quoted strings in PHP?

In the below code, I'm using "Complex (curly) syntax". See https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing for more information.

echo "<tr>
<td>{$fetch['fecha']}</td>
<td>{$fetch['nombre']}</td>
<td>{$fetch['codigo']}</td>
<td>{$fetch['correo']}</td>
<td>{$fetch['img']}</td>

<td><center><img src='upload/{$fetch['img']}' width='165' height='165'></center></td>
<td><center><img src='upload/{$fetch['img']}' width='165' height='165'></center></td>
</tr>";

Here's an example using simple string concatenation instead, with the exact same result:

echo "<tr>
<td>".$fetch['fecha']."</td>
<td>".$fetch['nombre']."</td>
<td>".$fetch['codigo']."</td>
<td>".$fetch['correo']."</td>
<td>".$fetch['img']."</td>

<td><center><img src='upload/".$fetch['img']."' width='165' height='165'></center></td>
<td><center><img src='upload/".$fetch['img']."' width='165' height='165'></center></td>
</tr>";
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
  • One other thing I changed without mentioning it was `width="165"` (double quotes) to `width='165'` (single quotes). The reason I did this is because I think it looks cleaner to have single quotes inside of double quotes (and both are valid HTML), rather than escaping the nested double quotes - though both are 100% valid in PHP. So I could've had `..."' width=\"165\" ` to escape the double quotes (https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double) – WOUNDEDStevenJones Nov 06 '19 at 21:37
0

What is the content of $fetch['img'] ? it's the image name ? if that's the case your code should work, just check if you're also keeping the extension of the file, else just concatenate it.