2016-03-27 7 views
0

von explodieren in html kam habe ich ein Array meine Abfrage wie folgt basiert:PHP-Array erzeugen, die

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 
    ) 

) 

auf [NAMA_ITEM] Bitte beachten Sie. Dies ist vielleicht die einfache Ansicht.

[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 
) 

Wenn Sie sehen, werden diese Zeichenfolge in ein , Begrenzer basierten Array explodieren.

In html. Mein Code sah wie folgt aus:

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

Das ist mein representated:

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

Das Ergebnis ist wie folgt aus:

+----------------------------------------+ 
| L | P  | M | C | N | LL  | NAMA | 
+----------------------------------------+ 
| 2 | AHMAD | | | |  |  | 
| 6 | INDAH | | | |  |  | 
| 1 | 2  | 3 | 4 | 5 | 6  | HUD | 
| 2 | 3  | 4 | 5 | 6 | YAKUB |  | 
+----------------------------------------+ 

Es lookeds so umständlich. Ich brauche so:

+--------------------------------+ 
| L | P | M | C | N | LL | NAMA | 
+--------------------------------+ 
| | 2 | | | | | AHMAD | 
| | | | | | 6 | INDAH | 
| 1 | 2 | 3 | 4 | 5 | 6 | HUD | 
| | 2 | 3 | 4 | 5 | 6 | YAKUB | 
+--------------------------------+ 

Es sah einfach aus, aber ich habe für eine Stunde gestapelt. Für die Hilfe ist es so dankbar. Vielen Dank.

Antwort

1

Wenn ich Ihre Frage richtig verstanden habe, Ihre NAMA_ITEM enthält die Artikel-Indizes, die zugeordnet werden soll, so würde ich diese stattdessen tun:

$nama_item = explode(",", $row->NAMA_ITEM); 
for($i = 1 ; $i <= 6 ; ++$i): 
    echo in_array($i, $nama_item) ? "<td>$i</td>" : "<td></td>"; 
endfor; 

Also, was das bedeutet, ist, dass Sie immer eine Schleife durch 6 Indizes und nur Wenn der Index (in_array) des Arrays von $nama_item existiert, echo es. Else, echo die leere td, um es auszufüllen.