php - 从 MySql 从表中获取数据作为主类别,另一个作为子类别

标签 php mysql mysqli

信息:

我有两张 table 1. 分级主要 2. 分级子

主要分级

+-------------+-------------------+
|   g_main_id |    g_main_name    |
+-------------+-------------------+
|           1 | Responsibilities  |
|           2 | Skills            |
|           3 | Efforts           |
+-------------+-------------------+

分级子

+-------------+---------------+--------------------+
|   g_sub_id  |   g_sub_name  |  main_element_id   |
+-------------+---------------+--------------------+
|           1 | Quality       | 1                  |
|           2 | Treatment     | 1                  |
|           3 | Equipment     | 2                  |
+-------------+---------------+--------------------+

我使用这段代码将它们连接在一起

$sql = "SELECT * FROM grading_sub s JOIN grading_main m ON s.main_element_id = m.g_main_id";

我想这样做:

获取: g_main_name(元素名称)作为标题

并获取子元素作为复选框

示例:输出

+-------------+----------------+-----------------------+
|   g_main_id |   g_main_name  |  g_sub_name           |
+-------------+----------------+-----------------------+
|           1 |Responsibilities| checkbox() Quality    |
|             |                | checkbox() Treatment  |
+-------------+---------------+------------------------+

+-------------+---------------+------------------------+
|   g_main_id |   g_main_name |  g_sub_name            |
+-------------+---------------+------------------------+
|           1 |   Skills      | checkbox() Equipment   |
|             |               |                        |
+-------------+---------------+------------------------+

我使用了这段代码:

$sql = "SELECT * FROM grading_sub s JOIN grading_main m ON s.main_element_id = m.g_main_id";
$run = mysqli_query($DBcon,$sql);
while($rows = mysqli_fetch_assoc($run)){
    echo '
        <div class="row">
             <div class="col-md-12 col-sm-12 col-xs-12 with-margin">
               '.$rows['g_main_name'].'
             </div>                                             
        </div>
                <div class="col-md-3 col-sm-4 col-xs-6 with-margin"> 
                    <input type="checkbox" class="flat" name="sub_elements[]" value="'.$rows['g_sub_id'].'"> '.$rows['g_sub_name'].'
                </div>
            ' ;

}

结果是这样的:

+-------------+----------------+-----------------------+
|   g_main_id |   g_main_name  |  g_sub_name           |
+-------------+----------------+-----------------------+
|           1 |Responsibilities| checkbox() Quality    |
|           1 |Responsibilities| checkbox() Treatment  |
+-------------+----------------+-----------------------+

所以问题是我想获取表 grading_main 的 g_main_name 作为标题,以及他们通过 main_element_id 和 g_main_id 加入的 grading_sub 表的所有 g_sub_name

谢谢,

最佳答案

首先也是最重要的:+1 通过添加表格和示例来改进问题!

如果我没理解错的话,你想显示一个标题,然后是属于该标题的 X 个复选框。

$sql = "SELECT * FROM grading_sub s JOIN grading_main m ON s.main_element_id = m.g_main_id";
$run = mysqli_query( $DBcon, $sql );

// First obtain all data and store it in an array.
while( $row = mysqli_fetch_assoc( $run ) ) {
    $data[ $row['g_main_name'] ][] = $row;
}


// Now use two loops to loop over the array.
// The first one loops over the headers
foreach( $data AS $g_main_name => $subItems ) {
    echo '
        <div class="row">
             <div class="col-md-12 col-sm-12 col-xs-12 with-margin">
               ' . $g_main_name . '
             </div>                                             
        </div>';
    foreach( $subItems AS $item ) {
    echo '
            <div class="col-md-3 col-sm-4 col-xs-6 with-margin"> 
                <input type="checkbox" class="flat" name="sub_elements[]" value="' . $item['g_sub_id'] . '"> '. $item['g_sub_name'] . '
            </div>';
}   }

注意:未经测试的代码。我希望你明白这一点。

关于php - 从 MySql 从表中获取数据作为主类别,另一个作为子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46254024/

相关文章:

php - mysql_real_escape_string 转换为 mysqli

php - MySQLi count(*)始终返回1

php - 从 MySQL 数据库选择性导出

c++ - 如何避免在重复更新时插入选择中的死锁情况?

php - PDOException SQLSTATE[HY000] [2002] 没有这样的文件或目录

php - 从数据库中获取一行并替换其中的一些内容

javascript - 使用 .htpasswd 锁定 .user.js 文件

php - 替换特定标签内的字符串

php - 事务隔离的需求和范围 - MySQL

Python 插入到 mysql 表不工作