【问题标题】:Create and display array from MySQL database从 MySQL 数据库创建和显示数组
【发布时间】:2017-06-16 08:24:36
【问题描述】:

我正在尝试修改一些代码,这些代码为数据库中的菜单创建对象数组和对象数组,我需要它来将 SubCategoryID 添加到数组中的每个项目

对于我的一生,我对如何做到这一点感到精神空白

<?php

include_once ('includes/sqlopen.php');


$sql = mysqli_query($conn, "SELECT CategoryName, SubcategoryName, 
                                   SubcategoryID 
                            FROM products 
                            GROUP BY SubcategoryID 
                            ORDER BY CategoryName");

$menu = array();


while ($row = mysqli_fetch_assoc($sql)) {
    if (!in_array($row['CategoryName'], $menu['category'])) {
        $menu['category'][] = $row['CategoryName'];
    }
    if (!empty($row['SubcategoryName']))
        $menu['SubcategoryName'][$row['CategoryName']][] = $row['SubcategoryName'];
}

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

include_once ('includes/sqlclose.php');
?>

基本上我想实现这个:

Parent Cat 1 (link to prodlist.php?id=100) 
--Child Cat1 (link to prodlist.php?id=103)
--Child Cat2 (link to prodlist.php?id=104)

Parent Cat 2 (link to prodlist.php?id=200)

Parent Cat 3 (link to prodlist.php?id=300)
--Child Cat1 (link to prodlist.php?id=301)
--Child Cat2 (link to prodlist.php?id=302)

数据库布局:

CategoryName  SubcategoryName ItemName SubCategoryID
ParentCat1,   ChildCat1,      Item1,   103
ParentCat1,   ChildCat1,      Item2,   103
ParentCat1,   ChildCat2,      Item1,   104
ParentCat2,                   Item1,   200
ParentCat3,   ChildCat1,      Item1,   301
ParentCat3,   ChildCat2,      Item2,   302

** 更新 - 感谢所有试图帮助我的人,我知道我可能不太擅长解释我需要帮助的内容..

我已经玩了一段时间了,它正在做我想做的事。我只是有一个小故障,如果 2 个不同的父类别具有相同名称的子类别,它会将两个子类别 ID 添加到数组中

$menu = array();


while ($row = mysqli_fetch_assoc($sql)) {

    if (!in_array($row['CategoryName'], $menu['category'])) {
            $menu['category'][] = $row['CategoryName'];
            }
    if (!empty($row['SubcategoryName']))
                $menu['SubcategoryName'][$row['CategoryName']][] = $row['SubcategoryName'];
                $menu['SubcategoryID'][$row['SubcategoryName']][] = $row['SubcategoryID'];

}

输出

--Accessories
id--766
id--243
id--992
id--871
id--977

Monitor Arms
--Spacedec
id--789
--Visidec
id--791
--Telehook
id--792
--Spacepole
id--804
--Accessory
id--866
--Monitor Accessories
id--990
id--584
--Stands
id--991
id--538

【问题讨论】:

  • 如果您告诉我们问题出在哪里,我们可以花更少的时间自己解决问题
  • 对于从数据库添加到数组的每个项目,我希望它添加 SubCategoryID 以便在打开菜单项时将其作为变量传递到下一页\跨度>
  • 您能否将 ParentCatid 添加到您的选择中并更新您的“数据库已铺设”部分。
  • 添加了更新.. 感谢大家的帮助.. 我知道当我需要帮助时我不擅长解释事情

标签: php mysqli


【解决方案1】:

首先,你可以改进你的数据库结构

如下:

id|category|parent_id

简单干净不是。

现在试试这个:

//your database records
$rows = [
['id'=>1,'category'=>'Indland','parent_id'=>0],
['id'=>2,'category'=>'Udland','parent_id'=>0],
['id'=>3,'category'=>'Sport','parent_id'=>0],
['id'=>4,'category'=>'Handbold','parent_id'=>3],
['id'=>5,'category'=>'Fodbold','parent_id'=>3],
['id'=>6,'category'=>'Sample','parent_id'=>4]
];

print_r(getList($rows));

// Recursive function
function getList($rows, $id=0){

   // Start a new list
   $newList = null;

   foreach($rows as $key => $row) {
      if ($row['parent_id'] == $id) {
        // Add an UL tag when LI exists
        $newList == null ? $newList = "<ul>" : $newList;
        $newList .= "<li>";
        $newList .= "<a href=\"\">{$row['category']}</a>";
        // Find childrens
        $newList .= getList(array_slice($rows,$key),$row['id']);
        $newList .= "</li>";
    }   
  }   
  // Add an UL end tag
  strlen($newList) > 0 ? $newList .= "</ul>" : $newList;
  return $newList;
}

这将导致:

【讨论】:

  • 如果我可以对数据库做点什么,那就太棒了,但是他们服务器上的供应商数据库每小时都会更改:(
  • @JustinBland - 好吧
【解决方案2】:

DB 结构是一个明显的问题,因为它使用的是固定结构。虽然这在某些情况下可能很有效,但它不容易扩展。实现您当前正在尝试实现的目标的方法是:

  1. 按 CategoryName 获取和分组所有项目(在代码中)(我希望类别名称是一个 slug?)
  2. 对于每个类别名称,显示所有带有 id 链接的子类别

翻译成代码是这样的(你当前的 SQL 查询没问题):

$menu = [];    

while ($row = mysqli_fetch_assoc($sql)) {
    if (!isset($menu[$row['CategoryName']])) {
        $menu[] = $row['CategoryName'];
    }
    if (!empty($row['SubCategoryName']))
        $menu[$row['CategoryName']][] = $row['SubcategoryName'];
    }
}

echo "Start of Array";
echo "<br>";
foreach ($menu as $cat => $catsubs) {
    echo $cat."<br>";
    foreach ($catsubs as $sub) {
        echo "--" . $sub."<br>";
    }
}
echo "<br>";
echo "End of Array";

【讨论】:

    【解决方案3】:

    好吧,伙计们,我想我有它..我把它贴出来,以便其他人可以从中受益。再次感谢所有帮助过我的人

    代码

    <?php
    
    include_once ('includes/sqlopen.php');
    
    
        $sql = mysqli_query($conn, "SELECT CategoryName, SubcategoryName, CategoryCode, SubcategoryID 
                                        FROM products GROUP BY SubcategoryID ORDER BY CategoryName");
    
        $menu = array();
    
    
        while ($row = mysqli_fetch_assoc($sql)) {
            // Creates First Level Array Items For Parent IDs 
            if (!in_array($row['CategoryName'], $menu['category'])) {
                    $menu['category'][] = $row['CategoryName'];
                    }
            if (!empty($row['SubcategoryName']))
                        // Creates Second Level Array for Child IDs
                        $menu['SubcategoryName'][$row['CategoryName']][] = $row['SubcategoryName'];
                        // Creates Third Level Array for Category IDs
                        $menu['SubcategoryID'][$row['SubcategoryName']][$row['CategoryName']][] = $row['SubcategoryID'];
    
        }
    
    
    echo "Start of Array";
        echo "<br>";
    
        foreach ($menu['category'] as $cat) {
           echo $cat."<br>";
            foreach ($menu['SubcategoryName'][$cat] as $subcat) {
                echo "--" . $subcat."<br>";
                foreach($menu['SubcategoryID'][$subcat][$cat] as $id)
                    echo "id--".$id."<br>";
    
    
            } echo "<br>";
        }
        echo "<br>";
        echo "End of Array";
    
    
    
    include_once ('includes/sqlclose.php');
    ?>
    
    <pre>
    <?php print_r ($menu); ?>
    </pre> 
    

    Foreach 循环输出

    CPU
    --AMD Socket FM2
    id--778
    --Mobile
    id--558
    --Intel Socket 1150 (4th Gen)
    id--825
    --Intel Socket 1151 (6th Gen Skylake)
    id--945
    --AMD Socket AM3
    id--693
    --Intel Xeon
    id--980
    --Intel Socket 2011 (3rd and 4th Gen)
    id--744
    --Intel Socket 1151 (7th Gen Kabylake)
    id--1021
    --AMD Socket AM4
    id--1023
    

    print_r 输出

     [AMD Socket FM2] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 778
                            )
    
                    )
    
                [Mobile] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 558
                            )
    
                    )
    
                [Intel Socket 1150 (4th Gen)] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 825
                            )
    
                    )
    
                [Intel Socket 1151 (6th Gen Skylake)] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 945
                            )
    
                    )
    
                [AMD Socket AM3] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 693
                            )
    
                    )
    
                [Intel Xeon] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 980
                            )
    
                    )
    
                [Intel Socket 2011 (3rd and 4th Gen)] => Array
                    (
                        [CPU] => Array
                            (
                                [0] => 744
                            )
    
                    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      • 2014-01-21
      • 2010-10-17
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多