Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:04+00:00 2026-05-26T17:09:04+00:00

I have a table that displays months(Jan through Dec) in a table as follows.

  • 0

I have a table that displays months(Jan through Dec) in a table as follows.
This table get the values from database and it is not fixed.

<table width="100%" style="font-size:9px">
<thead>
<tr>
<th colspan="14" style="text-align:center">Year: <?PHP echo $year?></th>
</tr>
<tr >
<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>Nov</th>
<th>Dec</th>
<th>Total</th>
</tr></thead><tbody>   
<tr > 
<?PHP 
for ($i=1; $i<=12; $i++) {
    $yearstart = $year."-".$i."-01";
    $yearend = $year."-".$i."-31";
    $sql="SELECT SUM(total) as totalsales_$i from salesorder where user_id=7 and created  BETWEEN '$yearstart' AND '$yearend' ";
    $res = $db->sql_query($sql);
    $row =  $db->sql_fetchrow($res);
    $total = $total + $row['totalsales_'.$i];
?>
<td style="width: 96px; text-align:right">
<?PHP if ($row['totalsales_'.$i] !='' ) 
    echo "$".number_format($row['totalsales_'.$i],2);
  else     
    echo "<font color=#c2c2c2>$0.00</font>";?>
 </td>
<?PHP //for ?> 
<td style="text-align:right"><?PHP echo "$".number_format($total,2) ;?></td>
</tr>                                       
</tbody></table>

OUTPUT:
    Jan    feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec    Total
    $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0
    $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0
    $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0     $0
    ...
    ...

But what I need is how to display the table vertically, as follows

                2011    2012    2013    ...

    Jan         $0.00   $0.00   $0.00   $0.00
    feb         $0.00   $0.00   $0.00   $0.00
    Mar         $0.00   $0.00   $0.00   $0.00
    Apr         $0.00   $0.00   $0.00   $0.00
    May         $0.00   $0.00   $0.00   $0.00
    Jun         $0.00   $0.00   $0.00   $0.00
    Jul         $0.00   $0.00   $0.00   $0.00
    Aug         $0.00   $0.00   $0.00   $0.00
    Sep         $0.00   $0.00   $0.00   $0.00
    Oct         $0.00   $0.00   $0.00   $0.00
    Nov         $0.00   $0.00   $0.00   $0.00
    Dec         $0.00   $0.00   $0.00   $0.00
    Total       $0.00   $0.00   $0.00   $0.00

I tried a few different ways, but no luck. I wish I could show you some code here about what I tried, but I realy don’t have anthing to show.

Does anyone have any idea how to display this table vetically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:09:05+00:00Added an answer on May 26, 2026 at 5:09 pm

    First of all , you may want to stop executing 12n queries and get all info with one go and power of aggregation mechanism in MySQL:

    SELECT 
        YEAR(created) as `Y`, 
        MONTH(created) AS `M`, 
        SUM(total) AS `SUM` 
    FROM 
        salesorder
    WHERE
        user_id = 7
    GROUP BY 
        YEAR(created), 
        MONTH(created) 
    WITH ROLLUP
    

    this will result in resultset like this:

    Y    | M    | SUM
    2011 | 10   | 20.00
    2011 | 11   | 10.00
    2011 | 12   | 5.00
    2011 | NULL | 35.00     // M == NULL -> sum for this year
    2012 | 1    | 7.00
    2012 | 2    | 7.00
    2012 | NULL | 14.00
    NULL | NULL | 49.00     // M & Y == NULL -> sum for whole resultset
    

    So you will have sum for individual months in every row, sum for every year if M column is null and sum for whole result in the last row (you can discard it, if you don’t need it)

    Now you have to create other array that will be easier to iterate while adding rows into your table. I would suggest it to look like this:

    $tableRows = array(
        0 => array( // 0 will be for sum from each year
                 2011 => 0,
                 2012 => 0
        ),
        1 => array( // January
                 2011 => 0
        ...
    );
    

    I don’t know what class are you using to interact with database, so let me assume there is some method that allows to fetch associative array from resultset

    $years = array();
    while($dbRow = $res->fetchAssoc()){
        $y = intval($dbRow['Y']);
        if($y){
            $m = intval($dbRow['M']);
            $tableRow[$m][$y] = $dbRow['SUM'];
        }    
    }
    $allYears = array_keys($tableRows[0]); // get all years we have
    

    Finally you can present it in your table:

    <table>
       <thead>
         <tr>
           <th>Month</th>
           <?php foreach($allYears as $year): ?>
           <th><?=$year?></th>
           <?php endforeach; ?>
         </tr>
       <thead>
       <tfoot>
       <? foreach($tableRows as $month => $years): ?>
       <tr>
          <td><?=$month?></td>
       <?php foreach($allYears as $y): ?>
          <td><?=$years[$y]?></td>
       <?php endforeach; 
       if(!$month): ?>
       </tr>
       </tfoot>
       <tbody>
       <?php else: ?>
       </tr>
       <?php endif;
       endforeach; ?>
       </tbody>
    </table>
    

    what needs to be corrected is to display month name and Total instead of numbers in first column

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that displays a table in two different modes. In each
So I have a list box that displays averages in a table like format
I have a large database table that I need to display on a Windows
I have table that I insert data with following query (from c# code): INSERT
I have a stored proc that produces a table like this: PeriodStart PeriodEnd TotalActive
I have a table: Hourly_Sales that is like this (counting how many buy something
I have to filter data for last 3 months from current date, so that
i have a page that displays large datasets into html tables. how can i
So I have a parent that defines a onmouseup event that hide/display a table.
I have a javascript function that toggles the display for rows in a table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.