【问题标题】:PHP nested foreach loops: html tables inside of themPHP嵌套foreach循环:其中的html表
【发布时间】:2012-09-17 19:29:39
【问题描述】:

我需要我的 PHP 页面显示以下结构:

  <table border="1">
  <tr>
         <td>Title one</td><td>Title two</td>
  </tr>
  <tr>
         <td>I'm content one</td><td>And here's content two</td>
  </tr>
  </table>

这是我的 php 页面:

$array = array( array( Title => "Title one"), array( Title => "Title two") );
$array2 = array( array( Content => "I'm content one"), array( Content => "And here's content two") );
 $myvar ="Title one";

$htm .="<table border='1'>";
    for ($i=0; $i<=1; $i++) 
    {
        $htm .="<tr>";
        foreach ($array[$i] as $title)
        {   
            $htm .="<td >".$title."</td>"; 
            foreach($array2 as $b)
            {
                $content  = $b['Content'];
                    if ($myvar == $title)
                    {
                        $htm .="<tr><td>".$content."</td></tr>"; 
                    }else
                    {
                        $htm .="<tr><td ></td></tr>"; 
                    }

            } 
        }
    }

$htm .="</table>";
echo $htm;

输出的是什么:

<table border='1'>
<tr>
<td >Title one</td><tr><td>I'm content one</td>
</tr>
<tr>
<td>And here's content two</td></tr>
<tr><td >Title two</td><tr>
<td></td></tr><tr><td ></td></tr>
</table>

如何修改 html 输出以获得所需的结构?

【问题讨论】:

    标签: php html loops foreach html-table


    【解决方案1】:
    <?php
    $array  = array( array( Title => "Title one"), array( Title => "Title two") );
    $array2 = array("I'm content one", "And here's content two");`enter code here`
    // A variable to print the corrosponding value in array2.
    // increment it in inner for loop and break to get out of the loop.
    $incr = 0;
    $htm = "<table border='1'>";
        for ($i=0; $i<=1; $i++) 
        {
            $htm .="<tr>";
        foreach ($array[$i] as $title)
            {   
                $htm .="<td >".$title."</td>";
            $htm .="<td>".$array2[$incr]."</td>"; 
                $htm .="</tr>";
        $incr++;
        break;
            }
    
       }
    $htm .="</table>";
    echo $htm;
    ?>
    

    【讨论】:

    • 好吧,我刚刚解决了你想要的,但这不是办法。数组是 php 的力量,有一些好的实践。
    【解决方案2】:

    试试这个代码,我希望这是你想要的:

    $array = array( array( Title => "Title one"), array( Title => "Title two") );
    $array2 = array( array( Content => "I'm content one"), array( Content => "And here's content two") );
    
    $htm .="<table border='1'>";
        for ($i=0; $i<=1; $i++) 
        {
            $htm .="<tr>";
            foreach ($array[$i] as $title)
            {   
                $htm .="<td >".$title."</td>"; 
                $j=0;
                foreach($array2 as $b)
                {
    
                    $content  = $b['Content'];
                        if ($j == $i)
                        {
                            $htm .="<td>".$content."</td></tr>"; 
                        }
                        $j++;
    
                } 
            }
        }
    
    $htm .="</table>";
    echo $htm;
    

    【讨论】:

      【解决方案3】:

      对于初学者来说,您的数组结构很差。 最好从以下开始:

      $ArrTitles = array("Title one", "Title two");
      $ArrContent = array("I'm content one","And here's content two");
      

      噪音要小得多。 :-)

      甚至:

      $ArrTitles = array("Title one", "Title two");
      $ArrContent = array(
      array("I'm content one","And here's content two"),
      array("I'm content one for next row","And here's content two for next row")
      );
      

      这样您就可以轻松地向表格中添加更多行。

      我会以这个更新后的结构(不是你原来的结构)来回答你的问题。

      如果你真的需要原始结构,它也可以很容易地使用。 代码如下:

      $ArrTitles = array("Title one", "Title two");
      $ArrContent = array(
        array("I'm content one","And here's content two"),
        array("I'm content one for next row","And here's content two for next row")
      );
      // assuming the number of elements in the arrays in $ArrContent match the number of titles.
      $htm ="<table border='1'>\n<tr>";
      foreach ($ArrTitles as $oneTitle){
        $htm .= "<td>{$oneTitle}</td>\n";
      }
      $htm .="</tr>\n";
      foreach ($ArrContent as $oneContentRow){
        $htm .= "<tr>";
        foreach ($oneContentRow as $oneContent){
          $htm .= "<td>{$oneContent}</td>\n";
        }
        $htm .= "</tr>";
      }
      $htm .= "</table>";
      echo $htm;
      

      (我添加了一些 \n 以提高输出 HTML 的可读性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        • 2013-07-05
        • 2016-07-25
        • 1970-01-01
        • 2013-03-07
        • 2019-11-17
        相关资源
        最近更新 更多