2017-07-07 55 views
-3

我想用这个结果创建表格(看Echo),当我把它放在表格中时,它发生的所有结果都不是在一个表格中,而是在一个表格中的一个结果。如何在PHP中创建表格

foreach ($item_array as $ia_key => $ia_value) 
    { 
     //$theitems = explode('|',$ia_key); 
     for($x = 0; $x < count($ia_key); $x++) 
     { 
      if (($ia_value>=$support)) 
      { 
       echo "$ia_key($ia_value)"; 
       echo "<br>"; 
       $z++; 
      } 
     } 
    } 

我试过这段代码。但结果不在一张表中。

foreach ($item_array as $ia_key => $ia_value) 
    { 
     //$theitems = explode('|',$ia_key); 
     for($x = 0; $x < count($ia_key); $x++) 
     { 
      if (($ia_value>=$support)) 
      { 
       echo "<div class='row'>"; 
        echo "<div class='col-sm-12'>"; 
         echo "<h4 class='m-t-0 header-title'></h4>"; 
         echo "<div class='table-responsive m-b-20'>"; 
          echo "<p class='text-muted font-13 m-b-30'>"; 
          echo "<table id='datatable-buttons' class='table table-striped table-bordered'>"; 
           echo "<thead>"; 
            echo "<tr>"; 
             echo "<th>No.</th>"; 
             echo "<th>Nama Item</th>"; 
             echo "<th>Jumlah</th>"; 
            echo"</tr>"; 
            echo "</thead>"; 
            echo "<tbody>"; 
            $no = 0; 
            $no++; 
             echo "<tr>"; 
              echo "<td> $no </td>"; 
              echo "<td> $ia_key </td>"; 
              echo "<td> $ia_value </td>"; 
              $z++; 
             echo "</tr>"; 
            echo "</tbody>"; 
           echo "</tr>"; 
          echo "</table>"; 
         echo "</div>"; 
        echo "</div>"; 
       echo"</div>"; 
      } 
     } 
    } 

Result of above code < ==点击查看图像。

+0

男人,看看这个:https://www.w3schools.com/html/html_tables.asp –

+0

你需要重复行的结构,并没有完整的表elem。 –

+0

例如,用硬木或金属等硬质材料制成的桌子要好得多。 – Martin

回答

0

你应该把你的表结构的foreach()循环之外:

$no = 0; 
echo "<div class='row'>"; 
echo "<div class='col-sm-12'>"; 
echo "<h4 class='m-t-0 header-title'></h4>"; 
echo "<div class='table-responsive m-b-20'>"; 
echo "<p class='text-muted font-13 m-b-30'>"; 
echo "<table id='datatable-buttons' class='table table-striped table-bordered'>"; 
echo "<thead>"; 
echo "<tr>"; 
echo "<th>No.</th>"; 
echo "<th>Nama Item</th>"; 
echo "<th>Jumlah</th>"; 
echo"</tr>"; 
echo "</thead>"; 
echo "<tbody>"; 

foreach ($item_array as $ia_key => $ia_value) 
{ 
    //$theitems = explode('|',$ia_key); 
    for($x = 0; $x < count($ia_key); $x++) 
    { 
     if (($ia_value>=$support)) 
     { 

      $no++; 
      echo "<tr>"; 
      echo "<td> $no </td>"; 
      echo "<td> $ia_key </td>"; 
      echo "<td> $ia_value </td>"; 
      $z++; 
      echo "</tr>"; 
     } 
    } 
} 

echo "</tbody>"; 
echo "</tr>"; 
echo "</table>"; 
echo "</div>"; 
echo "</div>"; 
echo"</div>"; 
+0

非常感谢...这是工作.. –

0

在你的代码的问题是,你回响在循环中的表和这个结果在定义表中的另一个表中的每个时间内环路重新安排您的代码在循环之前打印表格并在循环可能如此后结束它

echo "<div class='row'>"; 
           echo "<div class='col-sm-12'>"; 
            echo "<h4 class='m-t-0 header-title'></h4>"; 
            echo "<div class='table-responsive m-b-20'>"; 
             echo "<p class='text-muted font-13 m-b-30'>"; 
             echo "<table id='datatable-buttons' class='table table-striped table-bordered'>"; 
    echo "<thead>"; 
               echo "<tr>"; 
                echo "<th>No.</th>"; 
                echo "<th>Nama Item</th>"; 
                echo "<th>Jumlah</th>"; 
               echo"</tr>"; 
               echo "</thead>"; 

      for($x = 0; $x < count($ia_key); $x++) 
        { 
         if (($ia_value>=$support)) 
         { 

               echo "<tbody>"; 
               $no = 0; 
               $no++; 
                echo "<tr>"; 
                 echo "<td> $no </td>"; 
                 echo "<td> $ia_key </td>"; 
                 echo "<td> $ia_value </td>"; 
                 $z++; 
                echo "</tr>"; 
               echo "</tbody>"; 
              echo "</tr>"; 
                       } 
       } 
    echo "</table>"; 
            echo "</div>"; 
           echo "</div>"; 
          echo"</div>"; 
0

您需要重新访问PHP基础知识。它是一种模板语言,而不是 回显出多个字符串,您可以直接关闭PHP标记(?>)和 在那里写您想要的输出。然后重新打开,当您需要 回到PHP代码(<?php)。

至于你确切的问题,那是因为你回声循环内每个表格的开始和结束 ,所以它会发生多次。相反, 您想要启动您的表并在循环外结束它,并使用 循环来循环所重复的内容:

<div class='row'> 
    <div class='col-sm-12'> 
    <h4 class='m-t-0 header-title'></h4> 
    <div class='table-responsive m-b-20'> 
     <p class='text-muted font-13 m-b-30'> 
     <table id='datatable-buttons' class='table table-striped table-bordered'> 
     <thead> 
      <tr> 
      <th>No.</th> 
      <th>Nama Item</th> 
      <th>Jumlah</th> 
      </tr> 
     </thead> 
     <tbody> 
<?php 
    foreach ($item_array as $ia_key => $ia_value) 
    { 
     for($x = 0; $x < count($ia_key); $x++) 
     { 
      if (($ia_value>=$support)) 
      { 
       $no = 0; 
       $no++; 
       echo "<tr>"; 
       echo "<td> $no </td>"; 
       echo "<td> $ia_key </td>"; 
       echo "<td> $ia_value </td>"; 
       $z++; 
       echo "</tr>"; 
      } 
     } 
    } 
?> 
     </tbody> 
     </table> 
    </div> 
    </div> 
</div> 

另外,修改你的逻辑。您每次迭代重置$no,然后 将其递增。

+0

理想情况下,你会看到随着时间和大型项目,尝试让演示文稿与逻辑分离。不用在HTML中嵌入代码,您可能只需调用模板或仅需要其他文件。代码越干净,开发越好,发现错误和维护。 – sidyll