我正在使用PHP和MYSQL创建一个社交网站。我在加载朋友和当前用户的帖子时遇到问题,并根据最近的安排进行排列。这是我的程序,我知道这不是一个好方法,但这是我可以设法编码的。
<?php
if(isset($_COOKIE["uid"]))
{
include("includes/functions.php");
include("includes/config.php");
$uid=$_COOKIE["uid"];
$posts=array();
$friends=array();
$sql="SELECT * FROM posts WHERE uid='$uid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$posts[]=[$row["pid"],$row["uid"],$row["post"],$row["time"]];
}
}
$sql="SELECT * FROM friends WHERE uid1='$uid' OR uid2='$uid' AND status='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($uid1==$uid){$friends[]=$uid2;}else{$friends[]=$uid1;}
}
}
for($i=0;$i<count($friends);$i++)
{
$temp_uid=$friends[$i];
$sql="SELECT * FROM posts WHERE uid='$temp_uid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$posts[]=[$row["pid"],$row["uid"],$row["post"],$row["time"]];
}
}
}
$eo=0;
for($i=0;$i<count($posts);$i++)
{
echo "Username: ".u2u($posts[$i][1])."<br>Post Message: ".nl2br($posts[$i][2])."<br>Time: ".t2t($posts[$i][3])."<p>";
}
}
else
{
echo "error";
}
?>
的config.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "evenure";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
的functions.php
<?php
function u2u($temp_uid){
require("../config.php");
$sql="SELECT username FROM users WHERE uid='$temp_uid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
return $row["username"];
}
}
else
{
return "Invalid UID";
}
}
function t2t($temp_time){
date_default_timezone_set('Asia/Kolkata');
$etime = time() - $temp_time;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
?>
我的上述代码工作正常但唯一的问题是我无法根据最近的时间安排它们,即首先加载新帖子然后加载旧帖子。
答案 0 :(得分:1)
<?php
if(isset($_COOKIE["uid"]))
{
include("includes/functions.php");
include("includes/config.php");
$uid=$_COOKIE["uid"];
$posts = [];
$friends = [];
$sql="SELECT uid1, uid2 FROM friends WHERE uid1='$uid' OR uid2='$uid' AND status='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$friends[$uid1] = $uid1;
$friends[$uid2] = $uid2;
}
}
$friends[$uid] = $uid;
// $friends now contain all friends and person himself
$friendsSql = implode(', ', $friends);
$sql="SELECT * FROM posts WHERE uid IN ($friendsSql)' ORDER BY posts.time DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$posts[]=[$row["pid"],$row["uid"],$row["post"],$row["time"]];
}
}
// $posts now contain posts of person and his friends in desc order
}
答案 1 :(得分:0)
在你的SQL查询中
$sql="SELECT * FROM friends WHERE uid1='$uid' OR uid2='$uid' AND status='1'";
你需要使用ORDER BY运算符,你没有告诉存储发布日期和时间的数据库的列名,所以让我们假设列名为post_dt。
从旧版本到最新版本,您将把这个版本写成:
$sql="SELECT * FROM friends WHERE uid1='$uid' OR uid2='$uid' AND status='1' ORDER BY post_dt ASC" ;
对于最新到最旧的帖子,您将其写为:
$sql="SELECT * FROM friends WHERE uid1='$uid' OR uid2='$uid' AND status='1' ORDER BY post_dt DESC ";
我希望这会对你有所帮助。