[英]Specify number of posts to display by date in PHP
我有这个脚本,可以从文本文件中提取数据(帖子)并对其进行排序。 现在,我可以选择显示该列表中的所有内容或文本中显示的特定数量的帖子。 我的下一个目标是找出某种按日期显示的方式。 例如,显示过去30天的所有帖子。
我认为可以通过更改一些如何按日期而不是数字进行排序来完成:
$number = min(count($data), 5);
for($i = 0; $i < $number; $i++)
到目前为止,我有:
<?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);
?>
您应该使用sort
功能。 您可以使用usort
完成此操作。 尝试这个:
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);
最近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);
您是否有充分的理由将所有内容存储在文本文件中?
如果没有,我将使用某种数据库。 在存储,访问和排序方面,这将使您的生活变得更加轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.