2015-11-05 44 views
0

有一個從'tablesite'tabel提取用戶名的下拉列表。當他/她想選擇某個服務PHP時,停止用戶的回顯用戶名SQL SQL

<?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']:''; 
} 

回答

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

替換此:

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>'; 
    } 
} 

這應該是一個辦法......

1
while($amch=mysql_fetch_assoc($dbresult)) 
{ 
if ($amch['id_user'] != $_SESSION['user_id']) { 
echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>'; 
} 
} 
相關問題