0

I need my PHP page to show the following structure:

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

And here’s my php page:

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

Which is outputting:

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

How can I modify the html outputs to get the structure I need?

3 Answers 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;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Well, I just solved what u wanted but this is not the way. Arrays are the power to php, have some good hands-on them.
0

Try this code, I hope this is what you want:

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.