0

MySQL に と を持つテーブルがありstartdateますenddate

1行の例は次のとおりです(全体の命名は無視してください):

eg 
startdate -> 20121224
endate -> 20121226
title -> name
etc

私が持っている現在のスクリプトはうまく機能し、行ごとに JSON に追加されますが、.... したい.... mysql 行ごとに開始日と終了日を取得し、開始日と終了日を含む各日付を JSON に追加しますエンコード。

したがって、上記の 1 行の例ではなく、次のように返されます。

[{
    "user": "378",
    "date": "UNIX_TIMESTAMP(startdate)",
    "title": "name",
    "description": "6 Days",
    "url": "UNIX_TIMESTAMP(enddate)",
    "bmanager": "manager name",
    "academic_year": "20120801",
    "division": "division name",
    "manager_id": "3"
}]

各日付の 1 つの行に対して 3 つの JSON が返されます。

[{
    "user": "378",
    "date": "UNIX_TIMESTAMP(1st date)",
    "title": "name",
    "description": "6 Days",
    "url": "UNIX_TIMESTAMP(1st date)",
    "bmanager": "manager name",
    "academic_year": "20120801",
    "division": "division name",
    "manager_id": "3"
}, {
    "user": "378",
    "date": "UNIX_TIMESTAMP(2nd date)",
    "title": "name",
    "description": "6 Days",
    "url": "UNIX_TIMESTAMP(2nd date)",
    "bmanager": "manager name",
    "academic_year": "20120801",
    "division": "division name",
    "manager_id": "3"
}, {
    "user": "378",
    "date": "UNIX_TIMESTAMP(3rd date)",
    "title": "name",
    "description": "6 Days",
    "url": "UNIX_TIMESTAMP(3rd date)",
    "bmanager": "manager name",
    "academic_year": "20120801",
    "division": "division name",
    "manager_id": "3"
}, ]

JSON

<?php
    header('Content-type: text/json');

    $lmanager = $_GET["lmanager"];

    $sqldata = mysql_query('
    SELECT
    requests.user,
    UNIX_TIMESTAMP(requests.start_date) AS date,
    requests.employee AS title, 
    requests.days AS description,
    UNIX_TIMESTAMP(requests.end_date) AS url,
    business.line_manager AS bmanager,
    requests.academic_year,
    business.academic_year,
    business.division,
    line_managers.userid AS manager_id
    FROM requests 
    INNER JOIN holiday_entitlement_business_manual AS business ON requests.user=business.userid AND requests.academic_year=business.academic_year
    INNER JOIN line_managers ON business.line_manager=line_managers.name
    WHERE requests.approved = 1 AND line_managers.userid = '.$lmanager.'
    ');

    $posts = array();

    while($row = mysql_fetch_assoc($sqldata))
    {
        $row['date'] = $row['date'].'000';
        $row['url'] = $row['url'].'000';
        $row['description'] = $row['description'].' Days';
        $posts[] = $row;
    }

    mysql_free_result($sqldata);
    die(json_encode($posts));
?>
4

1 に答える 1

1

これをSQL選択クエリに追加します:

select datediff(requests.end_date, requests.start_date) as numdays

次に、次のようにします。

while($row = mysql_fetch_assoc($sqldata))
{
    for($i = 0; $i < $row['numdays']; $i++){
        $row['date'] = $row['date']+$i*24*60*60; // add 24 hours in seconds to the start_date stamp
        $posts[] = $row;
    }
}

微調整が必​​要かもしれませんが、

于 2012-09-18T16:04:17.823 に答える