On Mon, 2003-10-27 at 12:44, Aaron Gould wrote:
> This array problem has been stumping my brain for a little while now...
> here's a sample of my data:
> 
>      $purchases[001][200304] = array('regular'=>3, 'booked'=>4);
>      $purchases[002][200303] = array('regular'=>5, 'booked'=>1);
>      $purchases[002][200304] = array('regular'=>1, 'booked'=>0);
>      $purchases[002][200307] = array('regular'=>0, 'booked'=>2);
>      $purchases[003][200303] = array('regular'=>0, 'booked'=>4);
>      $purchases[003][200301] = array('regular'=>5, 'booked'=>0);
>      $purchases[004][200309] = array('regular'=>0, 'booked'=>2);
> 
> The first array element of $purchases is a Part Number (or "SKU").  The
> second element of $purchases is the month/year for which the assigned data
> ("regular" and "booked" values) are set.
> 
> I need to take this data and place it into a table that has a row for each
> part number, and 12 months worth of cells.  An example of the desired chart
> (apologies to the
> non-fixed font viewers!):
> 
> SKU| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 001|     |     |     | 3/4 |     |     |     |     |     |     |     |
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 002|     |     | 5/1 | 1/0 |     |     | 0/2 |     |     |     |     |
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 003| 5/0 |     | 0/4 |     |     |     |     |     |     |     |     |
> ---+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----
> 004|     |     |     |     |     |     |     |     | 0/2 |     |     |
> 
> In this case, the table layout would be 4 rows and 12 cells.
> 
> In this sample data's case, there are three rows of Part Number "002".  This
> needs to be combined into one row.  There will only be a maximum of
> 12 elements for each SKU (there will never be duplicated Month/Year values).
> 
> Can anyone lead me in the right direction?

I was feeling generous so I whipped up the answer for you :)

<?php

$purchases[001][200304] = array('regular'=>3, 'booked'=>4);
$purchases[002][200303] = array('regular'=>5, 'booked'=>1);
$purchases[002][200304] = array('regular'=>1, 'booked'=>0);
$purchases[002][200307] = array('regular'=>0, 'booked'=>2);
$purchases[003][200303] = array('regular'=>0, 'booked'=>4);
$purchases[003][200301] = array('regular'=>5, 'booked'=>0);
$purchases[004][200309] = array('regular'=>0, 'booked'=>2);

echo '<table border="1">'."\n"
    .'<tr>'                   
    .'<th>SKU</th>'
    .'<th>Jan</th>'
    .'<th>Feb</th>'
    .'<th>Mar</th>'
    .'<th>Apr</th>'
    .'<th>May</th>'
    .'<th>Jun</th>'
    .'<th>Jul</th>'
    .'<th>Aug</th>'
    .'<th>Sep</th>'
    .'<th>Oct</th>'
    .'<th>Nob</th>'
    .'<th>Dec</th>'
    .'</tr>'."\n"; 

foreach( $purchases as $sku => $skuData )
{
    echo '<tr>'
        .'<td>'.sprintf( '%03d', $sku ).'</td>';

    for( $i = 1; $i <= 12; $i++ )
    {
        $key = '2003'.sprintf( '%02d', $i );

        echo '<td>';

        if( isset( $skuData[$key] ) )
        {
            echo $skuData[$key]['regular'].'/'.$skuData[$key]['booked'];
        }                                       
        else
        {                        
            echo '&nbsp;';
        }

        echo '</td>';
    }

    echo '</tr>'."\n";
}

echo '</table>'."\n";


Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to