See if there is an image and put it in a news item

1

I'm doing a very simple "news load" and when I speak of simple, believe me it's very simple ... I have a table where I load the titles, and a little picture on the left and a summary. It has more to the right a button where the user enters to read the complete news ... The model is more or less like this:

Now, the pictures that I am going to upload are in a folder whose route is "../../images/news" and the images are numbered by the id of the news. Simple up there. Now, when I load that table of the news, I look in the directory of the images if this photo corresponds to that news. I do the following:

<?php
        $nombre_fichero = "../../images/noticias/".$row['id'].".jpg";
        if (file_exists($nombre_fichero)) {
          $archivo = $row['id']."jpg";
        } else {
          $archivo = "imagen.png";
        }
      ?>

      <td><img class="ui tiny image" src="../../images/noticias/" <?php $archivo; ?>"</td>

The idea is that if you can not find the corresponding photo, you would "upload" a standard image of the type:

now well ... I think that what I'm doing would not be working because it does not load ... well, I think I'm passing a text string and not a "file" ... can it be?

    
asked by MNibor 05.07.2017 в 17:14
source

1 answer

2

I think you need to do the echo in php and include it within the src parameter:

<img class="ui tiny image" src="../../images/noticias/<?php echo $archivo; ?>" />

You can also improve your old php code a bit:

<?php
    $archivo = 'imagen.png';

    $nombre_fichero = "../../images/noticias/$row['id'].jpg";
    if (file_exists($nombre_fichero)) {
      $archivo = "$row['id'].jpg";
    }
?>

Or you can use the ternary operator:

<?php
    $nombre_fichero = "../../images/noticias/$row['id'].jpg";

    $archivo = file_exists($nombre_fichero) ? "$row['id'].jpg" : 'imagen.png';
?>
    
answered by 05.07.2017 / 17:19
source