在PHP eco里面的锚标记里面的图像标签

时间:2016-09-29 22:31:26

标签: php html mysql sql href

以下是我的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>';

我想要&#39;链接&#39;带我到图片文件&#39;我上传了。

但是因为我把锚标记放在了回声中,所以它假设了#39; 美元的新&#39;作为字面值而不是从$ new变量中获取值。

我该怎么做才能避免这种情况?

2 个答案:

答案 0 :(得分:2)

当我遇到这种情况时,我会把事情分解为像这样的可管理的部分。

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

您不需要此代码上的端口号或完整的域名,如果您使用它,当您移动到真实的实时服务器或从一个域移动到另一个域时,您将不得不修改所有这些代码,当然,你喜欢我们其他人会忘记这样做。

所以试试这个

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

答案 1 :(得分:0)

试试这一行

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