所以,我一直在尝试为一个练习项目创建一个用户的活动页面,在一个页面上显示他们的所有活动和他们朋友的活动。我已经完成了这项工作,但由于查询被嵌套,因此无法按日期排序。问题是我必须先得到用户的朋友,它在一个表中,然后我必须得到提到这个用户的通知。
进行查询以便可以按日期排序的最佳方法是什么。谢谢你的帮助!
表格的设置很模糊,如下所示:
一个用户
编号 || 名字 || 姓氏 || 用户名
B朋友
用户名 || 好友编号
C墙贴:
Wallpost_id || from_user_id || to_user_id || 墙贴|| 日期时间代码如下:
try {
$DBH = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOexception $e) {
echo $e->getMessage();
}
try {
$myid = $_SESSION['identification'];
$result = $DBH->prepare("SELECT Friends.*, users.id, users.Firstname,users.Lastname FROM Friends
JOIN users ON Friends.Friends=users.id
WHERE Friends.id=:myid");
$result->execute(array(':myid' => $myid));
$result->setFetchMode(PDO::FETCH_ASSOC);
$r = $result->fetchAll();
echo "<br><br><br>";
echo "<ul>";
try{
$stmt = $DBH->prepare("CREATE TEMPORARY TABLE usering(`user_id` int(10) , `First_Name` varchar(250), `Last_Name` varchar(250), `user_name` varchar(135), `password` varchar(135), `NaCl` varchar(135))");
$stmt->execute();
$stmy = $DBH->prepare("INSERT INTO usering(`user_id`, `First_Name`, `Last_Name`, `user_name`, `password`, `NaCl`) SELECT * from users");
$stmy->execute();
}catch(PDOexception $e) {
echo $e->getMessage();
}
try {
foreach($r as $row){
$FriendId = $row['Friends'];
$result2 = $DBH->prepare("SELECT wallpost.*, usering.*, users.* FROM wallpost
JOIN usering ON wallpost.from_user_id=usering.user_id
JOIN users ON wallpost.to_user_id=users.id
WHERE :FriendId in (from_user_id, to_user_id) ORDER BY wallpost_id DESC
");
$result2->execute(array(':FriendId' => $FriendId));
$result2->setFetchMode(PDO::FETCH_ASSOC);
$r2 = $result2->fetchAll();
foreach($r2 as $row2){
$from_user_id = $row2['from_user_id'];
$fromUsername = $row2['user_name'];
$from_user_name = $row2['First_Name'] . " " . $row2['Last_Name'];
$to_user_name = $row2['Firstname'] . " " . $row2['Lastname'];
$toUsername = $row2['username'];
$to_user_id = $row2['to_user_id'];
$wallpost = $row2['wallpost'];
/*"<div class='miniprofile_right'><img class='microphoto' src='../" . $from_username . "/profile.jpg'><div class='status_text'><a href='../" . $from_username . "/" . $from_user_id . "profile.php'>" . $from_user_firstname . " " . $from_user_lastname . "</a> => ". $firstname . " " . $lastname . ":</br>" . $walldate2 . "<br>" . $wallpost . " - at " . $walltime . "</div></div><br><br>"*/
if($from_user_id == $to_user_id){
echo "<div class='miniprofile_right'><img class='microphoto' src='../members/" . $fromUsername . "/profile.jpg'><div class='status_text'><a href='../members/" . $fromUsername . "/" . $from_user_id . "profile.php'>" . $from_user_name . "</a> posted a status: <br>" . $wallpost . "</div></div></br></br>";
} else {
echo "<div class='miniprofile_right'><img class='microphoto' src='../members/" . $fromUsername . "/profile.jpg'><div class='status_text'><a href='../members/" . $fromUsername . "/" . $from_user_id . "profile.php'>" . $from_user_name . "</a> posted on " . "<a href='../members/" . $toUsername . "/" . $to_user_id . "profile.php'>" . $to_user_name . "'s" . "</a> wall: <br> " . $wallpost . "</div></div></br></br>";
}
}
}
}catch(PDOexception $e) {
echo $e->getMessage();
}
echo "</ul>";
} catch(PDOexception $e) {
echo $e->getMessage();
}