【问题标题】:PHP Loading data from mysql according to timePHP根据时间从mysql加载数据
【发布时间】:2015-07-27 06:10:08
【问题描述】:

我正在使用 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';
        }
    }
}
?>

我上面的代码工作正常,但唯一的问题是我无法根据最近的时间安排它们,即先加载新帖子,然后加载旧帖子。

【问题讨论】:

  • 你不能用ORDER BY吗?
  • SELECT * FROM posts WHERE uid='$temp_uid' ORDER BY name_of_your_date_column DESC(如果你想要最旧的优先,则为 ASC)
  • @KayNelson:你还不明白我的问题。假设我使用按时间排序,然后我先获取当前用户的帖子,然后再获取当前用户的朋友帖子。但是如果朋友比当前用户更早发帖怎么办?我想按照时间排列数组中的所有帖子。

标签: php mysql sql mysqli social-networking


【解决方案1】:

在你的 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 ";

希望对你有帮助。

【讨论】:

  • 不,你没有明白我的意思。它不仅是当前用户的帖子,还有他的朋友的帖子。所以首先我得到了用户的所有帖子,然后是他的朋友,但是如果他的朋友之前给当前用户发了帖子怎么办?这对我的情况没有帮助:(
  • @Manikiran,为此您需要在 jquery 中使用间隔类型的函数,该函数将在您在函数中提供的时间限制内执行,并将在该间隔内更新用户页面。任何您需要 ORDER BY 运算符来执行排列结果的方式。
【解决方案2】:
<?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
}

【讨论】:

  • 谢谢,但您可以直接将当前用户添加到朋友数组中,例如$friends[]=$uid;,而不是所有这些功能。无论如何,这对我有用:)
  • 对不起,我没看到,但你使用了$friends[]=$uid;。干得好
猜你喜欢
  • 2011-06-05
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 2014-11-14
  • 1970-01-01
相关资源
最近更新 更多