I am creating a social networking website using PHP and MYSQL. I am facing problem in loading posts of friends and the current user, and arranging them according to the recent. Here is my program, I know this is not a good method but this is what i could manage to code.
<?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';
}
}
}
?>
My above codes work properly but the only problem is I am not able to arrange them according to the recent time, i.e., load new posts first and then old posts.
<?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
}