如果$userid
等于$row['RequestRecipientID']
,则脚本将输出所有等于“已接受”的$row['StatusType']
。我需要这个脚本才能输出等于$row['StatusType']
的{{1}}。有什么想法吗?
$userid
答案 0 :(得分:1)
如果您只需要与用户标识相关的结果,则应首先使用SQL查询进行过滤。
// Assuming that your SQL query is correct
// added WHERE users.RequestRecipientID = '$userid'
$results = mysql_query("SELECT requests.RequestRecipientID, requests.StatusType, requests.AddedMessage, requests.FriendType, requests.RequestSentByID, requests.id, users.username, users.firstname, users.lastname, users.email, users.user_pid, users.id FROM requests, users WHERE users.RequestRecipientID = '$userid'");
// used fetch_assoc instead of fetch_array since you are
// not using numeric array
while($row = mysql_fetch_assoc($results)) {
// normally 3 is my limit to use if-else
// if i have more than 3, i'll use switch
if ($row['StatusType'] == "Accepted") {
// accepted action
}
elseif ($row['StatusType'] == "Denied") {
// denied action
}
else {
// not denied & accepted action
}
}
答案 1 :(得分:1)
要做的一些事情。
首先,阅读,学习并喜欢MySQL Joins - Coding Horror: MySQL Joins
您似乎在RequestRecipientID和RequestSendByID之间感到困惑(对于我的解决方案,我假设您希望查看当前用户发送或发送给当前用户的所有请求。
每当与数据库交互时,您确实需要测试错误。没有测试错误只是要求在没有解释的情况下破解。
<?php
include( 'enc.php' );
mysqlcon();
$userid = $_SESSION['userid'];
$sqlStr = 'SELECT
`r`.`RequestRecipientID`, `r`.`StatusType`, `r`.`AddedMessage`, `r`.`FriendType`, `r`.`RequestSentByID`, `r`.`id` AS `requestID`,
`other`.`username`, `other`.`firstname`, `other`.`lastname`, `other`.`email`, `other`.`user_pid`, `other`.`id` AS `otherID`
FROM `request` AS `r`
LEFT JOIN `users` AS `other` ON ( IF( `r`.`RequestRecipientID` = '.(int) $userid.' , `r`.`RequestSentByID` , `r`.`RequestRecipientID` ) = `user`.`id` )
WHERE
'.(int) $userid.' IN ( `r`.`RequestSentByID` , `r`.`RequestRecipientID` )';
$results = mysql_query( $sqlStr );
if( !$results ){
// There was an error in the SQL Query
}elseif( mysql_num_rows( $results )==0 ){
// No Records were returned
}else{
while( $r = mysql_fetch_array( $results ) ){
if( $r['RequestSentByID']==$userid ){
// A Request Sent by the Current User
switch( strtolower( $r['StatusType'] ) ){
case 'accepted' :
case 'denied' :
echo $r['firstname'].' has '.strtolower( $r['StatusType'] ).' your friend request';
break;
case 'pending' :
echo $r['firstname'].' has not yet responded to your friend request';
default:
echo 'This Request has an unexpected Status of '.strtolower( $r['StatusType'] );
}
}else{
// A Request Sent to the Current User
switch( strtolower( $r['StatusType'] ) ){
case 'accepted' :
case 'denied' :
echo 'You '.strtolower( $r['StatusType'] ).' a friend request from '.$r['firstname'];
break;
case 'pending' :
echo '<span class="text1">'.$r['firstname'].' '.$r['lastname'].'<br>'.$r['AddedMessage'].'</span>';
echo '<form method="get" action="protected/process-friend-request-action.php">';
echo '<input class="action_button" name="accepted" type="submit" value="Accept" />';
echo '<input class="action_button" name="denied" type="submit" value="Deny" />';
echo '<input type="hidden" name="id" value="'.$r['requestID'].'" />';
echo '</form>';
default:
echo 'This Request has an unexpected Status of '.strtolower( $r['StatusType'] );
}
}
}
}