Im having a problem with my PHP search script. I'm building a social networking site where one can search for a user, an event or a club. I would like the search results to display a user avatar thumb or a default thumb if none has been uploaded.
Im testing on localhost.
User avatar's are stored in the User_Images (c:\wamp\www\NNL\User_Images)
folder while the default avatar is stored in c:\wamp\www\NNL\Style\Images\default_avatar.png.
The following is my PHP code:
<?php
while ($row_user = mysql_fetch_assoc($user))
{
echo "\n<table width='500' border='0'>";
echo "\n\t<tr>";
echo "<td width='50' height='50' align='center' valign='middle'><a href='user_view.php?user_id=".$row_user['user_id']."'>
<img src=User_Images/$row_user[picture_thumb_url] !=''? $row_user[picture_thumb_url]: '../Style/Images/default_avatar.png'
border='0' height='50' width='50'/></a></td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_user['user_first_name']. "</td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_user['user_last_name']. "</td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_user['username']. "</td>";
echo "<td width='30'><a class='text_12_link_green' href='user_view.php?user_id=".$row_user['user_id']."'>View</a></td>";
echo "\n\t</tr>";
echo "\n</table>";
}
?>
<?php
while ($row_event = mysql_fetch_assoc($event))
{
echo "\n<table width='500' border='0'>";
echo "\n\t<tr>";
echo "<td width='50' height='50' align='center' valign='middle'><a href='#table_index.php'>
<img src='Images/$row_event[event_thumb_url]' border='0' height='50' width='50'/></a></td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_event['event_name']. "</td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_event['event_venue']. "</td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_event['event_date']. "</td>";
echo "<td width='30'><a class='text_12_link_green' href='#user_view.php?user_id=".$row_user['username']."'>View</a></td>";
echo "\n\t</tr>";
echo "\n</table>";
}
?>
<?php
while ($row_establishment = mysql_fetch_assoc($establishment))
{
echo "\n<table width='500' border='0'>";
echo "\n\t<tr>";
echo "<td width='50' height='50' align='center' valign='middle'><a href='#table_index.php'>
<img src='Establishment_Images/$row_establishment[establishment_thumb_url]' border='0' height='50' width='50'/></a></td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_establishment['establishment_name']. "</td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_establishment['location_name']. "</td>";
echo "<td width='140' class='ordinary_text_12'>" .$row_establishment['establishment_pricing']. "</td>";
echo "<td width='30'><a class='text_12_link_green' href='#user_view.php?user_id=".$row_user['username']."'>View</a>开发者_运维知识库</td>";
echo "\n\t</tr>";
echo "\n</table>";
}
?>
The problem is in the $row_user
while loop where im trying to echo $row_user
thumb. Right now, if a user has an avatar, it displays the image, however it returns no image at all if a user has no avatar. Where am I going wrong?
you can do this although it's not exactly readable.
echo "<td width='50' height='50' align='center' valign='middle'><a href='user_view.php?user_id=".$row_user['user_id']."'>
<img src=User_Images/" . ( $row_user['picture_thumb_url'] != '' ? $row_user['picture_thumb_url'] : '../Style/Images/default_avatar.png' ) . " border='0' height='50' width='50'/></a></td>";
You're best of doing the conditional beforehand then echo the variable:
Updated:
$thumbnail = $row_user['picture_thumb_url'] != '' ? $row_user['picture_thumb_url'] : '../Style/Images/default_avatar.png';
echo "<td width='50' height='50' align='center' valign='middle'><a href='user_view.php?user_id=".$row_user['user_id']."'>
<img src=User_Images/$thumbnail border='0' height='50' width='50'/></a></td>";
You cannot put an 'if' statement into your 'echo'.
Go this way:
if($row_user[picture_thumb_url] !='')
$thumb = $row_user[picture_thumb_url];
else
$thumb = '../Style/Images/default_avatar.png';
And then:
echo "<img src=\"$thumb\">";
精彩评论