开发者

Problem to present data from SQL with multiple loops

开发者 https://www.devze.com 2023-02-06 12:48 出处:网络
I\'ve tried to make this work for 24 hours now, and now it feels like I\'m close to it! I would like to get date (column:datum) and location (column:plats) from MySQL, where my code looks like:

I've tried to make this work for 24 hours now, and now it feels like I'm close to it!

I would like to get date (column:datum) and location (column:plats) from MySQL, where my code looks like:

<?php

$sql = "SELECT datum FROM gigs GROUP BY DATE_FORMAT( datum, '%Y' ) ORDER BY datum DESC";
$result = mysql_query($sql);

 while($r = mysql_fetch_array($result)) {

  $date = $r['datum'];
  $date_new = new DateTime($date);
  $year = $date_new->format('Y');
  $month = $date_new->format('M');
  $day = $date_new->format('d');

  $sql2 = "SELECT * FROM gigs WHERE DATE_FORMAT( datum, '%Y' ) = $year ORDER BY datum ASC";
  $res开发者_如何转开发ult2 = mysql_query($sql2);

  echo "<tr height=20px><td></td><td align=center><b>".$year."</b></td></tr>";

  echo "<tr><td><b>".$month."</b></td></tr>";

  while($r2 = mysql_fetch_array($result2)) {

   $date2 = $r2['datum'];
   $date_new2 = new DateTime($date2);
   $year2 = $date_new2->format('Y');
   $month2 = $date_new2->format('M');
   $day2 = $date_new2->format('j');

 //echo "<b>Month: ".$month."</b>";
 //echo "<b>Month2: ".$month2."</b>";

    if($month != $month2) {
     echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month2."</b></td></tr>";
    }

    $month = $month2;

    echo "<tr class=giglist><td>".$day2."</td><td>".$r2['plats']."</td></tr>"; 



  }
    echo "</td></tr>";
 }

?>

This gives me:

2011
Jan
1. Location
2. Location
3. Location
...

Feb
1. Location
2. Location
3. Location
...
...
Dec
1. Location
2. Location
3. Location
...

2010
Dec

Jan
1. Location
2. Location
3. Location
...
Feb
1. Location
2. Location
3. Location
...
...
Dec
1. Location
2. Location
3. Location
...

I do NOT want the extra December written under "2010"...

I hope someone can help, cause I'm lost in my own code xD thanks!


Your code is to much complicated, you can do that with only one query, see this code

<?php 
$sql = 'SELECT
            *, 
            DATE_FORMAT( datum, "%Y" ) "year", 
            DATE_FORMAT( datum, "%M" ) "month", 
            DATE_FORMAT( datum, "%e" ) "day" 
        FROM gigs
        ORDER BY year DESC, month DESC, day ASC';

$year = null;
$month = null;
$stmt = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
    if ($year != $result['year']) {
        // Year changed
        $year = $result['year'];
        echo "<tr height=20px><td></td><td align=center><b>".$year."</b></td></tr>";
    }
    if ($month != $result['month']) {
        // Month changed
        $month = $result['month'];
        echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month."</b></td></tr>";
    }
    echo "<tr class=giglist><td>".$result['day']."</td><td>".$row['plats']."</td></tr>"; 
}


First of all, please please, I really recommend you to use the separation of concerns principle! Use a templating system, e.g. Smarty, http://www.smarty.net/ and use a database abstraction layer. Then your code will be MUCH more flexible, easier to read, etc. and then you wouldn't need to ask this question, and save lots and lots of time.

This said, you could try this (I didn't test it though..);

<?php

$sql = "SELECT datum FROM gigs GROUP BY DATE_FORMAT( datum, '%Y' ) ORDER BY datum DESC";
$result = mysql_query($sql);

while($r = mysql_fetch_array($result))
{
    $date = $r['datum'];
    $date_new = new DateTime($date);
    $year = $date_new->format('Y');
    $month = $date_new->format('M');
    $day = $date_new->format('d');

    $sql2 = "SELECT * FROM gigs WHERE DATE_FORMAT( datum, '%Y' ) = $year ORDER BY datum ASC";
    $result2 = mysql_query($sql2);

    echo "<tr height=20px><td></td><td align=center><b>".$year."</b></td></tr>";
    $first_month_printed = false;

    while($r2 = mysql_fetch_array($result2))
    {
        $date2 = $r2['datum'];
        $date_new2 = new DateTime($date2);
        $year2 = $date_new2->format('Y');
        $month2 = $date_new2->format('M');
        $day2 = $date_new2->format('j');

        if($month != $month2) {
            echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month2."</b></td></tr>";
        } else if (!$first_month_printed)
            echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month2."</b></td></tr>";
            $first_month_printed = true;
        }

        $month = $month2;
        echo "<tr class=giglist><td>".$day2."</td><td>".$r2['plats']."</td></tr>"; 
    }

    echo "</td></tr>";
 }

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号