2

My array -

$myfinal = Array(
        13 => Array
            (
            5 => 85,
            4 => 75,
            3 => 65,
            2 => 55 
            ),
        12 => 11,
        7  => 100
        );

This is what I want to generate(table) dynamically -

Required Output - http://jsfiddle.net/LCKW6/

<table cellspacing="1" cellpadding="4" border="3" bgcolor="#f5f5f5">
<tbody>

    <tr bgcolor="#99cccc">
    <th colspan="4">13</th>
    <th colspan="0">12</th>
    <th colspan="0">7</th>
    </tr>

    <tr bgcolor="#99cccc">
    <th width="70">5</th>
    <th width="70">4</th>
    <th width="70">3</th>
    <th width="70">2</th>
    <th width="70">No subcat</th>
    <th width="70">No subcat</th>
    </tr>

    <tr align="right">
    <td>85</td>
    <td>75</td>
    <td>65</td>
    <td>55</td>
    <td>11</td>
    <td>100</td>
    </tr>
</tbody></table>

My code try, I tried with the first tr and th but for the rest I am confused with the loop:

<?php
$myfinal = Array(
                13 => Array
                        (
                        5 => 85,
                        4 => 75,
                        3 => 65,
                        2 => 55 
                        ),
                12 => 11,
                7  => 100
             );

?>
<table cellspacing="1" cellpadding="4" border="3" bgcolor="#c3cece">
<tbody>
<tr bgcolor="#99cccc">
<?php
foreach( $myfinal as $key => $value )
{
    if( is_array($value) )
    {
    echo '<th colspan="'.sizeof($value).'">'.$key.'</th>';
    }
    else 
    {
    echo '<th colspan="0">'.$key.'</th>';
    }
}

?>
</tr>
</tbody>
</table>
4
  • You need colspan = "1", not 0 Commented Feb 21, 2013 at 20:43
  • Do you need arbitrary depth or just 2 layers? Commented Feb 21, 2013 at 20:46
  • @PhilipWhitehouse This type of output I need jsfiddle.net/LCKW6 Commented Feb 21, 2013 at 20:46
  • @PhilipWhitehouse No actually I just need to set table headers like that way..its all going to be dynamic..as lower trs will be populated from another table Commented Feb 21, 2013 at 20:47

1 Answer 1

2

Works for two layers... Arbitrary layers is probably possible but much more complex.

// Top row
echo '<tr>';
foreach( $myfinal as $key => $value )
{
    if( is_array($value) )
    {
        echo '<th colspan="'.sizeof($value).'">'.$key.'</th>';
    }
    else 
    {
        echo '<th colspan="1">'.$key.'</th>';
    }
}
echo '</tr>';
//Middle row
echo '<tr>';
foreach( $myfinal as $key => $value )
{
    if( is_array($value) ) 
    { 
        foreach($value as $key => $column) {
            echo '<th colspan="1">'.$key.'</th>';
        }
    }
    else 
    {
        echo '<th colspan="1">No subcat</th>';
    }
}
echo '</tr>';
//Data
echo '<tr>';
foreach( $myfinal as $key => $value )
{
    if( is_array($value) ) 
    { 
        foreach($value as $key => $column) {
            echo '<td>'.$column.'</td>';
        }
    }
    else 
    {
        echo '<td>'.$value.'</td>';
    }
}
echo '</tr>';
Sign up to request clarification or add additional context in comments.

3 Comments

Its working fine..but since I have a dynamic dataset..will the code set i am using this time ok or it needs modification..as I think its not the appropriate way I am handling the data this time
Most people store data as $array[$row]['column'];. This is easy to loop and parse. The format you are suggesting above is much different which will probably give you many problems along the way, apart from this bit.
@PhilpWhitehouse yeah I too feel so..thx a lot man :) +1 and green tick :)..sometimes I really feel lost with theloops :( just let me know if you have any idea to learn and practice :)

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.