简体   繁体   English

指定要按日期在PHP中显示的帖子数

[英]Specify number of posts to display by date in PHP

I have this script that pulls data (posts) from a text file, and sorts it. 我有这个脚本,可以从文本文件中提取数据(帖子)并对其进行排序。 Right now I have an option to display everything in that list or a certain number of posts from the text. 现在,我可以选择显示该列表中的所有内容或文本中显示的特定数量的帖子。 My next goal is figuring out some way to display by date. 我的下一个目标是找出某种按日期显示的方式。 Excample, show all posts from the last 30 days. 例如,显示过去30天的所有帖子。

I think it might be done with changing this some how to sort by date instead of number count: 我认为可以通过更改一些如何按日期而不是数字进行排序来完成:

    $number = min(count($data), 5);
    for($i = 0; $i < $number; $i++)

What I have so far: 到目前为止,我有:

<?php
$data = unserialize(file_get_contents('data.txt'));
$data = array_reverse($data);
$c = 0;
$number = min(count($data), 5);
for($i = 0; $i < $number; $i++)
{
$date = date("F j, Y, g:i a", $data[$i]['date']);
$user = htmlspecialchars(stripslashes($data[$i]['user']));
$message = htmlspecialchars(stripslashes($data[$i]['message']));
$other = htmlspecialchars(stripslashes($data[$i]['other']));
$website = htmlspecialchars(stripslashes($data[$i]['website']));
$user = "$user";

if($c == 0)
{
$c1 = '#BBBBBB';
$c2 = '#DDDDDD';
$c = 1;
}
else
{
$c1 = 'CCCCCC';
$c2 = '#EEEEEE';
$c = 0;
}
if($data[$i]['user'] != '11jds83jd7')
{
echo"<tr><td width=\"300\" valign=\"top\" style=\"background-color: $c1\"><strong>$user</strong><br/>$date</td><td valign=\"top\" style=\"background-color:         $c2\">$other<br>$website<br>$message<br></td></tr>";
}
}
if(count($data) == 0)
{
echo '<tr><td colspan="2"><strong>There is nothing to display.</strong><br/><br/></td></tr>';
}
$n1 = rand(0, 10);
$n2 = rand(0, 10);
?>

You should use a sort function. 您应该使用sort功能。 You can use usort to accomplish this. 您可以使用usort完成此操作。 Try this: 尝试这个:

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

$data = unserialize(file_get_contents('data.txt'));
$data = array_reverse($data);
$c = 0;
$number = min(count($data), 5);

usort($data, "cmp");    

//You can change $number here in 30 if you want it only for the last 30 days.

for($i = 0; $i < $number; $i++)
{
    $date = date("F j, Y, g:i a", $data[$i]['date']);
    $user = htmlspecialchars(stripslashes($data[$i]['user']));
    $message = htmlspecialchars(stripslashes($data[$i]['message']));
    $other = htmlspecialchars(stripslashes($data[$i]['other']));
    $website = htmlspecialchars(stripslashes($data[$i]['website']));
    $user = "$user";

    if($c == 0)
    {
        $c1 = '#BBBBBB';
        $c2 = '#DDDDDD';
        $c = 1;
    }
    else
    {
        $c1 = 'CCCCCC';
        $c2 = '#EEEEEE';
        $c = 0;
    }

    if($data[$i]['user'] != '11jds83jd7')
    {
        echo"<tr><td width=\"300\" valign=\"top\" style=\"background-color: $c1\"><strong>$user</strong><br/>$date</td><td valign=\"top\" style=\"background-color:         $c2\">$other<br>$website<br>$message<br></td></tr>";
    }
}

if(count($data) == 0)
{
    echo '<tr><td colspan="2"><strong>There is nothing to display.</strong><br/><br/></td></tr>';
}

$n1 = rand(0, 10);
$n2 = rand(0, 10);

EDITED for the last 30 days instead of last 30 messages 最近30天而不是最近30条消息的编辑时间

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

$data = unserialize(file_get_contents('data.txt'));
$data = array_reverse($data);
$c = 0;
$number = min(count($data), 5);

usort($data, "cmp");    

//Set a date array of the last 30 days
$dates          = array();
$numberOfDays   = 30;

for($i = 0; $i < $numberOfDays ; $i++)
{
    $dates[] = date("F j, Y", strtotime( '-'.$i.' days' ));
}


for($i = 0; $i < $number; $i++)
{
    if(in_array(date("F j, Y", $data[$i]['date']), $dates))
    {
        $date = date("F j, Y, g:i a", $data[$i]['date']);
        $user = htmlspecialchars(stripslashes($data[$i]['user']));
        $message = htmlspecialchars(stripslashes($data[$i]['message']));
        $other = htmlspecialchars(stripslashes($data[$i]['other']));
        $website = htmlspecialchars(stripslashes($data[$i]['website']));
        $user = "$user";

        if($c == 0)
        {
            $c1 = '#BBBBBB';
            $c2 = '#DDDDDD';
            $c = 1;
        }
        else
        {
            $c1 = 'CCCCCC';
            $c2 = '#EEEEEE';
            $c = 0;
        }

        if($data[$i]['user'] != '11jds83jd7')
        {
            echo"<tr><td width=\"300\" valign=\"top\" style=\"background-color: $c1\"><strong>$user</strong><br/>$date</td><td valign=\"top\" style=\"background-color:         $c2\">$other<br>$website<br>$message<br></td></tr>";
        }
    }
}

if(count($data) == 0)
{
    echo '<tr><td colspan="2"><strong>There is nothing to display.</strong><br/><br/></td></tr>';
}

$n1 = rand(0, 10);
$n2 = rand(0, 10);

Do you have a really good reason for storing everything in a text file? 您是否有充分的理由将所有内容存储在文本文件中?

If not, I would use a database of some kind. 如果没有,我将使用某种数据库。 This will make your life a whole lot easier, in terms of storage, access and sorting. 在存储,访问和排序方面,这将使您的生活变得更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM