【问题标题】:filtering and sorting foreach loop by date (string) in php在php中按日期(字符串)过滤和排序foreach循环
【发布时间】:2021-01-27 18:47:13
【问题描述】:

我的问题有点令人困惑,

我的数据库中有事件 每个事件都有自己的开始日期和结束日期 所以我创建了三个表

  1. 第一个用于公开活动

  2. 第二个是未打开的事件

  3. 最后一个是关闭的事件

所以我想创建用于创建表行的 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>

【问题讨论】:

  • 欢迎您,到目前为止您尝试了什么
  • 您的结构一开始似乎不是最佳的。你有三个单独的表真的有充分的理由吗?对于这三种状态,你有很多不同的数据吗?
  • 不,我没有结构,但这种类型的事件需要一些分离
  • 表是指 3 个 HTML 表而不是 DB 表?您不能对您的案例使用排序。看看usort()
  • 是的,当然我的意思是 3 个 html 表格

标签: php loops


【解决方案1】:

您的结构是关联数组的数组。要按键值排序,您需要提供自定义比较函数,该函数接受两个数组,然后在它们之间进行比较。

您可以访问Array Sorting查看所有PHP排序函数。

我相信你需要使用usort

在文档示例中,有一个功能可以帮助您实现所需的功能。

function build_sorter($key) {
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

此函数将一个键作为参数,它会返回一个比较函数来比较与该键关联的值。

将该函数复制到您的代码中,然后使用它对您的数组进行排序。

// 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_dateend_date 作为字段名称,而不是date_startdate_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>

【讨论】:

  • 非常感谢,但是我怎样才能把它分成三个表
  • @Eymen 是 $today 一个字符串或日期对象,也是 start_date 字符串或日期对象
  • $today = date('Y-m-d'); start_date 和结束日期是包含日期的字符串,如'2021-01-28'
  • @Eymen 我用示例代码更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多