当php没有可从服务器提取的图像时,如何显示图像?
我最近开始学习Javascript,但在这方面还不是一位专家。当php没有可从服务器提取的图像时,如何显示图像?,php,javascript,mysql,image-replacement,Php,Javascript,Mysql,Image Replacement,我最近开始学习Javascript,但在这方面还不是一位专家。 所以我想知道是否有人能帮我 我正在使用MySql数据库和PHP构建一个投资组合网站 我的数据库表有3个冒号:名称、小图片、说明 我已经设置了PHP,以便它提取名称和小图片 $name = htmlentities( $row['name'] ); $image_small = "images/" . $row['image_small']; 它的回声是这样的: $name < img src='$image
所以我想知道是否有人能帮我 我正在使用MySql数据库和PHP构建一个投资组合网站 我的数据库表有3个冒号:
名称、小图片、说明
我已经设置了PHP,以便它提取名称
和小图片
$name = htmlentities( $row['name'] );
$image_small = "images/" . $row['image_small'];
它的回声是这样的:
$name
< img src='$image_small' with='$width' height='$height' />
$name = htmlentities( $row['name'] );
$image_small = ($row['image_small'] != '') ? "images/" . $row['image_small'] : '/path/to/default.png';
$name
我唯一的问题是,当我进入管理页面,在网站上添加一个没有图片的新作品时,它看起来是一个空白
我想做的是有一个图像,可以取代丢失的图像
有没有办法让它发挥作用?还是一种更好、更简单的方法
我真的很感激
这里不工作是完整的代码
<--------完整代码--->
// Loop through all of the records returned from the query
while( $row = mysql_fetch_array( $results ) ) {
// collect data from each field
$id = $row['id'];
$name = htmlentities( $row['name'] );
$desc_short = htmlentities( $row['desc_short'] );
if(isset( $row['image_small'])){
$image_small = "images/jewelry/small/" . $row['image_small'];
}
else{
$image_small = "images/blank.jpg";
}
echo "
<li class='column_$x $last_row_class'>
<a href='details.php?id=$id'>
<img src='$image_small' with='$width' height='$height' />
</a>
<p class='product_name'><a href='details.php?id=$id'>$name</a></p>
<p class='product_desc'>$desc_short</p>
$rating_div
</li>";
//循环查询返回的所有记录
while($row=mysql\u fetch\u数组($results)){
//从每个字段收集数据
$id=$row['id'];
$name=htmlentities($row['name']);
$desc_short=htmlentities($row['desc_short']);
如果(isset($row['image\u small'])){
$image_small=“images/jewelry/small/”$row['image_small'];
}
否则{
$image\u small=“images/blank.jpg”;
}
回声“
$desc\u short
$rating_div
”;
尝试将上面第一个代码块中的代码更改为如下内容:
$name
< img src='$image_small' with='$width' height='$height' />
$name = htmlentities( $row['name'] );
$image_small = ($row['image_small'] != '') ? "images/" . $row['image_small'] : '/path/to/default.png';
它所做的是检查
$row['image\u small']
是否有值,如果有,则将其用于$image\u small
,否则将$image\u small
设置为默认小图像的路径。如果数据库中不存在图像,则应显示空白图像(默认图像)。
e、 g
注意:您应该将blank.jpg放在您的图像文件夹中
我建议您在系统文件夹中上载图像时,将图像名称保存在数据库中。
或者,您可以检查文件夹中的图像文件,如下所示:
<?php
$filename = "images/" . $row['image_small'];
if (!file_exists($filename)) {
$image_small = "images/blank.jpg";
}
?>
享受!!!!!!!除其他建议外,您还可以使用MySQL中的ISNULL()函数返回默认图像名称。例如,在图像目录中创建一个default.png图像,并在查询中使用:
SELECT
name,
IF(ISNULL(small_image),'default.png',small_image) AS "small_image",
description
FROM
images;
不是说这一定是一个更好的答案,只是一个替代方案。客户端解决方案是使用img标记的“onerror”属性。 这是这样的:
<img src='$image_small' with='$width' height='$height' onerror='this.src = "/images/default.jpg" ;' />
您是否将blank.jpg图像放入图像文件夹?