Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Kategorisieren von MySQL-Daten in separate HTML-Tabellen?

Der HTML-Code ist deaktiviert, aber das sollte Ihnen den Einstieg erleichtern:

<?php

$query = 'SELECT CategoryID, CategoryName, ItemID, ItemName
FROM tableName
ORDER BY CategoryID';

$result = mysql_query($query);
if (!$result || mysql_num_rows($result) == 0) {
    echo "No rows found";
    exit;
}

$lastCatID = 0; //or some other invalid category ID

while ($row = mysql_fetch_assoc($result)) {
    if($lastCatID != $row['CategoryID']) {
        //starting a new category
        if($lastCatID != 0) {
            //close up previous table
            echo '</table>';
        }

        //start a new table
        echo '<table><th><td colspan="2">Category '
                . $row['CategoryName'] .'</td></th>';
        $lastCatID = $row['CategoryID'];
    }

    echo '<tr><td>' . $row['ItemName'] . '</td><td></td></tr>';
}

if($lastCatID != 0) {
    //close up the final table
    echo '</table>';
}

mysql_free_result($result);
?>