簡體   English   中英

將 Array 數據轉換為 html 表

[英]Convert Array data to html table

我從 json 文件中獲得的數組數據存在一些問題,但我想問題可能與我使用 json 解碼和 file_get_contents 轉換為數組的 json 結構有關。

基本上這是數組結構:

#items: array:4 [▼
    "Monday, 1 de Fev de 2021" => array:5 [▼
      "PM" => array:7 [▼
        1 => "1140-10"
        2 => "8498-25"
        3 => "7076-19"
        4 => "3380-20"
        5 => "8194-24"
        6 => "8288-22"
        7 => "687-22"
      ]
      "PT" => array:7 [▼
        1 => "6406-2"
        2 => "2976-19"
        3 => "6029-8"
        4 => "8130-8"
        5 => "7530-8"
        6 => "1071-18"
        7 => "064-16"
      ]
      "PTV" => array:7 [▶]
      "PTN" => array:7 [▶]
    ]
    "Sat, 31 de Jan de 2021" => array:2 [▼
      "PTM" => array:7 [▶]
      "PT" => array:7 [▶]
    ]

基本上日期是表格標題,帶有字母的索引,例如:“PTV”,“PM”,“PT”...”,是標題。

例如,在“PM”內部有一些結果:

 1 => "1140-10"
 2 => "8498-25"
 3 => "7076-19"
 4 => "3380-20"
 5 => "8194-24"
 6 => "8288-22"
 7 => "687-22"

其中索引 (1,2,3..) 是獎品,每個索引后面的值是結果。

我需要將我的表格構建成上面這張圖片

在此處輸入圖像描述

這是我的代碼:

@if(sizeof($results) > 0)
  @foreach($results as $date => $result)
    <div class="col-xl-12">
      <table class="table">
        <caption>
          {{ $date }}
        </caption>
        <thead>
          <tr>
            <th id="hoje" class="tabla-header"></th>
            @foreach($result as $banca => $re)
              <th id="{{ $banca }}" class="tabla-header">{{ $banca }}</th>
            @endforeach
          </tr>
        </thead>
        <tbody>
          {{ sizeof($result) }}
          @for ($i = 0; $i < 7; $i++)
            <tr>
              <td>1</td>
              @for ($n = 0; $n < sizeof($result); $n++)
                <td>{{ $result[] }}/td>
              @endfor
            </tr>
          @endfor
        </tbody>
      </table>
    </div>
  @endforeach
@endif

創建數據庫並插入表信息。 接下來使用控制台 make:controller // 制作 controller

制作方法...(數據庫的select信息)

          $var1 = DB::table("table_name")->get('column_1');// first use DB namespace
          $var2 = DB::table("table_name")->get('column_2');
          $var2 = $var2[
          // and other get columns table
          return view('view_name',['tr1' => $var1,'tr2' => $var2 /* and etc */);
}

並且視圖在代碼下

<!DOCTYPE htl>
<html>
<table class="table"> <!-- table class ; you create a table class in your code -->
<tr>
<th>{{$var1[0][0]->column_name}}</th>
<th>{{$var1[0][1]->column_name}}</th>
<th>{{$...}}</th>
</tr>
<tr>
<th> {{$avr2[0][0]->column_name}} </th>
<th> {{$var2[0][1]->coumn_name}} </th>
<!-- and continue above code -->
</tr>
</table>
</html>

首先你 select 數據庫的信息並在視圖中顯示它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM