简体   繁体   English

从MySQL数据库显示PHP中的类别和子类别

[英]Displaying Categories and Subcategories in PHP from MySQL database

I'm trying to display a list of categories and subcatagories on a website that are populated from a MySQL database and I'm a bit rusty with how to do it properly. 我试图显示从MySQL数据库填充的网站上的类别和子类别的列表,但对于如何正确执行操作,我有些生疏。

Basically I want to achieve this: 基本上我想做到这一点:

Parent Cat 1 
--Child Cat1
--Child Cat2
Parent Cat 2
Parent Cat 3
--Child Cat 1
--Child Cat 2

and the database is layed out: 并布置数据库:

ParentCat1,   ChildCat1,   Item1
ParentCat1,   ChildCat1,   Item2
ParentCat1,   ChildCat2,   Item1
ParentCat2,                Item1
ParentCat3,   ChildCat1,   Item1
ParentCat3,   ChildCat2,   Item2

EDIT this is what I have so far big thanks to Gowtham: 编辑,这是我迄今为止对Gowtham的最大感谢:

<?php
    $conn = mysql_connect("localhost", "USER", "PASS");

    if (!$conn) {
        echo "Unable to connect to DB: " . mysql_error();
        exit;
    }

    if (!mysql_select_db("DB-Store")) {
        echo "Unable to select DB-Store: " . mysql_error();
        exit;
    }

    $sql = "SELECT * FROM menu";

    $result = mysql_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print";
        exit;
    }

    $result = mysql_query($sql);
    $menu = array();
    echo "Start of Array";
    echo "<br>";
    while ($row = mysql_fetch_assoc($result)) {
        $menu['category'][] = $result['cat'];
        if (!empty($result['subcat']))
        $menu['subcat'][$result['cat']][] = $result['subcat'];          
    }

    foreach ($menu['category'] as $cat) {
        echo $cat."<br>";
        foreach ($menu['subcat'][$cat] as $subcat) {
            echo "--" . $subcat."<br>";
        }
    }
    echo "<br>";
    echo "End of Array";
    mysql_free_result($result);
?>

The first thing I will do to achieve it is, getting value from the database and store it in the array and run a loop to print the array. 我要做的第一件事是,从数据库中获取价值并将其存储在数组中,然后运行循环以打印数组。

Here is my code. 这是我的代码。 Let's consider category field name is cat and subcategory field name is subcat and table name is menu, then 让我们考虑类别字段名称为cat,子类别字段名称为subcat,表名称为menu,则

<?php

$sql = 'select * from menu';
$con = mysqli_connect("localhost", "my_user", "my_password", "my_db");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Perform queries
$result = mysqli_query($con, $sql);

$menu = array();
while ($row = mysqli_fetch_assoc($result)) {
    if (!in_array($row['cat'], $menu['category'])) {

            $menu['category'][] = $row['cat'];
    }
    if (!empty($row['subcat']))
        $menu['subcat'][$row['cat']][] = $row['subcat'];
}

foreach ($menu['category'] as $cat) {
    echo $cat."<br>";
    foreach ($menu['subcat'][$cat] as $subcat) {
        echo "--" . $subcat."<br>";
    }
}
?>

And the above program will satisfy your need. 以上程序将满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM