我需要我的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输出以获得我需要的结构?
答案 0 :(得分: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;
?>
答案 1 :(得分: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;
答案 2 :(得分: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的可读性。