猿问

制作表格数据多维数组

我得到的结果有问题,我尝试从我的数据库中获取数据,我想让所有数据都是动态的,所以对于“header”表我必须从数据库中获取它,而对于“body”我想要从数据库中获取它也取决于“header_id”。得到的“body”的结果应该是从左到右,而不是从上到下。

你可以看到我的结果是这样的

但这应该是这样的

http://img.mukewang.com/61a087a90001d70705150111.jpg

有人能帮我吗?这是我的视图代码。


<table class="table table-striped table-bordered zero-configuration">

    <thead>

        <tr>

        <?php

            foreach ($du_head as $key => $value) {

                echo "<th>".$value->duh_judul."</th>";

            }


         ?>

             th>Aksi</th>

         </tr>

     </thead>


     <tbody>

         <?php foreach ($du_head as $key => $value) { ?>

         <tr>

         <?php

             $q = $this->db->query("select * from data_umum where duh_id = '$value->duh_id'")->result();

                 foreach ($q as $ac) {

                     echo "<td>".$ac->duh_isi."</td>";

                     echo "<td><i class='fa fa-trash'><a href=''></a></i></td>";

                 }

             ?>

         </tr>

     <?php } ?>

  </tbody>

</table>

这是我的数组结果 du_head


array(3) {

  [0]=>

  object(stdClass)#38 (3) {

    ["duh_id"]=>

    string(1) "2"

    ["maplink_id"]=>

    string(1) "9"

    ["duh_judul"]=>

    string(10) "Nama Jalan"

  }

  [1]=>

  object(stdClass)#39 (3) {

    ["duh_id"]=>

    string(1) "3"

    ["maplink_id"]=>

    string(1) "9"

    ["duh_judul"]=>

    string(13) "Panjang Jalan"

  }

  [2]=>

  object(stdClass)#40 (3) {

    ["duh_id"]=>

    string(1) "4"

    ["maplink_id"]=>

    string(1) "9"

    ["duh_judul"]=>

    string(5) "Lebar"

  }

}


陪伴而非守候
浏览 195回答 2
2回答

12345678_0001

因为你希望每个排须是一个新的表列,你会希望把在foreach内部的<td>,而不是<tr>。此外,如果您已经du_head从查询中获取信息,则您可能不需要在 foreach 中执行该查询。duh_isi如果是同一个表的一部分,您可以只添加到选择中,或者如果不只是进行连接。此外,您应该使用 PHP 框架。这将允许您将数据库查询逻辑与 html 模板逻辑分开,并提供一个 ORM 来清理您的数据库输入,这两者都是安全和可维护代码的关键。

森栏

您的列和行循环逻辑混淆了,首先尝试将其映射到数组中:$rows = [];$additional_column = count($du_head); // count the columns to get the last column index for displaying the delete buttonforeach ($du_head as $colkey => $value) {&nbsp;&nbsp; &nbsp; $q = $this->db->query("select * from data_umum where duh_id = '$value->duh_id'")->result();&nbsp; &nbsp; foreach ($q as $rowkey => $ac) {&nbsp; &nbsp; &nbsp; &nbsp; $rows[$rowkey][$colkey] = "<td>" . $ac->duh_isi . "</td>";&nbsp; &nbsp; }&nbsp; &nbsp; $rows[$colkey][$additional_column] = "<td>" . $ac->duh_isi . "</td>";}然后你可以foreach再次循环显示行:foreach ($rows as $rowkey => $row) {&nbsp; &nbsp; echo "<tr>"; // print each table row&nbsp; &nbsp; foreach ($row as $colkey => $col) {&nbsp; &nbsp; &nbsp; &nbsp; echo $col; // print each table column&nbsp; &nbsp; }&nbsp; &nbsp; echo "<td><i class='fa fa-trash'><a href='" . site_url("controller/delete/$rowkey") . "'></a></i></td>"; // print each delete button, adjust it to whatever you need&nbsp; &nbsp; echo "</tr>\r\n";}把它放在一起,它会像:<table class="table table-striped table-bordered zero-configuration">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($du_head as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>" . $value->duh_judul . "</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Aksi</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $rows = [];&nbsp; &nbsp; &nbsp; &nbsp; $additional_column = count($du_head); // count the column total to get the last column index for displaying the delete button&nbsp; &nbsp; &nbsp; &nbsp; foreach ($du_head as $colkey => $value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $q = $this->db->query("select * from data_umum where duh_id = '$value->duh_id'")->result();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($q as $rowkey => $ac) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rows[$rowkey][$colkey] = "<td>" . $ac->duh_isi . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rows[$colkey][$additional_column] = "<td>" . $ac->duh_isi . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach ($rows as $rowkey => $row) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>"; // print each table row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($row as $colkey => $col) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $col; // print each table column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td><i class='fa fa-trash'><a href='" . site_url("controller/delete/$rowkey") . "'></a></i></td>"; // print each delete button, adjust it to whatever you need&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>\r\n";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </tbody></table>但这绝对很奇怪,因为您在循环内和模板视图内混合了查询,您应该考虑使用 PHP 框架来防止进一步的挫折。
随时随地看视频慕课网APP
我要回答