Jump to content

Array help lz


kiwimediapro

Recommended Posts

Hi,

 

I have the following code so far :

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

 

BUt this code gives me this:

 

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

 

Instead of this:

<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>
</table>
<table><tr><th>Month</th><th>Sales</th></tr>
<tr><th colspan=2>2010</th></tr>
<tr><td>January</td><td>609</td></tr>
</table>

 

ANy help appreciated thx

Link to comment
https://forums.phpfreaks.com/topic/236913-array-help-lz/
Share on other sites

<?php
$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 = NULL;
foreach($myarray as $row) {
    if ($row['year'] != $lastyear) {
        echo (!empty($lastyear)) ? '</table>', PHP_EOL : NULL;
        echo "<table><tr><th>Month</th><th>Sales</th></tr>", PHP_EOL,"<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;
?>

Link to comment
https://forums.phpfreaks.com/topic/236913-array-help-lz/#findComment-1217832
Share on other sites

YEs it works!  All hail the PHP guru . ...... :-)

 

Using the same array code i need to create a javascript array for Google Charts Like this:

 

raw_data=[['May',1429,0],['June',1565,0],['July',1581,0],['August',1437,0],['September',627,0],['October',660,0],['November',524,0],['December',521,0],['January',0,609],['February',0,619],['March',0,732],['April',0,605]];

 

Where:

 

raw_data= [ [ month1 , year1 sales figure(zero if no figure), year2 sales figure(zero if no figure).....], [ month1 , year1 sales figure(zero if no figure), year2 sales figure(zero if no figure).....]......];

Link to comment
https://forums.phpfreaks.com/topic/236913-array-help-lz/#findComment-1217860
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.