在PHP中设置默认图像

时间:2011-01-22 09:52:32

标签: php mysql

我正在构建一个社交网站,其中一个功能是更新状态,即只能将文本更新为状态,或者可以更新文本和图像。如下:

头像pic - 更新文本 - 更新图像(如果图像)

头像pic - 更新文本 - 默认图像(如果未上传更新图像)

默认头像pic - 更新文本 - 默认图像(如果头像和更新图像未上传)

我让默认头像工作正常,但问题是如果没有包含更新图像,则显示默认更新图像。

我该怎么做或有没有人有不同的解决方案?

以下是我的代码:

PHP

<?php
$user_id = $_SESSION['UserSession'];
//query user avatar
$query_avatar_thumb = "SELECT picture_thumb_url FROM picture WHERE user_id='$user_id' AND avatar='1' LIMIT 1";
$avatar_thumb_result = mysql_query($query_avatar_thumb) or die(mysql_error());  
$avatar_thumb = mysql_fetch_assoc($avatar_thumb_result);
$totalRows_avatar_thumb = mysql_num_rows($avatar_thumb_result);

//query username
$query_user_info = "SELECT username FROM users WHERE user_id='$user_id'";
$user_info = mysql_query($query_user_info, $connections) or die(mysql_error());
$row_user_info = mysql_fetch_assoc($user_info);

//query update image
$query_wid_updates = "SELECT update_text, picture_thumb_url FROM wid_updates LEFT JOIN picture ON wid_updates.attached_picture_id = picture.picture_id 
WHERE wid_updates.user_id = '$user_id' ORDER BY wid_updates.update_id DESC";
$wid_updates = mysql_query($query_wid_updates, $connections) or die(mysql_error());
$row_wid_updates = mysql_fetch_assoc($wid_updates);
$totalRows_wid_updates = mysql_num_rows($wid_updates);
?>

我的HTML显示结果:

<div id="previous_updates">   
<?php do { ?>                                      
<table width="460" border="0">                     
  <tr>
    <td height="50" width="50">
    <?php 
    if ($totalRows_avatar_thumb==1)
    {
        echo "<a href='index.php?#main_profile_div'><img src='User_Images/$avatar_thumb[picture_thumb_url]' border='0' height='50' width='50'/></a>";
    }
    else
    {
        echo "<a href='index.php?#main_profile_div'><img src='/NNL/Style/Images/default_avatar.png' border='0' height='50' width='50'/></a>";
    }
    ?>
    </td>
    <td width="360" colspan="3" class="overide" id="update_box"><div><?php echo $row_wid_updates['update_text']; ?></div></td>
    <td width="50">
    <?php 
    if ($totalRows_wid_updates)
    {
        echo "<a href='#pictures.php'><img src='User_Images/$row_wid_updates[picture_thumb_url]' border='0' height='50' width='50'/></a>";
    }
    else
    {
        echo "<a href='#pictures.php'><img src='/NNL/Style/Images/no_update_image.png' border='0' height='50' width='50'/></a>";
    }
    ?>
    </td>
  </tr>                      
  <tr>
    <td width="50" height="30"></td>
    <td width="120"></td>
    <td width="120" class="ordinary_text_12_green">Comment</td>
    <td width="120"></td>
    <td width="50"></td>
  </tr>
</table> 
<?php } while ($row_wid_updates = mysql_fetch_assoc($wid_updates)); ?>                                                                    
</div> 

我的wid_updates表:

update_id   update_text     attached_picture_id     user_id     timestamp
48             b                   NULL                92   2011-01-13 13:22:23
51             c                   61                  92   2011-01-13 18:30:06
52          d                   NULL                92  2011-01-13 18:30:14
53          PPP                 67                  95  2011-01-17 10:28:01
54          PPP                 NULL                95  2011-01-17 10:28:36
55          oo                  68                  95  2011-01-17 10:30:42
56          ll                  69                  95  2011-01-17 10:35:16
57          tt                  70                  95  2011-01-17 10:35:34
58          monday              71                  92  2011-01-17 10:52:59

截至目前,默认图片为空白且我明白为什么,我的if ($totalRows_wid_updates)声明是错误的。我如何解决它。顺便说一句,这是在profile.php中,只显示用户更新,index.php将包含用户及其朋友的更新。

1 个答案:

答案 0 :(得分:1)

<img src="User_Images/<?php echo ($avatar_thumb[picture_thumb_url] != ''? $avatar_thumb[picture_thumb_url]: 'file_name_of_some_default_image.png'); ?>" border='0' height='50' width='50'/>