0

我的网站上有两种类型的用户,“免费”和“高级”。基本上我有一个消息系统,列出了消息和发送消息的用户。

当您单击用户的名称/图像时,它将链接到'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>"; ?><? } ?>
4

2 回答 2

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>'; 
?>
于 2013-02-09T15:53:16.113 回答
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');
于 2013-02-09T15:39:10.953 回答