我正在使用以下代码以网格方式显示数据库中的图像。
此代码工作正常并显示图像。但如果row[img]
是“pdf”文件,我想显示默认图像。哪里错了?任何指导将受到高度赞赏。
<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($connecDB,"SELECT id,name,comment,datetime,img,reply FROM table ORDER BY date DESC LIMIT $position, $item_per_page");
//output results from database
$ext=".pdf";
//output results from database
function endsWith($img, $ext){
$extLength = strlen($ext);
if(substr($img, -extLength) == $ext){
return true;
}
return false;
}
while ($row = mysqli_fetch_assoc($results))
{
//get data
echo '<article class="white-panel">';
$id = $row['id'];
$comment = $row['comment'];
$datetime = $row['datetime'];
$name = $row['name'];
$img = $row['img'];
If(endsWith($row['img'], ".pdf")){ //executes if return is true
echo '<img src="http://myownwebsite.com/image.png" /></a><p>';
}
else
{
echo '<img src="http://myownwebiste.com/upload/' . $row['img'] . '" /></a><p>';
}
echo "</article>";
?>
我现在更新的代码但没有工作。
根据您的更新,我目前的多项选择代码如下
<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($connecDB,"SELECT id,name,comment,datetime,img,reply FROM table ORDER BY date DESC LIMIT $position, $item_per_page");
//output results from database
$ext=".pdf";
//output results from database
while ($row = mysqli_fetch_assoc($results))
{
//get data
echo '<article class="white-panel">';
$id = $row['id'];
$comment = $row['comment'];
$datetime = $row['datetime'];
$name = $row['name'];
$img = $row['img'];
switch(substr($img, -$extLength))
{
case ".pdf":
echo '<img src="http://myownsite.com/upload/imagepdf.png" /></a><p>';
break;
case ".mp4":
echo '<img src="http://myownsite.com/upload/imagemp4.png" /></a><p>';
break;
default:
echo '<img src="http://myownsite.com/upload/' . $row['img'] . '" /></a><p>';
break;
}
echo "</article>";
?>
答案 0 :(得分:0)
如果匹配则返回最后4个字符串(.pdf),返回true然后执行,否则执行
$ext=".pdf";
function endsWith($img, $ext){
$extLength = strlen($ext);
if(substr($img, -$extLength) == $ext){
return true;
}
return false;
}
If(endsWith($row['img'], ".pdf")){ //executes if return is true
echo '<img src="http://example.com/image.png" /></a><p>';
}
else
{ //executes if return is false
echo '<img src="http://example.com/upload/' . $row['img'] . '" /></a><p>';
}
用于不同分机的多个图像
$extLength=4;//most of extensions are 3 character + 1 for '.'
switch(substr($img, -$extLength))
{
case ".pdf":
echo "pdf.jpg";
break;
case ".mp4":
echo "mp4.jpg";
break;
default://for jpg i.e present
echo "http://eg.com/$row[img]";
break;
}