0

我需要我的 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 输出以获得我需要的结构?

4

3 回答 3

2
<?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;
?>
于 2012-09-26T09:45:32.873 回答
0

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

$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;
于 2012-09-26T08:11:09.877 回答
0

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

$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 的可读性。

于 2012-09-26T08:18:35.807 回答