2013-02-09 39 views
0

我在我的網站上有兩種類型的用戶,'免費'和'高級'。 基本上我有一個消息系統,列出消息和發送消息的用戶。如果賬戶類型= x轉到鏈接1否則如果賬戶類型= y需要鏈接2?

當你點擊用戶名/圖像上會鏈接到'profile.php?id=(user_id)'

什麼,我要做的,就是如果誰發來的消息=「免費」用戶的帳戶類型,然後我想利用用戶點擊時可以轉到不同的鏈接。

我真的很新的PHP,我不知道該怎麼做,請有人給我看我如何做到這一點的例子。這是我現在的代碼。

功能:

function message_account_type() { 
      global $connection; 
      global $_SESSION; 
      global $profile_id; 
      global $message_id; 
      $query = "SELECT ptb_users.account_type, ptb_messages.from_user_id 
         FROM ptb_users, ptb_messages 
         WHERE ptb_messages.from_user_id = \"$profile_id\" 
         AND ptb_profiles.user_id = ptb_messages.from_user_id "; 
      $message_account_type = mysql_query($query, $connection); 
      confirm_query($query, $connection); 
      return $message_account_type; 
     } 

PHP:

<?php 
$message_account_type = message_account_type(); 
while ($type = mysql_fetch_array($message_account_type)) 
if ($type['account_type'] == 'Premium') { 
echo "<a href=\"profile.php?id={$inbox['from_user_id']}\" ><img width=\"50px\" height=\"50px\" src=\"data/photos/{$inbox['from_user_id']}/_default.jpg\" /></a>";?></div><div class="message_text"><?php echo "<a href=\"profile.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}</a>"; ?><? } ?> 

<?php 
$message_account_type = message_account_type(); 
while ($type = mysql_fetch_array($message_account_type)) 
if ($type['account_type'] == 'Free') { 
echo "<a href=\"members.php?id={$inbox['from_user_id']}\" ><img width=\"50px\" height=\"50px\" src=\"data/photos/{$inbox['from_user_id']}/_default.jpg\" /></a>";?></div><div class="message_text"><?php echo "<a href=\"members.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}</a>"; ?><? } ?> 
+0

注意:PHP區分大小寫。 '免費'不等於'免費' – codingbiz 2013-02-09 15:38:16

回答

0

你最好的賭注是服務起來profile.php內的相關模板文件。例如,在你的profile.php你可以做這樣的事情:

$the_user = new User($_REQUEST['id']); 
$include_file = ($the_user->account_type == 'free') ? 'free' : 'premium'; 
include($include_file.'-profile.php'); 
0

我嚴重不知道爲什麼有這麼多的打開和無故文檔中密切<?php標籤...

以及一較短的版本將是:

<?php 
function is_premium_user($profile_id) 
{ 
    $query = sprintf("SELECT ptb_users.account_type WHERE ptb_messages.from_user_id = '%s' AND account_type = 'Premium' LIMIT 1", $profile_id); 
    $result = mysql_query($query); 
    if (!mysql_fetch_assoc($result)) 
     return false; 
    return true; 
} 
$message_account_type = message_account_type(); 
while ($type = mysql_fetch_array($message_account_type)) 
if (is_premium_user($profile_id)) 
    echo '<a href="profile.php?id='.$inbox['from_user_id'].'><img width="50px" height="50px" src="data/photos/'.$inbox['from_user_id'].'/_default.jpg"/></a></div><div class="message_text"> <a href="profile.php?id='.$inbox['from_user_id'].'">'.$inbox['display_name'].'</a>'; 
else 
    echo '<a href="members.php?id='.$inbox['from_user_id'].'><img width="50px" height="50px" src="data/photos/'.$inbox['from_user_id'].'/_default.jpg"/></a></div><div class="message_text"> <a href="members.php?id='.$inbox['from_user_id'].'">'.$inbox['display_name'].'</a>'; 
?>