I've recently started learning Javascript but still not a bit expert on the subject.
So I was wondering if anybody cloud help me out.I'm building a portfolio site using MySql databases and PHP
My database table has 3 colons: name, small_image, description
.
I've set up my PHP so that it extracts the name
and the small_image
.
$name = htmlentities( $row['name'] );
$image_small = "images/" . $row['image_small'];
and it echo like this:
$name
< img src='$image_small' with='$width' height='$height' />
my only problem is that when I go to the admin page and add a new work with no image on the site it appears an empty space.
what I would like to do is to have an image that can be replacing the missing image?
is there a way to make it work? or a better, easy way to do so?
I really appreciate it .
Is not working here is the full code.
< - - - - - - - - - - - - - - - - - - - - FULL CODE - - - - - - - - - - - - - - - - >
// 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>";
you should display blank image(default image) if there is not exists image from database. e.g.
$name = htmlentities( $row['name'] );
if(isSet($row['image_small'])){
$image_small = "images/" . $row['image_small'];
}
else{
$image_small = "images/blank.jpg";
}
Note: You should put blank.jpg in your image folder.
I suggest you to save image name in database when image uploaded in system folder. Or you can check your image file in folder as follows :
<?php
$filename = "images/" . $row['image_small'];
if (!file_exists($filename)) {
$image_small = "images/blank.jpg";
}
?>
Enjoy!!!!!!!
Try changing the code in your first code block above to something like this:
$name = htmlentities( $row['name'] );
$image_small = ($row['image_small'] != '') ? "images/" . $row['image_small'] : '/path/to/default.png';
What it does is check if $row['image_small']
has a value and if so use it for $image_small
, otherwise set $image_small
to the path of a default small image.
Alternatively to the other suggestions, you can use the ISNULL() function in MySQL to return the default image name. For example, create a default.png image in your images directory, and use this in your query:
SELECT
name,
IF(ISNULL(small_image),'default.png',small_image) AS "small_image",
description
FROM
images;
Not saying it's necessarily a better answer, just an alternative.
A client side solution is to use "onerror" attribute of img tag. This goes like :
<img src='$image_small' with='$width' height='$height' onerror='this.src = "/images/default.jpg" ;' />
精彩评论