0

これが私のデータベースの私のテーブルだとしましょう:

ID      | APP_TIME                  | NAME
----------------------------------------------------
1       | 2012-07-08 10:00:00       | John
2       | 2012-07-08 12:00:00       | Johnny
3       | 2012-07-09 13:00:00       | Fred
4       | 2012-07-10 09:00:00       | George
5       | 2012-07-10 10:30:00       | Eva
6       | 2012-07-10 14:00:00       | Monica
7       | 2012-07-11 12:00:00       | Helen
8       | 2012-07-11 13:00:00       | Kim

次に、自分のサイトで出力したいもの:

07-08-2012
Time    | Name
----------------------------------------------------
10:00   | John
12:00   | Johnny


07-09-2012
Time    | Name
----------------------------------------------------
13:00   | Fred


07-10-2012
Time    | Name
----------------------------------------------------
09:00   | George
10:30   | Eva
14:00   | Monica


07-11-2012
Time    | Name
----------------------------------------------------
12:00   | Helen
13:00   | Kim

私の問題は、日時の値などをフォーマットすることではありません...私にとっての課題は、「属する」日付に従って結果をリストすることです。

誰か助けて?:)

4

2 に答える 2

1

次のように、MySQL に大変な作業を任せます。

SELECT DATE(app_time) as `day`, TIME(app_time) as `time`, name FROM table ORDER BY `day` ASC, `time` ASC

これは、日付、時刻、および名前を、日付、時刻の順に並べ替えて返します。

次に、結果行をループし、以下の例のように任意の構造に集計します (これは、クエリ セットからの結果セットが既にあり、$resultmysqli を使用していることを前提としています)

$sorted_array = array();
while ($row = mysqli_fetch_object($result)) {
    $sorted_array[$row->day][] = array('time' => $row->time, 'name' => $row->name);
}
于 2012-08-03T18:27:46.740 に答える
0

この解決策を思いついた:

$today = date('%Y-%m-%d 00:00:00');
$get_dates = mysql_query("SELECT DISTINCT DATE_FORMAT(app_time, '%Y-%m-%d') AS new_date FROM reservations WHERE app_time > '$today' ORDER BY app_time ASC");
while ($row = mysql_fetch_array($get_dates)) {
    $the_date = $row['new_date'];
    $results = mysql_query("SELECT * FROM reservations WHERE app_time >= '$the_date 00:00:00' AND app_time < '$the_date 23:59:59' ORDER BY app_time ASC");
    while ($the_results = mysql_fetch_array($results))
    {
    // Do some stuff eg:
    echo $the_results['name'] . '<br />';
    }
}
于 2012-08-04T13:16:43.713 に答える