0

I am using a code-igniter query which returns the the array below with additional calculated [year_totalsales] added to array.

Array ( [0] => Array ( [year] => 2009 [month] => November [month_sales] => 524 [year_totalsales] => 3610 ) [1] => Array ( [year] => 2009 [month] => December [month_sales] => 521 [year_totalsales] => 3610 ) [2] => Array ( [year] => 2010 [month] => January [month_sales] => 609 [year_totalsales] => 3610 ) [3] => Array ( [year] => 2010 [month] => February [month_sales] => 619 [year_totalsales] => 3610 ) [4] => Array ( [year] => 2010 [month] => March [month_sales] => 732 [year_totalsales] => 3610 ) [5] => Array ( [year] => 2010 [month] => April [month_sales] => 605 [year_totalsales] => 3610 ) )

Is there a way to get the output result to look this:

year-------------------------

month---------------monthsales
0

3 Answers 3

1

well you could do something like:

<?
$myarray = array( 0 => array( 'year' => 2009, 'month' => "November", 'month_sales' => 524, 'year_totalsales' => 3610 ),
1 => array ( 'year' => 2009, 'month' => 'December', 'month_sales' => 521, 'year_totalsales' => 3610 ),
                  2 => array ( 'year' => 2010, 'month' => "January", 'month_sales' => 609, 'year_totalsales' => 3610 ));

$linewidth = 29; // Change to the width you want of course
$sep = "-"; // Change the seperator you want
$lastyear = "";
function pad($output, $length) {
    global $sep;
    $output = (string)$output;
    while (strlen($output) < $length) {
        $output .= $sep;
    }
    return $output;
}
foreach($myarray as $row) {
    if ($row['year'] != $lastyear) {
        echo pad($row['year'], $linewidth), PHP_EOL;
        $lastyear = $row['year'];
    }
    echo pad($row['month'], $linewidth-strlen((string)$row['month_sales'])), $row['month_sales'], PHP_EOL;
}
?>

Of course you could add your own formatting or whatever if this isn't satisfactory.

Edit: tested, it works properly now.

Outputs:

2009-------------------------
November------------------524
December------------------521
2010-------------------------
January-------------------609

Edit this uses an HTML table instead:

<?
$myarray = array( 0 => array( 'year' => 2009, 'month' => "November", 'month_sales' => 524, 'year_totalsales' => 3610 ),
1 => array ( 'year' => 2009, 'month' => 'December', 'month_sales' => 521, 'year_totalsales' => 3610 ),
                  2 => array ( 'year' => 2010, 'month' => "January", 'month_sales' => 609, 'year_totalsales' => 3610 ));

$lastyear = "";
echo "<table><tr><th>Month</th><th>Sales</th></tr>", PHP_EOL;
foreach($myarray as $row) {
    if ($row['year'] != $lastyear) {
        echo "<tr><th colspan=2>", $row['year'], "</th></tr>", PHP_EOL;
        $lastyear = $row['year'];
    }
    echo "<tr><td>", $row['month'], "</td><td>", $row['month_sales'], "</td></tr>", PHP_EOL;
}
echo "</table>", PHP_EOL;
?>

This outputs:

<table><tr><th>Month</th><th>Sales</th></tr>
<tr><th colspan=2>2009</th></tr>
<tr><td>November</td><td>524</td></tr>
<tr><td>December</td><td>521</td></tr>
<tr><th colspan=2>2010</th></tr>
<tr><td>January</td><td>609</td></tr>
</table>
Sign up to request clarification or add additional context in comments.

6 Comments

Your idea seems to be what I am lookingto achieve. But throwing it into a design seem to not do what it is supposed to which is: <h3>Year - Total</h3> <div> <table> <tr> </tr> </table> </div>
Your idea seems to be what I am lookingto achieve. But throwing it into a design seem to not do what it is supposed to which is: <h3>Year(2009) - Total</h3> <div> <table> <tr> <td>month</td> <td>sales</td> </tr> <tr> <td>month</td> <td>sales</td> </tr> etc............ </table> </div> <h3>year(2010) - Total</h3> <div> <table> <tr> <td>month</td> <td>sales</td> </tr> <tr> <td>month</td> <td>sales</td> </tr> etc............ </table> </div>
CODE IS BELOW: <?php if( !empty($data)){ $lastyear = ""; foreach($data as $row) { if ($row['year'] != $lastyear) { <h3><a href="#"><?=$row['year']?> - Total <?=$row['year_totalsales']?></a></h3> <?php $lastyear = $row['year']; } ?> <div> <table width="100%"> <thead> <tr> <th>Month</th> <th width="10%">Amount</th> </tr> </thead> <tbody> <tr> <td><?=$row['month']?></td> <td><?=$row['month_sales']?></td> </tr> </tbody> </table> </div> <?php } } ?>
I added a table implementation. You can see it in the answer as an edit.
Wow thx for the reply. I am still a noob PHP dude but I do learn from other...:-) THing is I actually needed a table for each year... :-)
|
0

Try this:

foreach($year_array as $months_array)
{
echo 'year        '.$year_array[0];
foreach($months_array as $months)
{
echo 'monthsales        '.$months[0];
}
}

6 Comments

It's generally better to avoid embedding loops inside of loops because it kills your algorithm's efficiency. Here's a good resource to learn more if you wish -- bit.ly/bOow9P
@Stephen - While I agree that's a great site, you just linked to about 36 hours of video with no hint at where to find the information regarding embedded loops. Care to narrow it down a little?
It's a whole class on Algorithms, including asymptotic analysis. What you're probably wanting to know about is "Big O" notation and asymptotic analysis - it outlines what patterns are most efficient and such
Start on the first video at about 17:20
(off-topic) @Syntax Error - I checked out your website and I think you should see this article bit.ly/d23hQC
|
0

printf could be used quite well to accomplish this. PHP.net docs for sprintf has the details on what the formatting options are.

foreach ($month_data as $m) {
    printf("%d\n%20s $%.2lf\n", $m['year'], $m['month'], $m['month_sales']);
}

If you wanted to only print unique years, as suggested in the comments, you could try this.

$current_year = null;
foreach ($month_data as $m) {
    if ($current_year != $m['year']) {
        printf("%d\n", $m['year']);
        $current_year = $m['year'];
    }
    printf("%20s $%.2lf\n", $m['month'], $m['month_sales']);
}

1 Comment

I think he is looking for a way to only display the year once instead of before each month.

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.