Pass a String obtained from a database to the src of an image

1

I would like to know if there is any way to pass a data String obtained from a database to reference an image, here the example:

<div class="row">
        <div class="col-md-7">
          <a href="#">
            <img class="img-fluid rounded mb-3 mb-md-0" src='img/menudia/paletamango.png' alt="">
          </a>
        </div>
        <div class="col-md-5">
          <h3><?php printf("%s", $NAME); ?></h3>
          <p><?php printf("%s", $DESCR); ?></p>
          <P> $<?php printf("%d", $COSTO); ?></P>
          <P> <?php printf("%s", $IMG); ?></P>


          <a class="btn btn-primary" href="post.html">Ordenar</a>
        </div>
      </div>

I need the text String of variable "$IMG "to be as a reference for the image instead of "paletamango.png"

That is, it would be something like src='img/menudia/(Aquí iría la información de $IMG)'

    
asked by Carolina Franco 01.12.2017 в 07:25
source

2 answers

1

You can concatenate it as follows:

<?php echo "<img class='img-fluid rounded mb-3 mb-md-0' src='img/menudia/".$IMG."' alt=''>" ?>

This would be the complete line of <img>

    
answered by 01.12.2017 / 07:35
source
1

As the comrade says well, you can concatenate it, however the ideal would be to put a default image in case that when registering the article, this information is not at hand. using a if() together with a isset() you can check if said variable is defined, in case it does not, it will put the default image.

<div class="row">
    <div class="col-md-7">
      <a href="#">
        <img class="img-fluid rounded mb-3 mb-md-0" src='img/menudia/<?php if(isset($IMG)){ echo ($IMG); } else { echo("sinimagen.png"); ?>' alt="">
      </a>
    </div>
    <div class="col-md-5">
      <h3><?php printf("%s", $NAME); ?></h3>
      <p><?php printf("%s", $DESCR); ?></p>
      <P> $<?php printf("%d", $COSTO); ?></P>
      <P> <?php printf("%s", $IMG); ?></P>
      <a class="btn btn-primary" href="post.html">Ordenar</a>
    </div>
  </div>

Now we must remember that PHP reads the document from beginning to end, so you must declare the variable before executing the conditional if() , which I assume because you are not declaring it at any time. Also, I recommend that you store in the database the full name of the image file along with the extension, to avoid having to concatenate the extension. I hope it works for you:)

    
answered by 01.12.2017 в 07:42