dongzhui9936 2019-05-15 10:39
浏览 49

PHP - 根据数据库字段创建DIV

I have two tables inside my database. One table defines my categories and the other table includes my entries. At my output I would like to show my entries within the correct category

TABLE 1:

+----+-------+------------------+
| id | name  | source_system_id |
+----+-------+------------------+
|  1 | Cat 1 |                1 |
|  2 | Cat 2 |                1 |
|  3 | Cat 3 |                1 |
|  4 | Cat 4 |                1 |
+----+-------+------------------+

TABLE 2:

+---------+---------+------+----------+--------+
| SEGMENT |  FIELD  | LENG | DATATYPE | REF_ID |
+---------+---------+------+----------+--------+
| Entry 1 | Field 1 |   12 | VARCHAR  |      1 |
| Entry 2 | Field 2 |   15 | VARCHAR  |      1 |
| Entry 3 | Field 3 |   18 | VARCHAR  |      3 |
| Entry 4 | Field 4 |   18 | VARCHAR  |      4 |
+---------+---------+------+----------+--------+

Output should look like:

+---------+-------+---------+---------+
|  Cat 1  | Cat 2 |  Cat 3  |  Cat 4  |
+---------+-------+---------+---------+
| Entry 1 |       |         |         |
| Entry 2 |       |         |         |
|         |       | Entry 3 |         |
|         |       |         | Entry 4 |
+---------+-------+---------+---------+

So the id from table 1 and the ref_id from table 2 refer to each other. I cannot do a JOIN statement, cause table two is just a temporary table. Anyway I try to fetch all data, which works and then I want to display them as you can see above. Here is my code:

if(mysqli_num_rows($sqlCheckReference) > 0) {

    $getAllSAPModules = $db_conn->getRows('dwh_metamasterdata','global_source_modules',array('where'=>array('source_system_id'=>1),'return_type'=>'all'));

    if(!empty($getAllSAPModules)) {
    print "<div class='row'>";
        foreach ($getAllSAPModules as $sortedModule) {
            print "<div class='col-md-2 col-xs-12 col-lg-2'>";
                print "".$sortedModule["name"]."";
                while ($res = mysqli_fetch_array($sqlCheckReference)) {
                    if ($res["REF_ID"]==$sortedModule["id"]) {
                        print "<tr>";
                            print "<td>".$res["SEGMENT"]."</td>";
                        print "</tr>";
                    }
                }
            print "</div'>";
        }
    print "</div>";
    }

}

I receive all information from my tables, this is working so far but my code seems to be wrong, because the entries are not shown, depending on their REF_ID=id

Can someone help me out what I am doing wrong here?

EDIT: As requested here my var_dump output from $getAllSAPModules:

array(6) {
  [0]=>
  array(6) {
    ["id"]=>
    string(1) "1"
    [0]=>
    string(1) "1"
    ["name"]=>
    string(22) "Cat 1"
    [1]=>
    string(22) "Cat 1"
    ["source_system_id"]=>
    string(1) "1"
    [2]=>
    string(1) "1"
  }
  [1]=>
  array(6) {
    ["id"]=>
    string(1) "2"
    [0]=>
    string(1) "2"
    ["name"]=>
    string(23) "Cat 2"
    [1]=>
    string(23) "Cat 2"
    ["source_system_id"]=>
    string(1) "1"
    [2]=>
    string(1) "1"
  }
  [2]=>
  array(6) {
    ["id"]=>
    string(1) "3"
    [0]=>
    string(1) "3"
    ["name"]=>
    string(19) "Cat 3"
    [1]=>
    string(19) "Cat 3"
    ["source_system_id"]=>
    string(1) "1"
    [2]=>
    string(1) "1"
  }
  [3]=>
  array(6) {
    ["id"]=>
    string(1) "4"
    [0]=>
    string(1) "4"
    ["name"]=>
    string(18) "Cat 4"
    [1]=>
    string(18) "Cat 4"
    ["source_system_id"]=>
    string(1) "1"
    [2]=>
    string(1) "1"
  }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器