亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

制作表格數據多維數組

制作表格數據多維數組

PHP
陪伴而非守候 2021-11-26 15:06:34
我得到的結果有問題,我嘗試從我的數據庫中獲取數據,我想讓所有數據都是動態的,所以對于“header”表我必須從數據庫中獲取它,而對于“body”我想要從數據庫中獲取它也取決于“header_id”。得到的“body”的結果應該是從左到右,而不是從上到下。你可以看到我的結果是這樣的但這應該是這樣的有人能幫我嗎?這是我的視圖代碼。<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_headarray(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"  }}
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

因為你希望每個排須是一個新的表列,你會希望把在foreach內部的<td>,而不是<tr>

此外,如果您已經du_head從查詢中獲取信息,則您可能不需要在 foreach 中執行該查詢。duh_isi如果是同一個表的一部分,您可以只添加到選擇中,或者如果不只是進行連接。

此外,您應該使用 PHP 框架。這將允許您將數據庫查詢邏輯與 html 模板邏輯分開,并提供一個 ORM 來清理您的數據庫輸入,這兩者都是安全和可維護代碼的關鍵。


查看完整回答
反對 回復 2021-11-26
?
森欄

TA貢獻1810條經驗 獲得超5個贊

您的列和行循環邏輯混淆了,首先嘗試將其映射到數組中:


$rows = [];

$additional_column = count($du_head); // count the columns to get the last column index for displaying the delete button

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

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

    foreach ($q as $rowkey => $ac) {

        $rows[$rowkey][$colkey] = "<td>" . $ac->duh_isi . "</td>";

    }

    $rows[$colkey][$additional_column] = "<td>" . $ac->duh_isi . "</td>";

}

然后你可以foreach再次循環顯示行:


foreach ($rows as $rowkey => $row) {

    echo "<tr>"; // print each table row

    foreach ($row as $colkey => $col) {

        echo $col; // print each table column

    }

    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

    echo "</tr>\r\n";

}

把它放在一起,它會像:


<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 

        $rows = [];

        $additional_column = count($du_head); // count the column total to get the last column index for displaying the delete button

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

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

            foreach ($q as $rowkey => $ac) {

                $rows[$rowkey][$colkey] = "<td>" . $ac->duh_isi . "</td>";

            }

            $rows[$colkey][$additional_column] = "<td>" . $ac->duh_isi . "</td>";

        }


        foreach ($rows as $rowkey => $row) {

            echo "<tr>"; // print each table row

            foreach ($row as $colkey => $col) {

                echo $col; // print each table column

            }

            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

            echo "</tr>\r\n";

        }

        ?>

    </tbody>

</table>

但這絕對很奇怪,因為您在循環內和模板視圖內混合了查詢,您應該考慮使用 PHP 框架來防止進一步的挫折。


查看完整回答
反對 回復 2021-11-26
  • 2 回答
  • 0 關注
  • 229 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號