Reputation: 75
example if user have no photo in our database then row show default photo how can i do this please help me thanks
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
echo "<table class='hovertable' border='1' cellpadding='0' cellspacing='0'>";
echo "<tr><th>Photo</th><th>Teacher Detail</th></tr>";
if ($rows > 0) {
while($row = mysql_fetch_array($result)) {
echo "<tr><td align='center'>";
echo "<img src=images/".$row['photo'] ." width='90' height='90'></a></td>";
echo '<td valign="top">Teacher Name: ';
echo $row['name'], '<br/>';
echo 'Teacher No: ';
echo $row['teacherno'], '<br/>';
echo 'Father Name: ';
echo $row['fathername'], '<br/>';
echo '</td></tr>';
}
} else {
echo "<tr><td colspan=\"5\">No results found!</td></tr>";
}
echo "</table>"
Upvotes: 0
Views: 3387
Reputation: 892
Just check to see if $row['photo']
is not empty or contains a string. If we get a match, then the user has an image, otherwise, we can output the default image. A nice sample follows.
The Code
if (!empty($row['photo']) AND isset($row['photo'])){
// Image is not empty and there is data contained within the array value
echo "<img src='images/" . $row['photo'] . "' width='90' height='90' />";
} else {
// There is no data contained within the value, show the default image
echo "<img src='images/default_image.png' width='90' height='90' />";
}
What's going on?
using empty()
we use this to determine if a string (or an array) is empty, if it is, this will return FALSE
. isset()
checks to see if the variable is set, if it isn't, it will return FALSE
.
-- snip -- User changed question, answer changed.
Upvotes: 0
Reputation: 2129
echo "<table class='hovertable' border='1' cellpadding='0' cellspacing='0'>";
echo "<tr><th>Photo</th><th>Teacher Detail</th></tr>";
if ($rows > 0) {
while ($row = mysql_fetch_array($result)) {
echo "<tr><td align='center'>";
if (isset($row['photo']) && !empty($row['photo'])) {
echo "<img src='images/" . $row['photo'] . "' width='90' height='90'></a></td>";
} else {
echo "<img src='images/noimage.jpg' width ='90' height = '90'></a></td>";
}
echo '<td valign="top">Teacher Name: ';
echo $row['name'], '<br/>';
echo 'Teacher No: ';
echo $row['teacherno'], '<br/>';
echo 'Father Name: ';
echo $row['fathername'], '<br/>';
echo '</td></tr>';
}
} else {
echo "<tr><td colspan = \"5\">No results found!</td></tr>";
}
echo "</table>";
Upvotes: 1
Reputation: 1183
This does not appear to be a complete section of the code at hand.
However, I would recommend removing the die(mysql_error())
if your query is intact, and then after the two echo statements try something like:
} else {
echo "<img src='images/default_image.png' width='90' height='90'>";
}
Also of note is you have a terminating </a>
without an opening <a>
tag. This is not valid HTML.
Upvotes: 0
Reputation: 128
Try doing this:
echo "<img src=images/". $row['photo'] === NULL ? "default_img" : $row['photo'] .
"width='90' height='90'></a></td>";
Upvotes: 1
Reputation: 10834
if ($rows > 0){
echo 'the user photo';
else{
echo 'default photo';
}
Upvotes: 0
Reputation: 625
<?php
if(isset($row['photo'] && $row['photo'])
{
echo "<tr><td align='center'>";
echo "<img src=images/".$row['photo'] ." width='90' height='90'></a></td>";
}
else
{
echo "<tr><td align='center'>";
echo "<img src=images/Sample.jpg" width='90' height='90'></a></td>";
}
?>
Upvotes: 2