首页 > 解决方案 > 我想循环创建基于 Select2 数据格式的分组数据

问题描述

我想通过 HardwareTypeCode 循环创建基于 select2 数据格式的分组数据。

我不知道如何处理 PHP。

SQL查询:

 SELECT historyhardwaretb.HardwareID,
    hardwaretb.HardwareCode,
    hardwaretb.IPAddress,
    hardwaretb.MacAddress,
    hardwaretypetb.HardwareTypeCode,
    historyhardwaretb.EmployeeID,
    historyhardwaretb.Description
    FROM historyhardwaretb
    INNER JOIN hardwaretb ON historyhardwaretb.HardwareID = hardwaretb.HardwareID
    INNER JOIN hardwaretypetb ON hardwaretb.HardwareTypeID = hardwaretypetb.HardwareTypeID

PHP:

while ($row = $sql->fetch_assoc()) {
   $jsonGroup[$i]['text'] = $row['HardwareTypeCode'];
}

预期的 select2 分组数据格式结果:

{
  "jsonGroup": [
    { 
      "text": "PC", 
      "children" : [
        {
            "id": 1,
            "text": "Option 1.1"
        },
        {
            "id": 2,
            "text": "Option 1.2"
        }
      ]
    },
    { 
      "text": "NB", 
      "children" : [
        {
            "id": 3,
            "text": "Option 2.1"
        },
        {
            "id": 4,
            "text": "Option 2.2"
        }
      ]
    }
  ],
  "pagination": {
    "more": true
  }
}

参考: https ://select2.org/data-sources/formats#grouped-data

标签: phpmysqlloopsjquery-select2

解决方案


推荐阅读