我需要查看图像,但是我的代码不知道图像是png还是jpg我需要你的帮助
<?php
// Get all members
$members = "";
$results = $db_handle->runQuery("SELECT id,fullname FROM trainees");
if(!empty($results)) {
foreach ($results as $key => $row) {
$members.= '
<li>
<img src="images/user-images/'.$row["id"].'.*'.'" alt="User Image">
<a class="users-list-name" href="#">'.$row["fullname"].'</a>
<span class="users-list-date">'.$row["sub_from"].'</span>
</li>';
}
}
?>
重写代码。如果您的用户存储了一个图像(用户图像),那么该文件的名称应该与其他信息一起存储在数据库中。不是图像文件本身,只是需要为特定用户获取的文件名。
如果这不是一个选项,你总是知道它是jpg或png,然后你可以做一个检查,使用file_exists() -像这样:
<img src="images/user-images/'.$row['id'].'.(file_exists($row['id'].'.png' ? '.png' : '.jpg').'" alt="User Image">
这假设了很多事情—首先,文件名只是用户id,后面是扩展名;其次,文件总是.png或.jpg文件;第三,文件总是存在(因为该三元运算符中的第二个子句将显示.png是否不存在,而不管文件是否存在)。
非常感谢您的帮助,但这段代码帮助了我
<?php
// Get all members
$members = "";
$results = $db_handle->runQuery("SELECT id,fullname,sub_from FROM trainees LIMIT 8");
if(!empty($results)) {
foreach ($results as $key => $row) {
if (file_exists('images/user-images/'.$row["id"].'.jpg')) {
$img = '<img src="images/user-images/'.$row["id"].'.jpg'.'" alt="User Image">';
} else if (file_exists('images/user-images/'.$row["id"].'.jpg')) {
$img = '<img src="images/user-images/'.$row["id"].'.png'.'" alt="User Image">';
}else
$img = '<img src="images/user-images/avatar.png" alt="User Image">';
$members.= '
<li>'.$img.'
<a class="users-list-name" href="#">'.$row["fullname"].'</a>
<span class="users-list-date">'.$row["sub_from"].'</span>
</li>';
}
}
?>