Bagaimana membuat table dengan php?

output yang diinginkan seperti ini :

tetapi yang keluar seperti ini :

Coding saya :


<?php
$arr = array(
  array(
    'kode' => 'A1',
    'kuliah' => 'Kalkulus',
    'sks' => 4
  ),
  array(
    'kode' => 'C7',
    'kuliah' => 'Matematika Diskrit',
    'sks' => 2
  ),
  array(
    'kode' => 'B3',
    'kuliah' => 'Etika',
    'sks' => 2
  ),
  array(
    'kode' => 'E6',
    'kuliah' => 'Logika',
    'sks' => 2
  ),
);
?>

<!doctype html>

<html>

<head>
  <title>Latihan PHP</title>
<!--Meta-->
<meta charset="UTF-8">
    <meta name="description" content="T-5 Intro to PHP">
  <meta name="keywords" content="PHP">
  <meta name="author" content="MUHAMAD ARIQ RASYID-2020130036">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">


  <!-- Import Bootstrap CSS di sini -->
    <link rel="stylesheet" href="Asset/Plug-ins/bootstrap/css/bootstrap.min.css">
</head>

<body>
  <div class="container mt-5">
    <table>
      <thead>
      </thead>

      <tbody>
      <tr class="table_header">
      <td width="66"><strong>No.</strong></td>
      <td width="50"><strong>Kode</strong></td>
      <td width="100"><strong>Mata Kuliah<strong></td>
      <td width="75"><strong>SKS<Strong></td>
</tr>
      </tbody>
      <?php

$Kode= '';
$Mata_Kuliah = '';
$SKS = '';
foreach($arr as $arr_Code=>$arr_Code_Info){

        $rowcount = 1;

    foreach($arr_Code_Info as $arrMataKuliah=>$arr_SKS){

        echo "<tr>";
          echo "<td>";
              echo ($rowcount == 1 )? $arr_Code :  "" ;
          echo "</td>";

          echo "<td>";
            echo "$arrMataKuliah <br/>";
          echo "</td>";

          echo "<td>";

        foreach($arr_SKS as $arr_TSKS){
                echo "$arr_TSKS <br/>";
        }
          echo "</td>";
        echo "</tr>";
        $rowcount +=1;
    }

}
?>
    </table>
  </div>

  <!-- Import Bootstrap JS di sini -->
  <script type="text/javascript" src="Asset/Plug-ins/bootstrap/js/bootstrap.min.js"></script>
</body>

</html>

pendapat saya mungkin ada yang salah di baris ke 81(

  foreach($arr_SKS as $arr_TSKS) ...

)

tetapi saya tidak tahu harus bagaimana

terima kasih sebelunya ^^

avatar muhamadariqrasyid
@muhamadariqrasyid

2 Kontribusi 0 Poin

Diperbarui 2 tahun yang lalu

1 Jawaban:

<div>Harusnya cukup 1x looping aja, ngga usah dua kali, kemudian datanya itu kan dalam bentuk array, harusnya kita cukup mengakses berdasarkan key nya aja. Berikut contoh kode yang sudah diperbaiki, silahkan dicek letak perbedaannya.</div><pre>&lt;?php $arr = array( array( 'kode' =&gt; 'A1', 'kuliah' =&gt; 'Kalkulus', 'sks' =&gt; 4 ), array( 'kode' =&gt; 'C7', 'kuliah' =&gt; 'Matematika Diskrit', 'sks' =&gt; 2 ), array( 'kode' =&gt; 'B3', 'kuliah' =&gt; 'Etika', 'sks' =&gt; 2 ), array( 'kode' =&gt; 'E6', 'kuliah' =&gt; 'Logika', 'sks' =&gt; 2 ), ); ?&gt;

&lt;!doctype html&gt;

&lt;html&gt;

&lt;head&gt; &lt;title&gt;Latihan PHP&lt;/title&gt; &lt;!--Meta--&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="description" content="T-5 Intro to PHP"&gt; &lt;meta name="keywords" content="PHP"&gt; &lt;meta name="author" content="MUHAMAD ARIQ RASYID-2020130036"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;

&amp;lt;!-- Import Bootstrap CSS di sini --&amp;gt;
&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;Asset/Plug-ins/bootstrap/css/bootstrap.min.css&quot;&amp;gt;

&lt;/head&gt;

&lt;body&gt; &lt;div class="container mt-5"&gt; &lt;table border="1" cellpadding="3" cellspacing="0"&gt; &lt;thead&gt; &lt;tr class="table_header"&gt; &lt;td width="66"&gt;&lt;strong&gt;No.&lt;/strong&gt;&lt;/td&gt; &lt;td width="50"&gt;&lt;strong&gt;Kode&lt;/strong&gt;&lt;/td&gt; &lt;td width="100"&gt;&lt;strong&gt;Mata Kuliah&lt;strong&gt;&lt;/td&gt; &lt;td width="75"&gt;&lt;strong&gt;SKS&lt;Strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php $rowcount = 1; foreach($arr as $data){ echo "&lt;tr&gt;"; // Pembuka table row

            echo &quot;&amp;lt;td&amp;gt;&quot; . $rowcount . &quot;&amp;lt;/td&amp;gt;&quot;;
            echo &quot;&amp;lt;td&amp;gt;&quot; . $data[&#039;kode&#039;] . &quot;&amp;lt;/td&amp;gt;&quot;;
            echo &quot;&amp;lt;td&amp;gt;&quot; . $data[&#039;kuliah&#039;] . &quot;&amp;lt;/td&amp;gt;&quot;;
            echo &quot;&amp;lt;td&amp;gt;&quot; . $data[&#039;sks&#039;] . &quot;&amp;lt;/td&amp;gt;&quot;;

          echo &quot;&amp;lt;/tr&amp;gt;&quot;; // Penutup table row

          $rowcount +=1; // Increment variabe;
        }
      ?&amp;gt;
    &amp;lt;/tbody&amp;gt;
  &amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;

&lt;!-- Import Bootstrap JS di sini --&gt; &lt;script type="text/javascript" src="Asset/Plug-ins/bootstrap/js/bootstrap.min.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><div><br><br>Semoga berhasil.</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 2 tahun yang lalu

Login untuk ikut Jawaban