2011-06-03 74 views
2

我最近开始学习Javascript,但仍然没有在这个问题上的专家。
所以我想知道是否有人云帮助我。如何在php无法从服务器提取图像时显示图像?

我使用MySQL数据库和PHP

我的数据库表构建投资组合的网站有3个冒号:name, small_image, description

我已经设置了我的PHP,以便它提取namesmall_image

$name  = htmlentities($row['name']); 
$image_small = "images/" . $row['image_small']; 

,并将它显示是这样的:

$name 
< img src='$image_small' with='$width' height='$height' /> 

我唯一的问题是,当我进入管理页面,添加了新的工作,在网站上没有图像出现一个空的空间。

我想要做的是有一个图像,可以取代失踪的图像?

有没有办法让它工作?或者更好,简单的方法来做到这一点?

我真的很感激它。

这里不工作是完整的代码。

< - - - - - - - - - - - - - - - - - - - - 全码 - - - - - - - - - - - - - - - - >

// 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>"; 
+0

你把blank.jpg图像放在图像文件夹? – 2011-06-03 05:28:43

回答

0

试着改变你的第一个代码块上面的代码是这样的:

$name   = htmlentities($row['name']); 
$image_small = ($row['image_small'] != '') ? "images/" . $row['image_small'] : '/path/to/default.png'; 

它所做的是检查是否$row['image_small']有一个值,如果是用它来$image_small,否则设置$image_small为默认的路径小图像。

1

如果数据库中不存在图像,则应显示空白图像(默认图像)。 例如

$name = htmlentities($row['name']); 
if(isSet($row['image_small'])){ 
     $image_small = "images/" . $row['image_small']; 
} 
else{ 
    $image_small = "images/blank.jpg"; 
} 

注意:您应该将blank.jpg放在图像文件夹中。

我建议你在图像上传到系统文件夹时将图像名称保存在数据库中。 或者你可以在文件夹中查看您的图像文件,如下所示:

<?php 
$filename = "images/" . $row['image_small']; 

if (!file_exists($filename)) { 
    $image_small = "images/blank.jpg"; 
} 
?> 

享受!!!!!!!

+0

感谢您的帮助,但仍未显示。 – contesta 2011-06-03 05:19:18

+0

你应该把blank.jpg放在图像文件夹中,然后它就可以工作。 – 2011-06-03 05:30:37

0

除了其他建议,您可以使用MySQL中的ISNULL()函数返回默认图像名称。例如,创建你的图像目录为Default.png图像,并在查询中使用这样的:

SELECT 
    name, 
    IF(ISNULL(small_image),'default.png',small_image) AS "small_image", 
    description 
FROM 
    images; 

不是说这一定是一个更好的答案,只是一个替代品。

0

客户端解决方案是使用img标签的“onerror”属性。 这就像︰

<img src='$image_small' with='$width' height='$height' onerror='this.src = "/images/default.jpg" ;' />