简体   繁体   English

图像标签位于PHP eco内的锚标签内

[英]Image tag inside anchor tag inside PHP eco

The following is my php code 以下是我的PHP代码

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. $ new'作为文字,而不是从$ new变量中获取值。

What can I do to avoid this? 我该怎么做才能避免这种情况?

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>';

试试这条线

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM