Create Category Headings when displaying SQL data using PHP

与世无争的帅哥 提交于 2019-12-01 13:28:05

Initialize a category variable before you start fetching rows.

$category = null;

After you fetch each row, compare that row's category to the previous category. If it's different, output the header. Then that category becomes the new previous category.

while ($new_row = mysql_fetch_array($new_result)) {
    if ($new_row['category'] != $category) {
        echo "<h1>$new_row[category]</h1>";
        $category = $new_row['category'];
    }
    // ... rest of your while loop contents ...

By the way, the mysql extension has been deprecated for quite a while and was removed in the current version of PHP. You should look into updating your code to use mysqli or PDO instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!