0

PHPを介してmysqlテーブルからセクションセパレーターを使用してソートされたリストを実現しようとしていますが、少し問題があります。私が望むようにスープをスープヘッダーに入れる代わりに、私はそれを次のヘッダーに入れています(望まないように)!

視覚的な出力は次のとおりです。

2-Soup
3-Salad
    2   Soup     Demo: Tom KaKai
4-Entree
    3   Salad    Demo: Spinach Salad
    4   Entree   Demo: Pork Chop
    4   Entree   Demo: Spicy Topped Shrimp

ここに私のコードがあります:

 $cat = null;
while($row = mysql_fetch_array($result))
{
    if( $row['catnum'] != $cat )
    {
        echo '<h1>' . $row['catnum'] . '-' . $row['ctgry'] . '</h1>';
        $cat = $row['catnum'];
    }
   echo "<table><tr><td>" . $row['catnum'] . " </td><td>" . $row['ctgry'] . "</td><td> " . $row['Shrt_Desc'] . "</td></tr>";
}
4

1 に答える 1

2

タグを閉じるのではなく<table>、取得した DB 行ごとに新しいテーブルを開始しているため、構造的に HTML ページは完全に混乱しています。

次のようなものが必要です。

$first = true;
while ($row = mysql_fetch_array($result)) {
    if ($row['catnum'] != $cat) {
       if (!$first) {
           echo '</table>'; // close the table if we're NOT the first row being output.
       }

       $first = false; // no longer the first table, so disable this check.
       echo '<h1> etc...';
       echo '<table>'; // start new table
       $cat = $row['catnum'];
    }
    echo '<tr><td>' etc.... // output a table row
}

このコードは、カテゴリを変更したときにのみ<table>and > のものを出力します。<h1

于 2012-04-09T16:28:20.903 に答える