hal9000
hal9000

Reputation: 221

Image tag inside anchor tag inside PHP eco

The following is my php code

while ($row = mysqli_fetch_array($return_data)) {

        $id = "ID:".$row['demo_id']."<br>";
        $name = "Name: ".$row['demo_name']."<br>";
        $version = "Version: ".$row['demo_version']."<br>";
        $details = "Details: ".$row['demo_details']."<br>";
        $file = "File Link: ".$row['file']."<br>";
        $new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.
        '<img src = \"../demo_webpages_project/images/$new"/>';
        echo '<a href = "http://'.$_SERVER["SERVER_NAME"].':8080/demo_webpages_project/images/".$new> Link </a>';

I want the 'link' to take me to the image 'file' that I uploaded.

But since I put the anchor tag inside the the echo, it assumes '. $new' as literal instead of taking value from the $new variable.

What can I do to avoid this?

Upvotes: 0

Views: 1178

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94652

When I get into this sort of situation I just break things down into managable pieces like this.

while ($row = mysqli_fetch_array($return_data)) {

        $id = "ID:".$row['demo_id']."<br>";
        $name = "Name: ".$row['demo_name']."<br>";
        $version = "Version: ".$row['demo_version']."<br>";
        $details = "Details: ".$row['demo_details']."<br>";
        $file = "File Link: ".$row['file']."<br>";
        $new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.

        echo "<a href='http://{$_SERVER['SERVER_NAME']}:8080/demo_webpages_project/images/$new'>";
        echo "<img src='../demo_webpages_project/images/$new'/>";
        echo '</a>';

You should not need the port number on this code, or the full domain name, if you do use it, you would have to amend all this code when you move to a real live server, or from one domain to another, which of course you like the rest of us would forget to do.

So try this instead

        echo "<a href='demo_webpages_project/images/$new'>";
        echo "<img src='../demo_webpages_project/images/$new'/>";
        echo '</a>';

Upvotes: 2

Erdem KAYA
Erdem KAYA

Reputation: 41

Try this line

        echo '<a href = "http://'.$_SERVER["SERVER_NAME"].':8080/demo_webpages_project/images/'.$new.'"> Link </a>';

Upvotes: 0

Related Questions