0

データベースからの出力を期間にグループ化しようとしています。

例えば

2013年5月

一部のデータ

より多くのデータ

2013年6月

一部のデータ

一部のデータ

以下のコードはある程度機能しますが、各月に対して 1 行のデータしか配置されません。つまり、各月には複数のデータ行があります

誰でも理由を説明できますか?

  function show_records($mysql_link)
  {
   date_default_timezone_set('Europe/London');
   $today=date('Y/m/d');
   $future=date('Y-m-d', strtotime("+10 months", strtotime($today)));


   $q="SELECT number,startdate,traction,tourname,start,fares,tourcompany
   FROM specials
   WHERE startdate>='$today' AND startdate<='$future' AND steam='y'
   ORDER BY startdate";


   $r=mysqli_query($mysql_link,$q);
   $lastmonth="";
   if ($r)
   {


    echo "<Table id='customers'>
    <tr>
    <th>Date</th>
    <th>Locomotive</th>
    <th>Organiser</th>
    <th>Name</th>
    <th>Pick Up Points</th>
    <th>Destination</th>
    <th>Fares</th>
    </tr>";


    while ($row=mysqli_fetch_array($r,MYSQLI_ASSOC))
    {
     $parts=explode('-',$row['startdate']);
     $timestamp=strtotime($row['startdate']);
     $parts2=date('YM',$timestamp);

 if (empty($lastmonth)|| $lastmonth!=$parts2) 
     {

     if (!empty($lastmonth))
     {
      echo '</table>';
 }
  echo "<h1>$parts2</h1>";
  echo "<table id='customers'>";
  $lastmonth=$parts2;               


      echo "<tr>";
      echo "<td>".$parts2."</td>";
      echo "<td>".$row['traction']."</td>";
      echo "<td>".$row['tourcompany']."</td>";
      echo "<td>".$row['tourname']."</td>";
      echo "<td>".$row['start']."</td>";
      echo "<td>".$row['end']."</td>";
      echo "<td>".$row['fares']."</td>";
      echo "</tr>";

      }
     echo "</Table>";
     }

    }
   else {echo '<p>'.mysqli_error($mysql_link).'</p>' ;}
   }
   show_records($mysql_link);

   mysqli_close($mysql_link);

   ?>
4

2 に答える 2

1

の場合にのみ、行データを印刷しています$lastmonth != $parts2。代わりに次のコードを使用します。

if (empty($lastmonth) || $lastmonth != $parts2) {
    if (!empty($lastmonth)) {
        echo '</table>';
    }
    echo "<h1>$parts2</h1>";
    echo "<table>";
    $lastmonth = $parts2;
}
echo "<tr>";
echo "<td>".... // and so on
于 2013-11-02T15:16:46.573 に答える
0

あなたのループでは、$lastmonth 変数は初回のみ更新されます。次に、最初の更新後には決してない「空」になることだけを探しています。それを修正すれば、きっとうまくいくでしょう!

于 2013-11-02T15:17:23.597 に答える