如果账户类型= x转到链接1否则如果账户类型= y需要链接2?
问题描述:
我在我的网站上有两种类型的用户,'免费'和'高级'。 基本上我有一个消息系统,列出消息和发送消息的用户。如果账户类型= 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>"; ?><? } ?>
答
你最好的赌注是服务起来profile.php
内的相关模板文件。例如,在你的profile.php
你可以做这样的事情:
$the_user = new User($_REQUEST['id']);
$include_file = ($the_user->account_type == 'free') ? 'free' : 'premium';
include($include_file.'-profile.php');
答
我严重不知道为什么有这么多的打开和无故文档中密切<?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>';
?>
注意:PHP区分大小写。 '免费'不等于'免费' – codingbiz 2013-02-09 15:38:16