-1

開始日と終了日を持つ mysql のテーブルがあります。

1行の例は次のとおりです。

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

これは(OUTPUT!!!)を返します:

[{"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"}]

このレコードは 3 日間にわたるため、JSON で日付ごとに 1 つを出力したいと考えています (唯一の違いは日付です。以下を参照してください)

[{"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"},]

これは私が使用しているスクリプトです:

$sqldata = mysql_query('
SELECT
datediff(requests.end_date, requests.start_date) as numdays,
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
');

$posts = array();

while($row = mysql_fetch_assoc($sqldata))
{
    $row['date'] = $row['date'].'000';
    $row['url'] = $row['url'].'000';
    $posts[] = $row;
}
mysql_free_result($sqldata);
die(json_encode($posts));
4

2 に答える 2

2

コードを次のように置き換えてみてください。

$sqldata = mysql_query('
SELECT
requests.start_date as startdate,
datediff(requests.end_date, requests.start_date) as numdays,
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
');

$posts = array();

while($row = mysql_fetch_assoc($sqldata))
{
   $startDate = $row['startdate'];
   unset($array['startdate']);
   $dayDiff = $row['numdays'];
   $row['date'] = $row['date'].'000';
   $row['url'] = $row['url'].'000';
   for($i = 0; $i <= $dayDiff; $i++)
   {
      $row['date'] = ...; // Do here the date parsing and add $i to the days
      $posts[] = $row;
   }

}
mysql_free_result($sqldata);
die(json_encode($posts));

これらの関数dateparse()およびmktime()は、おそらく日付操作に役立ちます。

于 2013-03-05T17:54:39.167 に答える
0

私があなたの主張を理解したかどうかはわかりません。これが出発点になります。基本的に、すべての行をフェッチしてから、考慮したい日ごとに 1 つのレコードを作成する必要があります。

 $sqldata = mysql_query('
    SELECT
    datediff(requests.end_date, requests.start_date) as numdays,
    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
    ');

$posts = array();

while($row = mysql_fetch_assoc($sqldata))
{
    $row['date'] = $row['date'];
    $row['url'] = $row['url'];
    for($x = $row['date']; $x < $row['url']; $x+= 86400) {
          // where 86400 makes you advancing by a day in secs
          $row['date'] = $x;
          $row['url'] = $x;
          $posts[] = $row;
    }
    $posts[] = $row;
}
mysql_free_result($sqldata);
die(json_encode($posts));
于 2013-03-05T17:56:17.060 に答える