我正在尝试根据文章发表时的《泰晤士报》对结果进行分组(就像newsnow网站汇总一样)
我的PHP代码如下:
$sql="SELECT articles.id, name, title, description, timestamp
FROM articles
LEFT JOIN sources ON `articles`.`source_id` = `sources`.`id`
ORDER BY timestamp
DESC LIMIT 25";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;
switch (true) {
case ($timedifference < 300):
echo "<br/>Less than 5 minutes ago <br/>";
echo "$row['title']";
break;
case ($timedifference < 900 && $timedifference > 300):
echo "<br/>15 Mins ago <br/>";
echo "$row['title']";
break;
case ($timedifference < 1800 && $timedifference > 900):
echo "<br/>30 mins ago <br/>";
echo "$row['title']";
break;
case ($timedifference < 3600 && $timedifference > 1800):
echo "<br/>about an hour ago <br/>";
echo "$row['title']";
}
}
} mysqli_close($db);
当前,上面的代码显示如下:
Less than 5 minutes ago
Article Name A
Less than 5 minutes ago
Article Name B
Less than 5 minutes ago
Article Name C
30 mins ago
Article Name D
30 mins ago
Article Name E
有没有一种方法可以按文章的发布时间对其进行分组(例如:)
Less than 5 minutes ago
Article Name A
Article Name B
Article Name C
30 mins ago
Article Name D
Article Name E
非常感谢:)
答案 0 :(得分:1)
您必须将所有访问过的案例信息存储在数组或变量中。当前的解决方案。
$visitedCase = [];
while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;
switch (true) {
case ($timedifference < 300):
if(!(in_array(1, $visitedCase)) {
echo "<br/>Less than 5 minutes ago <br/>";
$visitedCase[] = 1;
}
break;
case ($timedifference < 900 && $timedifference > 300):
if(!(in_array(2, $visitedCase)){
echo "<br/>15 Mins ago <br/>";
$visitedCase[] = 2;
}
break;
case ($timedifference < 1800 && $timedifference > 900):
if(!(in_array(3, $visitedCase)){
echo "<br/>30 mins ago <br/>";
$visitedCase[] = 3;
}
break;
case ($timedifference < 3600 && $timedifference > 1800):
if(!(in_array(4, $visitedCase)){
echo "<br/>about an hour ago <br/>";
$visitedCase[] = 4;
}
}
echo $row['title'];
}
答案 1 :(得分:0)
您可以使用标志来识别是否是第一次开关=>
$i = 0;
$j = 0;
$k = 0;
while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;
switch (true) {
case ($timedifference < 300):
if($i == 0){
echo "<br/>Less than 5 minutes ago <br/>";
$i++;
}
echo "$row['title']";
break;
case ($timedifference < 900 && $timedifference > 300):
if($j == 0){
echo "<br/>15 Mins ago <br/>";
$j++;
}
echo "$row['title']";
break;
case ($timedifference < 1800 && $timedifference > 900):
if($k == 0){
echo "<br/>30 mins ago <br/>";
$k++;
}
echo "$row['title']";
break;
case ($timedifference < 3600 && $timedifference > 1800):
echo "<br/>about an hour ago <br/>";
echo "$row['title']";
}
}
}
答案 2 :(得分:0)
如果要使用PHP进行操作,则必须记住您已经输出了标题行,因此每个分组只进行一次
$sql="SELECT articles.id, name, title, description, timestamp
FROM articles
LEFT JOIN sources ON `articles`.`source_id` = `sources`.`id`
ORDER BY timestamp
DESC LIMIT 25";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;
$done = ['d300'=>false,'d900'=>false,'d1800'=>false,'d3600'=>false];
switch (true) {
case ($timedifference < 300):
if ( !$done['d300'] ) {
echo "<br/>Less than 5 minutes ago <br/>";
$done['d300'] = true;
}
echo "$row['title']";
break;
case ($timedifference < 900 && $timedifference > 300):
if ( !$done['d900'] ) {
echo "<br/>15 Mins ago <br/>";
$done['d900'] = true;
}
echo "$row['title']";
break;
case ($timedifference < 1800 && $timedifference > 900):
if ( !$done['d1800'] ) {
echo "<br/>30 mins ago <br/>";
$done['d1800'] = true;
}
echo "$row['title']";
break;
case ($timedifference < 3600 && $timedifference > 1800):
if ( !$done['d3600'] ) {
echo "<br/>about an hour ago <br/>";
$done['d3600'] = true;
}
echo "$row['title']";
break;
}
}
答案 3 :(得分:0)
这是可以根据需要显示输出的代码。
$sql="SELECT articles.id, name, title, description, timestamp FROM articles
LEFT JOIN sources ON `articles`.`source_id` = `sources`.`id`
ORDER BY timestamp DESC LIMIT 25";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)) {
$currentTime = time();
$articletimes = $row['timestamp'];
$timedifference = $currentTime - $articletimes;
$td_3 = $td_9 = $td_18 = $td_36 = 0;
switch (true) {
case ($timedifference < 300):
if($td_3 == 0){
echo "<br/>Less than 5 minutes ago <br/>";
}
$td_3 = 1;
echo "$row['title']";
break;
case ($timedifference < 900 && $timedifference > 300):
if($td_9 == 0){
echo "<br/>15 Mins ago <br/>";
}
$td_9 = 1;
echo "$row['title']";
break;
case ($timedifference < 1800 && $timedifference > 900):
if($td_18 == 0){
echo "<br/>30 mins ago <br/>";
}
$td_18 = 1;
echo "$row['title']";
break;
case ($timedifference < 3600 && $timedifference > 1800):
if($td_36 == 0){
echo "<br/>about an hour ago <br/>";
}
$td_36 = 1;
echo "$row['title']";
}
}