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

for starters, your arrays are poorly structured. Better would be to start with:

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

Which is a lot less noise. :-)

Or even:

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

That way you can easily add more rows to your table.

I'll answer your question with this updated structure in mind (not your original).

If you really need the original structure, it can also easily be used. Here is the code:

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

(I added some \n to improve readability of outputted HTML.

于 2012-09-26T08:18:35.807 に答える