[英]filtering and sorting foreach loop by date (string) in php
我的问题有点令人困惑,
我的数据库中有事件,每个事件都有自己的开始日期和结束日期,所以我创建了三个表
第一个是公开活动
第二个是未打开的事件
最后一个是封闭的事件
所以我想创建用于创建表行的 foreach 循环,并按第二个的开始日期对其进行排序,并按第一个和最后一个的结束日期对其进行排序
谢谢
我的尝试:
<table>
<thead>
<tr>
.
.
.
</tr>
</thead>
<tbody>
<?php
sort($rows);
$u = 0;
foreach ($rows as $row) :
$u++;
$university = getUniversityByBsc($row['id']);
if ($today < $row['date_end'] && $today > $row['date']) :
?>
<tr>
.
.
.
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
.
.
.
</tr>
</tfoot>
</table>
<table>
<thead>
<tr>
.
.
.
</tr>
</thead>
<tbody>
<?php
sort($rows);
$u = 0;
foreach ($rows as $row) :
$u++;
$university = getUniversityByBsc($row['id']);
if ($today > $row['date_end'] && $today > $row['date']) :
?>
<tr>
.
.
.
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
.
.
.
</tr>
</tfoot>
</table>
<table>
<thead>
<tr>
.
.
.
</tr>
</thead>
<tbody>
<?php
sort($rows);
$u = 0;
foreach ($rows as $row) :
$u++;
$university = getUniversityByBsc($row['id']);
if ($today < $row['date_end'] && $today < $row['date']) :
?>
<tr>
.
.
.
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
.
.
.
</tr>
</tfoot>
</table>
您的结构是关联的 arrays 数组。 要按键值排序,您需要提供自定义比较 function ,它需要两个 arrays 然后在它们之间进行比较。
您可以访问数组排序查看所有 PHP 排序函数。
我相信你需要使用usort 。
在文档示例中,有 function 可以帮助您实现所需。
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
这个 function 将一个键作为参数,它将返回一个比较 function 将比较与该键关联的值。
将 function 复制到您的代码中,然后使用它对数组进行排序。
// This will sort the array based on start_date descending.
usort($rows, build_sorter('start_date'));
// This will sort the array based on start_date descending.
usort($rows, build_sorter('end_date'));
//Call the function before your table and sort the array as you which.
更新:
这是示例代码,请注意,当我编写代码时,我使用start_date
和end_date
作为字段名称,而不是date_start
和date_end
。 另外请避免使用旧的 PHP 语法。
<?php
//the function sorting function for usort
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
//sample data
$rows = array(
[
'id' => '1',
'start_date' => '2021-01-27',
'end_date' => '2021-02-28'
],
[
'id' => '2',
'start_date' => '2020-01-28',
'end_date' => '2020-02-28'
],
[
'id' => '3',
'start_date' => '2020-01-01',
'end_date' => '2020-01-10'
],
);
//Today dates as mentioned in the comments
$today = date('Y-m-d',time());
?>
<table>
<thead>
<tr>
<td>
id
</td>
<td>
start date
</td>
<td>
end date
</td>
</tr>
</thead>
<tbody>
<?php
//first table sort by end_date
usort($rows,build_sorter('end_date'));
//now your array is sorted you need to create the table
//for loop throw the rows to print and for filttering
foreach ($rows as $row){
if ($today < $row['end_date'] && $today > $row['start_date']){
echo "<tr><td>{$row['id']}</td><td>{$row['start_date']}</td><td>{$row['end_date']}</td><tr>";
}
}
?>
</tbody>
</table>
<table>
<thead>
<tr>
<td>
id
</td>
<td>
start date
</td>
<td>
end date
</td>
</tr>
</thead>
<tbody>
<?php
//second table sort by start_date
usort($rows,build_sorter('start_date'));
//now your array is sorted you need to create the table
//for loop throw the rows to print and for filttering
foreach ($rows as $row){
if ($today > $row['end_date'] && $today > $row['start_date']){
echo "<tr><td>{$row['id']}</td><td>{$row['start_date']}</td><td>{$row['end_date']}</td><tr>";
}
}
?>
</tbody>
</table>
<table>
<thead>
<tr>
<td>
id
</td>
<td>
start date
</td>
<td>
end date
</td>
</tr>
</thead>
<tbody>
<?php
//third table sort by end_date
usort($rows,build_sorter('end_date'));
//now your array is sorted you need to create the table
//for loop throw the rows to print and for filttering
foreach ($rows as $row){
if ($today < $row['end_date'] && $today < $row['start_date']){
echo "<tr><td>{$row['id']}</td><td>{$row['start_date']}</td><td>{$row['end_date']}</td><tr>";
}
}
?>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.