2016-11-12 2 views
0

Ich baue ein Forum für Lernzwecke. Ich entferne Forumkategorien erfolgreich aus der Datenbank und zeige sie in einer Tabelle an, aber nur die erste Kategorie wird in der Tabelle angezeigt, der Rest wird außerhalb der Tabelle angezeigt. Ich werde meinen Code und einen Screenshot des Bildes posten.Nur das Ergebnis der PHP-Abfrage wird in der HTML-Tabelle angezeigt, der Rest wird außerhalb angezeigt

<?php 
include 'connect.php'; 
include 'header.php'; 

$sql = "SELECT categories.cat_id,categories.cat_name, 
       categories.cat_description, COUNT(topics.topic_id) AS topics 
     FROM categories 
      LEFT JOIN topics ON topics.topic_id = categories.cat_id 
     GROUP BY categories.cat_name, categories.cat_description, 
       categories.cat_id"; 

$result = mysql_query($sql); 

if(!$result) { 
    echo 'The categories could not be displayed, please try again later.'; 
} else { 
    if(mysql_num_rows($result) == 0) { 
     echo 'No categories defined yet.'; 
    } else { 
     //prepare the table 
     echo ' 
      <div class="container"> 
      <table class="table forum tale-striped table-hover"> 
      <thead> 
      <tr> 
       <th class="cell-stat"></th> 
       <th><h3>Category</h3></th> 
       <th><h3>Last topic</h3></th> 
      </tr> 
      </thead>'; 

     while($row = mysql_fetch_assoc($result)) {   
      echo '<tbody>'; 
      echo '<tr >'; 
      echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger">  </i></td>'; 
      echo '<td><h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'].'</td>'; 

      echo '<td class="float-xs-right">'; 

      //fetch last topic for each cat 
      $topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat 
         FROM topics 
         WHERE topic_cat = " . $row['cat_id'] . " 
         ORDER BY topic_date DESC 
         LIMIT 1"; 

      $topicsresult = mysql_query($topicsql); 

      if(!$topicsresult) { 
       echo 'Last topic could not be displayed.'; 
      } else { 
       if(mysql_num_rows($topicsresult) == 0) { 
        echo 'no topics'; 
       } else { 
        while($topicrow = mysql_fetch_assoc($topicsresult)) 
         echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date'])); 
        } 
       } 
       echo '</td>'; 
       echo '</tr>'; 
       echo '</tbody>'; 
       echo '</table>'; 
       echo '</div>'; //container 
      } 
     } 
    } 

include 'footer.php'; 
?> 

https://www.dropbox.com/s/c1dgijuafgv9jzu/Capture.PNG?dl=0

+0

Jedes Mal, wenn Sie [die 'mysql_'] (http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-Funktionen-in-php) Datenbankerweiterung im neuen Code ** [ein Kätzchen wird irgendwo in der Welt erdrosselt] (http://2.bp.blogspot.com/- zCT6jizimfI/UjJ5UTb_Bei/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg) ** es ist veraltet und ist seit Jahren und ist für immer in PHP7 gegangen. Wenn du nur PHP lernst, verbringe deine Energie damit, die Datenbankerweiterungen 'PDO' oder' mysqli' zu lernen. [Hier starten] (http://php.net/manual/en/book.pdo.php) – RiggsFolly

+0

Einige sinnvolle Code-Einzug wäre eine gute Idee. Es hilft uns, den Code zu lesen und, was noch wichtiger ist, es hilft Ihnen, Ihren Code zu debuggen. ** [Werfen Sie einen Blick auf einen Kodierungsstandard] (http://www.php-fig.org/psr/psr-2/) zu Ihrem eigenen Vorteil. Sie werden möglicherweise aufgefordert, diesen Code in ein paar Wochen/Monaten zu ändern, und Sie werden mir am Ende danken. – RiggsFolly

Antwort

0

Ganz einfach haben Sie die schließende </table> Tag innerhalb der While-Schleife, also sobald die erste Zeile ausgegeben wird, schließen Sie die Tabelle. bewegen Sie sich einfach außerhalb der while-Schleife wie diese, auch die Öffnung <tbody> Bedarf über der while-Schleife zu bewegen als auch

<?php 
include 'connect.php'; 
include 'header.php'; 

$sql = "SELECT categories.cat_id,categories.cat_name, 
       categories.cat_description, COUNT(topics.topic_id) AS topics 
     FROM categories 
      LEFT JOIN topics ON topics.topic_id = categories.cat_id 
     GROUP BY categories.cat_name, categories.cat_description, 
       categories.cat_id"; 

$result = mysql_query($sql); 

if(!$result) { 
    echo 'The categories could not be displayed, please try again later.'; 
} else { 
    if(mysql_num_rows($result) == 0) { 
     echo 'No categories defined yet.'; 
    } else { 
     //prepare the table 
     echo ' 
      <div class="container"> 
      <table class="table forum tale-striped table-hover"> 
      <thead> 
      <tr> 
       <th class="cell-stat"></th> 
       <th><h3>Category</h3></th> 
       <th><h3>Last topic</h3></th> 
      </tr> 
      </thead> 
      <tbody>'; //<-- moved 

     while($row = mysql_fetch_assoc($result)) {   

      echo '<tr >'; 
      echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger">  </i></td>'; 
      echo '<td><h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'].'</td>'; 

      echo '<td class="float-xs-right">'; 

      //fetch last topic for each cat 
      $topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat 
         FROM topics 
         WHERE topic_cat = " . $row['cat_id'] . " 
         ORDER BY topic_date DESC 
         LIMIT 1"; 

      $topicsresult = mysql_query($topicsql); 

      if(!$topicsresult) { 
       echo 'Last topic could not be displayed.'; 
      } else { 
       if(mysql_num_rows($topicsresult) == 0) { 
        echo 'no topics'; 
       } else { 
        while($topicrow = mysql_fetch_assoc($topicsresult)) 
         echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date'])); 
        } 
       } 
       echo '</td>'; 
       echo '</tr>'; 
      } 
     } 
     echo '</tbody>';   //<-- moved 
     echo '</table>';   //<-- moved 
     echo '</div>'; //container //<-- moved 

    } 

include 'footer.php'; 
?> 
0

Becuse Sie die <table> Tag im while -loop sind zu schließen. Auch sollte die <tbody> außerhalb der Schleife sein, da es nur eine <tbody> in einer Tabelle wie dieser geben sollte.

Verwandte Themen