【问题标题】:How to make Table in PHP如何在 PHP 中制作表格
【发布时间】:2017-07-07 14:17:50
【问题描述】:

我想用这个结果制作表格(看 Echo),当我把它放在表格中时,所有结果都不在一张桌子上,而是一张桌子的一个结果。

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

【问题讨论】:

  • 伙计,看这个:w3schools.com/html/html_tables.asp
  • 你需要重复行 的结构并且没有完整的表元素。
  • 用硬木或金属等硬质材料制成的桌子会更好。
  • 谢谢大家,现在解决了.. :)
  • 祝你有美好的一天

标签: php html codeigniter


【解决方案1】:

您应该将表结构放在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>";

【讨论】:

    【解决方案2】:

    您的代码中的问题是您在循环中回显表格,这导致每次在循环中定义另一个表格重新安排您的代码以在循环之前打印表格并在循环之后结束它可能就像这个

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

    【讨论】:

      【解决方案3】:

      您需要重新学习 PHP 基础知识。它是一种模板语言,而不是 回显多个字符串,您只需关闭 PHP 标记 (?&gt;) 和 直接在那里写你想要的输出。然后在需要时重新打开 回到 PHP 代码 (&lt;?php)。

      至于您的确切问题,那是因为您回显了开头和结尾 循环内的每个表,因此它会发生多次。反而, 你想开始你的表并在循环之外结束它,并使用 循环遍历重复的内容:lines.

      <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 每次迭代,然后 增加它。

      【讨论】:

      • 理想情况下,随着时间的推移和更大的项目,您会看到,尽量将演示与逻辑分开。除了在 HTML 中嵌入代码,您可能只想调用一个模板或只需要另一个文件。您的代码越干净,就可以更好地开发、发现错误和维护。
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签