2017-03-13 3 views
0

UPDATE - Added AbfrageGruppe Zeilen aus SQL-Datenbank nach Datum in Tabellen

ich ein System entwickle, die Ergebnisse von Spielen aus einer MySQL-Datenbank sammelt. Ich möchte, dass die Ergebnisse nach dem Datum gruppiert werden (z. B. werden die Ergebnisse eines bestimmten Datums in einer Tabelle angezeigt).

Ich bin fast da, aber ich kämpfe darum, dass die Ergebnisse vom selben Datum als nächste Zeile der Tabelle angezeigt werden.

You can see the current output here.

Hier ist der Code, wie es steht:

$result = mysqli_query($con, "SELECT `MatchDate`, t1.`TeamName` AS HomeTeam, `MatchHomeScore`, `MatchAwayScore`, t2.`TeamName` AS AwayTeam `FROM `match` INNER JOIN `team` t1 ON `match`.`MatchHomeTeamID` = t1.`TeamID` INNER JOIN `team` t2 ON `match`.`MatchAwayTeamID` = t2.`TeamID` ORDER BY `MatchDate` ASC ");` 

$currentDate = false; 
while($row = mysqli_fetch_array($result)) 
{ 
    echo '<div class="col-lg-4">'; 
     echo '<div class="panel panel-default-fixtures">'; 
      if ($row['MatchDate'] != $currentDate){ 
      echo '<div class="panel-heading">'; 
       echo '<h4 class="panel-title">'; 
        echo $row['MatchDate']; 
       echo '</h4>'; 
      echo '</div>'; 
     $currentDate = $row['MatchDate']; 
    } 
    echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">'; 
       echo '<thead>'; 
        echo '<tr>'; 
         echo '<th class="all">'; 
          echo 'Home'; 
         echo '</th>'; 
         echo '<th colspan="2">'; 
          echo 'Score'; 
         echo '</th>'; 
         echo '<th class="all">'; 
          echo 'Away'; 
         echo '</th>'; 
        echo '</tr>'; 
       echo '</thead>'; 
       echo '<tbody>';           
        echo '<tr>'; 
         echo '<td>'.$row['HomeTeam'].'</td>'; 
         echo '<td>'.$row['MatchHomeScore'].'</td>'; 
         echo '<td>'.$row['MatchAwayScore'].'</td>'; 
         echo '<td>'.$row['AwayTeam'].'</td>'; 
        echo '</tr>';          
       echo '</tbody>'; 
      echo '</table>'; 
     echo '</div>'; 
    echo '</div>'; 
} 

2. Update

Code verwendet Shamil Omarov zur Verfügung gestellt und fixiert das Problem mit ein paar Änderungen. Hier ist der Arbeitscode:

$result = mysqli_query($con, " 
         SELECT `MatchDate`, t1.`TeamName` AS HomeTeam, `MatchHomeScore`, `MatchAwayScore`, t2.`TeamName` AS AwayTeam 
         FROM `match` 
         INNER JOIN `team` t1 ON `match`.`MatchHomeTeamID` = t1.`TeamID` 
         INNER JOIN `team` t2 ON `match`.`MatchAwayTeamID` = t2.`TeamID` 
         -- WHERE YEAR(`MatchDate`) =2017 
         ORDER BY `MatchDate` ASC "); 

$currentDate = false; 
while($row = mysqli_fetch_array($result)) 
{         

    if ($row['MatchDate'] != $currentDate){ 

     if ($currentDate != false){ 
      echo '</tbody>'; 
      echo '</table>'; 
      // /.table-responsive 
      echo '</div>'; 
      // /.panel 
      echo '</div>'; 
      // /.col-lg-4 
     } 

     echo '<div class="col-lg-4">'; 
     echo '<div class="panel panel-default-fixtures">'; 
     echo '<div class="panel-heading">'; 
     echo '<h4 class="panel-title">'; 
     echo $row['MatchDate']; 
     echo '</h4>'; 
     echo '</div>';     
     echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">'; 
     echo '<thead>'; 
     echo '<tr>'; 
     echo '<th class="all">'; 
     echo 'Home'; 
     echo '</th>'; 
     echo '<th colspan="2">'; 
     echo 'Score'; 
     echo '</th>'; 
     echo '<th class="all">'; 
     echo 'Away'; 
     echo '</th>'; 
     echo '</tr>'; 
     echo '</thead>'; 
     echo '<tbody>';  
     $currentDate = $row['MatchDate']; 
    }        

    if ($row['MatchHomeScore'] != null && $row['MatchAwayScore'] != null){ 
     echo '<tr class="success">'; 
    } else { 
     echo '<tr>'; 
    } 
    echo '<td>'.$row['HomeTeam'].'</td>'; 
    echo '<td>'.$row['MatchHomeScore'].'</td>'; 
    echo '<td>'.$row['MatchAwayScore'].'</td>'; 
    echo '<td>'.$row['AwayTeam'].'</td>'; 
    echo '</tr>'; 
    } 

Dieser neue Code jetzt gibt:

Antwort

1

überprüfen Sie dies.

$currentDate = false; 
    while($row = mysqli_fetch_array($result)) 
    { 
     echo '<div class="col-lg-4">'; 
     echo '<div class="panel panel-default-fixtures">'; 

     if ($row['MatchDate'] != $currentDate){ 

     if($currentDate != false){ 
      echo '</tbody>'; 
      echo '</table>'; 
      echo '</div>'; 
      echo '</div>'; 
     } 

     echo '<div class="panel-heading">'; 
     echo '<h4 class="panel-title">'; 
     echo $row['MatchDate']; 
     echo '</h4>'; 
     echo '</div>'; 
     echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">'; 
     echo '<thead>'; 
     echo '<tr>'; 
     echo '<th class="all">'; 
     echo 'Home'; 
     echo '</th>'; 
     echo '<th colspan="2">'; 
     echo 'Score'; 
     echo '</th>'; 
     echo '<th class="all">'; 
     echo 'Away'; 
     echo '</th>'; 
     echo '</tr>'; 
     echo '</thead>'; 
     echo '<tbody>'; 
     $currentDate = $row['MatchDate']; 
    } 


    echo '<tr>'; 
    echo '<td>'.$row['HomeTeam'].'</td>'; 
    echo '<td>'.$row['MatchHomeScore'].'</td>'; 
    echo '<td>'.$row['MatchAwayScore'].'</td>'; 
    echo '<td>'.$row['AwayTeam'].'</td>'; 
    echo '</tr>'; 

} 
+0

Vielen Dank für Ihre Hilfe Shamil, ich habe ein Update zum Post mit dem neuen Code und der Ausgabe hinzugefügt. – AdDaws

+0

Willkommen @AdDaws :) –

0

Ich glaube, Sie müssen die Abfrage ändern. Können Sie es zeigen, um herauszufinden, was geändert werden muss?

+0

Die Abfrage hinzugefügt, obwohl ich nicht sicher bin, dass das Problem ist. Ich glaube, es hat etwas mit der Positionierung der if-Anweisung zu tun. – AdDaws