有一个下拉列表,可以从' tablesite'中获取用户名。 TABEL。
<?php
...
$dbresult=mysql_query("SELECT *
FROM tablesite",$con2);
echo'* نام کاربری که به او خدمت داده اید: ','<br/>';
echo '<select name="users" dir="rtl">';
while($amch=mysql_fetch_assoc($dbresult))
{
/* if there is username of curent user, do not echo that username and just echo other usernames. somthing like this:
if(username='$username')
{...} */
echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>';
}
echo '</select>';
?>
一切都是真的。但我需要在此下拉列表中不显示当前登录用户的用户名。
以下是我们如何获取当前用户名的信息:
$username=$fgmembersite->UserNameOfUser();
$user_id =$fgmembersite->UserID();
使用这些功能:
function UserID()
{
return isset($_SESSION['user_id'])?$_SESSION['user_id']:'';
}
function UserFullName()
{
return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
}
答案 0 :(得分:3)
当然最简单的方法是不从查询中返回当前的用户名吗?
$currentuserName = ''; //however you store the curent username
"SELECT * FROM tablesite WHERE username != '$currentuserName'";
or
$user_id = $fgmembersite->UserID();
"SELECT * FROM tablesite WHERE id_user != '$user_id'";
...删除任何循环逻辑的neeed。
你还需要停止使用mysql_ *函数,并查看mysqli_ *或PDO,因为mysql_ *函数正在慢慢被弃用。
答案 1 :(得分:1)
替换它:
while($amch=mysql_fetch_assoc($dbresult))
{
/* if there is username of curent user, do not echo that username and just echo other usernames. somthing like this:
if(username='$username')
{...} */
echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>';
}
用这个:
$user_id =$fgmembersite->UserID();
while($amch=mysql_fetch_assoc($dbresult))
{
if ($user_id != $amch['id_user']) {
echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>';
}
}
这应该是一种方法......
答案 2 :(得分:1)
while($amch=mysql_fetch_assoc($dbresult))
{
if ($amch['id_user'] != $_SESSION['user_id']) {
echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>';
}
}