Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于关联数组php的While循环顺序?_Php_Mysql_Loops_While Loop - Fatal编程技术网

基于关联数组php的While循环顺序?

基于关联数组php的While循环顺序?,php,mysql,loops,while-loop,Php,Mysql,Loops,While Loop,我有以下代码用于显示一个简单的好友系统发送、接收和接受的邀请 while ($friend = $q -> fetch(PDO::FETCH_ASSOC)) { if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have sent a request to be <a href="http://www.n.com/viewaccount?

我有以下代码用于显示一个简单的好友系统发送、接收和接受的邀请

while ($friend = $q -> fetch(PDO::FETCH_ASSOC)) {
      if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have sent a request to be <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '\'s</a> friend.</p>';
      if ($friend['withID'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have recieved a request from <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '</a>.</p>';
      if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 1) echo '<p>You are friends with <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '</a>.</p>';
}

您可以更改SQL查询以执行以下操作:

ORDER BY x ASC/DESC
或者您可以在PHP中创建一个结果数组,然后使用任何PHP排序函数根据任何标准对数组进行排序

有关更多信息,请参阅

编辑:我想

uasort()

可能会有帮助。您可以定义自己的比较函数,然后PHP将根据该函数进行排序。有关mor信息,请参阅。

您可以将SQL查询更改为执行

ORDER BY x ASC/DESC
$q = $dbc -> prepare("SELECT social.*, accounts.username FROM social INNER JOIN accounts WHERE social.withID = accounts.ID AND (social.id = ? OR social.withID = ?) AND type = 'friend' ORDER BY social.friendAccept ASC");
$q -> execute(array($user['id'], $user['id']));
或者您可以在PHP中创建一个结果数组,然后使用任何PHP排序函数根据任何标准对数组进行排序

有关更多信息,请参阅

编辑:我想

uasort()
可能会有帮助。您可以定义自己的比较函数,然后PHP将根据该函数进行排序。有关mor信息,请参阅

$q = $dbc -> prepare("SELECT social.*, accounts.username FROM social INNER JOIN accounts WHERE social.withID = accounts.ID AND (social.id = ? OR social.withID = ?) AND type = 'friend' ORDER BY social.friendAccept ASC");
$q -> execute(array($user['id'], $user['id']));
按社交顺序。friendAccept

它将首先获得不被接受的用户

如果你给出一些数据示例,我会更有帮助

按社交顺序。friendAccept

它将首先获得不被接受的用户


如果您给出一些数据示例,我可以提供更多帮助,这是绝对可能的:

SELECT social.*, accounts.username 
FROM social 
INNER JOIN accounts 
WHERE social.withID = accounts.ID 
AND (social.id = ? OR social.withID = ?) 
AND type = 'friend'
ORDER BY 
(social.id = $user_id AND social.friendAccept = 0), 
(social.withID = $user_id AND social.friendAccept = 0),
(social.id = $user_id AND social.friendAccept = 1)
只需将ORDERBY子句按正确的顺序排列即可。按照这种方式,它使用表达式计算的布尔值对其进行排序


当然,您还应该将$user\u id转换为?在准备好的声明中。

绝对有可能:

SELECT social.*, accounts.username 
FROM social 
INNER JOIN accounts 
WHERE social.withID = accounts.ID 
AND (social.id = ? OR social.withID = ?) 
AND type = 'friend'
ORDER BY 
(social.id = $user_id AND social.friendAccept = 0), 
(social.withID = $user_id AND social.friendAccept = 0),
(social.id = $user_id AND social.friendAccept = 1)
只需将ORDERBY子句按正确的顺序排列即可。按照这种方式,它使用表达式计算的布尔值对其进行排序


当然,您还应该将$user\u id转换为?在准备好的声明中。

正确的顺序是什么?你能显示查询吗?Ok现在就去编辑我的问题…注意,
PDOStatement
实现了这个接口,因此你可以使用
foreach
而不是
while
循环结果。它不影响结果的顺序,但更具可读性(“对于序列的每个元素”而不是“当某些条件保持时”更好地描述了循环的功能)。正确的顺序是什么?你能显示查询吗?Ok现在就去编辑我的问题…注意,
PDOStatement
实现了这个接口,因此你可以使用
foreach
而不是
while
循环结果。它不影响结果的顺序,但更具可读性(“对于序列的每个元素”而不是“当某些条件保持时”更好地描述循环的功能)。它应该是
orderby
,而不是
ORDERY BY
:)它应该是
orderby
,而不是
ORDERY BY
:)好的,现在试试这个,我以前没有遇到过这个问题,谢谢。谢谢这是如此简单,我不知道查询可以根据结果排序。再次感谢巴德!好的,我现在就来试试,我以前没有遇到过,谢谢。谢谢这是如此简单,我不知道查询可以根据结果排序。再次感谢巴德!