Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Categorizando dados mysql em tabelas html separadas?


O HTML está desativado, mas isso deve ajudar você a começar:
<?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);
?>