2011-01-22 144 views
0

我正在構建一個社交網站,其中一個功能是更新狀態,用戶只能更新文本作爲狀態,或者可以更新文本和圖像。具體如下:在PHP中設置默認圖像

化身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
<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'/> 
+0

感謝您的回覆戈登。它的作品,但默認圖像需要在同一個文件夾,即。 User_Images。如果我想將默認圖像保存在另一個文件夾中,該怎麼辦? – 2011-01-22 17:03:54