Reputation: 43
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);
?>
Upvotes: 0
Views: 40
Reputation: 3434
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);
Upvotes: 1
Reputation: 554
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.
Upvotes: 0