Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

filtering and sorting foreach loop by date (string) in php

My question is a bit coufusing ,

I got events in my database every event has it own start date and end date so I created three tables

  1. the first one is for open events

  2. the second one is for not opened events

  3. the last one is for closed events

So I want to create foreach loop for creating table rows and sort it by start date for the second one and sort it by end date for the first and the last one

and thanks

my try :

<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>
question from:https://stackoverflow.com/questions/65925474/filtering-and-sorting-foreach-loop-by-date-string-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

your structure is array of associated arrays. To sort by key value, you need to provide your custom comparison function that takes two arrays then compare between them.

You can visit Array Sorting to see all PHP sorting functions.

I believe you need to use usort.

In the documentation example, there is function that can help you to achieve what you need.

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

This function will take a key as parameter, and it will return a comparison function that will compare the values associated with that key.

Copy the function to your code then use it to sort your array.

// 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. 

Update:

Here is sample code, please note when I wrote the code I used start_date and end_date as field name instead of date_start and date_end. Also please avoid using old PHP syntax.

<?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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...