Change image link dynamically within <img src>

Asked

Viewed 19 times

0

Good morning, friends, beginner question.

I have a query that shows in a table all the records of my comic. But since there are so many columns, I limited the display to names, birth date to a few data. And I created a last column with the link "complete data" that when clicked will show in another window all the data of that record. This is the code:

<a href="dadoscompletos.php?codigo=">Dados Completos

Thus, the page opens with the complete data of that record.

My problem is with the image. I want to display the photo of what is saved in the "img" folder. The name of the image is the code of the record. I tried so:

<?php

include("classe/conecta.php");
$codigo = $_GET['codigo'];
$consulta = "SELECT * FROM CadPessoas WHERE Cod='$codigo'";
$con = $mysqli->query($consulta) or die($mysqli->error);
?>

<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    
  <body>
           <?php 
           while($dado = $con->fetch_array()){ ?>
          
          <img src="img/(['$codigo']).jpg">

But it didn’t work. Can anyone help me with that last line of my code? Thank you

1 answer

0

<?php
   include("classe/conecta.php");
   $codigo = $_GET['codigo'];
   $consulta = "SELECT * FROM CadPessoas WHERE Cod='$codigo'";
   $con = $mysqli->query($consulta) or die($mysqli->error);
   ?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   </head>
   <body>
      <?php 
         while($dado = $con->fetch_array())
         {
            ?>
            <img src="img/<?=$dado['codigo'];?>.jpg"> //short hand do php
            <?
         }?>        

Browser other questions tagged

You are not signed in. Login or sign up in order to post.