在搜索上显示用户头像或默认头像

时间:2011-01-25 14:23:20

标签: php mysql

我的PHP搜索脚本有问题。我正在建立一个可以搜索用户,活动或俱乐部的社交网站。我希望搜索结果显示用户头像拇指或默认拇指(如果没有上传)。 我在localhost上测试。 用户头像存储在User_Images (c:\wamp\www\NNL\User_Images)文件夹中,而默认头像存储在c:\wamp\www\NNL\Style\Images\default_avatar.png.

以下是我的PHP代码:

<?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>";
}
?>

问题出在$row_user while循环中,我试图回显$row_user拇指。现在,如果用户具有化身,则显示图像,但是如果用户没有化身,则根本不返回图像。我哪里错了?

2 个答案:

答案 0 :(得分:1)

你不能在'echo'中添加'if'语句。

走这条路:

if($row_user[picture_thumb_url] !='')
   $thumb = $row_user[picture_thumb_url];
else
   $thumb = '../Style/Images/default_avatar.png';

然后:

echo "<img src=\"$thumb\">";

答案 1 :(得分:1)

你可以这样做,虽然它不完全可读。

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>";

你最好事先做条件然后回应变量:

更新:

$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>";