如何使用PHP变量回声图像



我想回声存储在数组中

public function getData(){
    $page =  $_GET['page'];
    $this->load->model('Mainpage');
    $countries = $this->Mainpage->getCountry($page);

    foreach($countries as $country){
         echo ('<img>'.$country->link.'</img>');
             echo '<p>'.$country->news.'</p>'; 
     }
    exit;
}

在这里$country->link存储图像的路径,但我无法映像请帮助。
我正在在CodeIgniter Framework中尝试此操作。

您未正确使用HTML图像标签,更改行,

echo ('<img>'.$country->link.'</img>');

echo ('<img src="'.$country->link.'">');

在评论后添加...

页面位置-127.0.0.1/viralhai
图像位置-127.0.0.1/viralhai/assests/postimage/post3.jpg
$country->link- ../assests/postimage/post1.jpg

../在路径中带您提高目录级别。
当您请求127.0.0.1/viralhai上的图像../assests/postimage/post1.jpg时,浏览器会查看127.0.0.1/assests/postimage/post3.jpg。看看viralhai是如何删除的?

在这种特定情况下,您可以前缀,然后删除返回的文件路径的前两个字符,例如

 echo ('<img src="/viralhai'.substr($country->link, 2).'">');

我在Google上花了大约5秒钟的时间,因此这可能不是最好的资源,但是这里有一些良好的知识-https://www.kirupa.com/html5/all_about_file_paths.htm。向下滚动到"绝对路径方式",这是我建议存储您的图像路径的方式。

如何更改代码:

public function getData(){
    $page =  $_GET['page'];
    $this->load->model('Mainpage');
    $countries = $this->Mainpage->getCountry($page);

    foreach($countries as $country){
         $country = '<img src="'.$country->link.'" />'); 
         return $country;
     }
    return false;
} 

告诉我这给你什么!

最新更新