简体   繁体   中英

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.

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. You can use usort to accomplish this. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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