PHP Loading data from mysql according to time -
i creating social networking website using php , mysql. facing problem in loading posts of friends , current user, , arranging them according recent. here program, know not method manage code.
<?php if(isset($_cookie["uid"])) { include("includes/functions.php"); include("includes/config.php"); $uid=$_cookie["uid"]; $posts=array(); $friends=array(); $sql="select * posts 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 * friends uid1='$uid' or uid2='$uid' , 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 * posts 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 users 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 $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago'; } } } ?>
my above codes work problem not able arrange them according recent time, i.e., load new posts first , old posts.
<?php if(isset($_cookie["uid"])) { include("includes/functions.php"); include("includes/config.php"); $uid=$_cookie["uid"]; $posts = []; $friends = []; $sql="select uid1, uid2 friends uid1='$uid' or uid2='$uid' , 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 contain friends , person himself $friendssql = implode(', ', $friends); $sql="select * posts uid in ($friendssql)' order 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 contain posts of person , friends in desc order }
Comments
Post a Comment