2013-08-29 43 views
0

所以我必須有一個while循環和它在數據庫方面的工作,if語句等等。但是由於某些原因,如果語句未正確運行While循環沒有運行if語句正確

這裏是代碼(編輯)

<?php session_start(); ?> 
<?php include 'core/init.php'; 
include 'includes/header.php'; 

require 'core/database/db_connection.php'; 

function friend_check($user_id, $friend_id) { 
$result = mysql_query("SELECT * FROM fyp_friends WHERE user_id={$user_id} AND   friend_id={$friend_id} AND type=1"); 
$num_rows = mysql_num_rows($result); 

echo $num_rows; 
} 

?> 
<link rel="stylesheet" type="text/css" href="style/members.css"> 
<?php 
/*-------------------------------------------------------------------------------------  --------------------*/ 
    //Get user information from fyp_users 
    $sql = ("SELECT user_id, user_name, first_name, last_name, email, profile FROM  fyp_users"); 
$user =mysql_query($sql) or die(mysql_error()); 
//$userinfo = mysql_fetch_array($user); 
//echo $userinfo['user_id']; 

//Get friend information from fyp_users 
//$sql = ("SELECT user_id, friend_id, type FROM fyp_friends"); 
//$friend =mysql_query($sql) or die(mysql_error()); 
//$friendinfo = mysql_fetch_array($friend); 
//echo $friendinfo['user_id']; 
?> 

<?php 
while ($usermain = mysql_fetch_array($user)) { 

echo '<div id="friends">'; 
echo '<div class="profilepiclarge"><img src="', $usermain['profile'], '"alt="', $usermain['first_name'],'\'s Profile Image"></div>'; 
?> 
<div class="userinfo"> 
<?php 
echo "</br>{$usermain['first_name']}"; 
echo "</br>{$usermain['last_name']}"; 
echo "</br>{$usermain['email']}"; 
echo "</br>{$usermain['user_name']}</br>"; 
echo '<div class="viewprofile">'; 
echo "<a href='profile.php?user_name={$usermain['user_name']}'>View Profile</a>"; 
echo '</div>'; 

$sql = ("SELECT user_id, friend_id, type FROM fyp_friends WHERE user_id=  {$usermain['user_id']}"); 
$friend =mysql_query($sql) or die(mysql_error()); 

$friend_result = friend_check($session_user_id, $usermain['user_id']); 
echo $friend_result; 
if ($usermain['user_id'] === $session_user_id) { 
echo "This is you"; 
} else if (intval($friend_result) == 1) { 
echo '<div class="removefriend">'; 
    echo"Remove Friend"; 
    echo '</div>'; 
} else if (intval($friend_result) == 0) { 
echo '<div class="addfriend">'; 
    echo"Add Friend"; 
    echo '</div>'; 
} 

echo "</div>"; 
echo "</div>"; 

//If statment to go here to see if already friends dont show add friends function also if  current user is logged in sont show it. If already friends the option 
//to remove the user as a fried becomes avalible 

} 
+0

多少紀錄在那裏'fyp_friends'? – putvande

+0

你確定'fyp_friends'不包含重複的行嗎?嘗試使用SQL瀏覽器並根據您的預期檢查結果。 –

+0

putvande目前只是3.邁克爾託德和耶已經檢查,並沒有重複。 –

回答

0

你的第二個查詢有沒有WHERE子句所以我沒有看到你是如何上市有關問題的用戶朋友,查詢更改爲:

SELECT user_id, friend_id, type FROM fyp_friends WHERE user_id = $usermain['user_id'] 
+0

沒有這個運氣 –

+0

嘗試改變循環,並使用'mysql_fetch_assoc' – Sir

+0

Nahh,沒有發生,一起停止第二個循環。 –