【问题标题】:Convert Array data to html table将数组数据转换为 html 表
【发布时间】:2021-05-06 02:54:31
【问题描述】:

我从 json 文件中获得的数组数据存在一些问题,但我想问题可能在于我使用 json decode 和 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

【问题讨论】:

  • @for ($n = 0; $n &lt; sizeof($result); $n++) 如果您将此循环更改为 foreach,您可以通过 &lt;td&gt;{{ $result[$key][$i] }}&lt;/td&gt; 访问这些值,我相信它会起作用吗?
  • 不是真的,我遇到的问题是每个索引 (PTM) 都有数据填充在每个索引中,我需要调用 PTM 的第一个位置,而不是其他位置,结构不是以我想要的方式构建表格非常简单。
  • 好吧,我相信我做到了。

标签: php arrays json laravel


【解决方案1】:

创建数据库并插入表信息。接下来使用控制台 make:controller // 制作控制器

制作方法...(选择数据库信息)

          $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>

首先你选择数据库的信息并在视图中显示它们

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多