我有一个基于我的查询的数组:
Array
(
[0] => stdClass Object
(
[ID_REQUEST] => 28
[DEPARTEMENT] => IT
[company] => TMS
[BULAN] => 03
[NUMBER_OF_LETTER] => 008
[NAMA_TIPE] => Perbaikan
[NAMA_ITEM] => 2
[first_name] => Ahmad
[last_name] => Fadly
[TANGGAL_PERSETUJUAN] =>
[TANGGAL_TERIMA] => 2016-03-26 00:00:00
[PERKIRAAN_SELESAI] => 2016-03-26 00:00:00
[TANGGAL_SELESAI] =>
[PELAKSANA] => Dzil
[KELUHAN] => Test
[CATATAN] => Saja
[nama_departement] => IT
)
[1] => stdClass Object
(
[ID_REQUEST] => 5
[DEPARTEMENT] => IT
[company] => TMS
[BULAN] => 03
[NUMBER_OF_LETTER] => 003
[NAMA_TIPE] => Permintaan
[NAMA_ITEM] => 6
[first_name] => Indah
[last_name] => Kurnia
[TANGGAL_PERSETUJUAN] => 2016-03-25 08:00:00
[TANGGAL_TERIMA] => 2016-03-25 09:00:00
[PERKIRAAN_SELESAI] => 2016-03-25 10:00:00
[TANGGAL_SELESAI] => 2016-03-25 11:00:00
[PELAKSANA] => Dzil
[KELUHAN] => Refill toner 85 A
[CATATAN] => Sudah di refill
[nama_departement] => LCL
)
[2] => stdClass Object
(
[ID_REQUEST] => 4
[DEPARTEMENT] => IT
[company] => TMS
[BULAN] => 03
[NUMBER_OF_LETTER] => 002
[NAMA_TIPE] => Perbaikan, Permintaan
[NAMA_ITEM] => 1,2,3,4,5,6
[first_name] => Hud
[last_name] => Adityawan
[TANGGAL_PERSETUJUAN] => 2016-03-25 03:00:00
[TANGGAL_TERIMA] => 2016-03-24 03:00:00
[PERKIRAAN_SELESAI] => 2016-03-25 03:00:00
[TANGGAL_SELESAI] => 2016-03-25 04:00:00
[PELAKSANA] => Awan
[KELUHAN] => Minta naik gaji dong
[CATATAN] => Iya, go ahead. Appproved
[nama_departement] => IT
)
[3] => stdClass Object
(
[ID_REQUEST] => 3
[DEPARTEMENT] => IT
[company] => JFS
[BULAN] => 03
[NUMBER_OF_LETTER] => 001
[NAMA_TIPE] => Perbaikan
[NAMA_ITEM] => 2,3,4,5
[first_name] => Yakub
[last_name] => Yusuf
[TANGGAL_PERSETUJUAN] => 2016-03-25 03:00:00
[TANGGAL_TERIMA] => 2016-03-24 03:00:00
[PERKIRAAN_SELESAI] => 2016-03-25 03:00:00
[TANGGAL_SELESAI] => 2016-03-25 04:00:00
[PELAKSANA] => Dzil
[KELUHAN] => Mohon diperiksa login novell saya karena saya tidak bisa login
[CATATAN] => Akan kami cek jaringan tersebut
[nama_departement] => Ekspor
)
)
请参阅[NAMA_ITEM]
。这可能是一个简单的视图。
[0] => stdClass Object
(
[NAMA_ITEM] => 2
)
[1] => stdClass Object
(
[NAMA_ITEM] => 6
)
[2] => stdClass Object
(
[NAMA_ITEM] => 1,2,3,4,5,6
)
[3] => stdClass Object
(
[NAMA_ITEM] => 2,3,4,5,6
)
如果你看到,这个字符串会被分解为基于,
分隔符的数组。
在HTML中。我的代码看起来像这样:
<table class="table table-striped table-bordered table-responsive table-hover" id="table-request" width="100%">
<thead>
<tr>
<th>No</th>
<th>Nomor Request</th>
<th>Jenis Request</th>
<th>L</th> // FOR CODE 1
<th>P</th> // FOR CODE 2
<th>M</th> // FOR CODE 3
<th>C</th> // FOR CODE 4
<th>N</th> // FOR CODE 5
<th>LL</th> // FOR CODE 6
<th>Nama</th>
<th>Dept</th>
<th>Tgl / Jam Permintaan</th>
<th>Tgl / Jam Penerimaan</th>
<th>Tgl / Jam Perkiraan</th>
<th>Tgl / Jam Selesai</th>
<th>Pelaksana</th>
<th>Uraian Keluhan</th>
<th>Tindakan Yang Diambil</th>
</tr>
</thead>
这是我的代表:
<tbody>
<?php
foreach ($coba as $key => $row):
echo "<tr>";
echo "<td>" . $key . "</td>";
echo "<td>$row->DEPARTEMENT/$row->company/$row->BULAN/$row->NUMBER_OF_LETTER </td>";
echo "<td>$row->NAMA_TIPE </td>";
**// The Problem**
$nama_item = explode(",", $row->NAMA_ITEM);
foreach ($nama_item as $key => $v):
echo "<td>v</td>";
endforeach;
echo "</tr>";
endforeach;
?>
</tbody>
结果如下:
+----------------------------------------+
| L | P | M | C | N | LL | NAMA |
+----------------------------------------+
| 2 | AHMAD | | | | | |
| 6 | INDAH | | | | | |
| 1 | 2 | 3 | 4 | 5 | 6 | HUD |
| 2 | 3 | 4 | 5 | 6 | YAKUB | |
+----------------------------------------+
看起来很尴尬。我需要这样:
+--------------------------------+
| L | P | M | C | N | LL | NAMA |
+--------------------------------+
| | 2 | | | | | AHMAD |
| | | | | | 6 | INDAH |
| 1 | 2 | 3 | 4 | 5 | 6 | HUD |
| | 2 | 3 | 4 | 5 | 6 | YAKUB |
+--------------------------------+
看起来很简单,但我已经堆叠了一个小时。为了帮助,它非常感激。感谢。
答案 0 :(得分:1)
如果我理解你的问题,你的NAMA_ITEM
包含应该分配的项目索引,所以我会这样做:
$nama_item = explode(",", $row->NAMA_ITEM);
for($i = 1 ; $i <= 6 ; ++$i):
echo in_array($i, $nama_item) ? "<td>$i</td>" : "<td></td>";
endfor;
所以这意味着你总是循环遍历6个索引,并且只有当$nama_item
数组的索引存在(in_array)时,才会回显它。否则,回显空td
以填充它。