2011-01-25 78 views
0

我的PHP搜索腳本存在問題。我正在建立一個社交網站,可以搜索用戶,活動或俱樂部。我希望搜索結果顯示用戶頭像縮略圖或默認縮略圖,如果沒有上傳。我在本地主機上進行測試。 頭像的存儲在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拇指。現在,如果用戶有頭像,它會顯示圖像,但是如果用戶沒有頭像,它將根本沒有圖像。我哪裏錯了?

+0

你在* *期待它返回,如果用戶沒有分身? – meagar 2011-01-25 14:30:50

+0

我希望它返回我已經存儲在一個文件夾中的默認圖像。該圖像被稱爲default_avatar.png。見頂部段落。我已經在用戶個人資料頁面中使用了類似的代碼,但無法在這一個 – 2011-01-25 14:36:52

回答

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>"; 
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\">"; 
+0

感謝喬納森。讓我試試這個。 – 2011-01-25 14:39:37