You are not logged in.

#1 2007-02-20 10:14:11

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Generate columns and rows with a loop

$query = "SELECT * FROM `Sale` WHERE `Date` >= '2007-01-11' AND `Date` <= '2007-01-14' GROUP BY `Date` ORDER BY Date ASC";
$result = mysql_query($query);

How to generate columns and rows with the above mysql query to get this output:

Name/Day 11    12    13    14
--------------------------------------
Appple      30     25    26    28
Orange     5       23     12     0
Grape       7       16     0    10

The header I managed to create but not the rows. This is what I did.

<?php
$query = "SELECT * FROM `Sale` WHERE `Date` >= '2007-01-11' AND `Date` <= '2007-01-14' GROUP BY `Date` ORDER BY Date ASC";
$result = mysql_query($query);

//Header
?>
<tr>
 <td valign=top>
  <table cols=6 width=100% border="1" cellspacing=1 cellpadding=1>
    <th BGCOLOR=#6C83B0 height="25" width="1"><font color=#FFFFFF size='1'><b></th>
    <th BGCOLOR=#6C83B0 width="120" align="left"><font color=#FFFFFF size='2'><b>Name/Day</b></th>
<?

$result = mysql_query($query);
if ($daterow = mysql_fetch_array($result)) {
  do {
$TimeStamp = strtotime("$daterow[Date]");
$day = date("j", $TimeStamp);
?>
   <th BGCOLOR=#6C83B0 width="5" align="center"><font color=#FFFFFF size='2'><b><?echo $day?></b></th
<?
     }while ($daterow = mysql_fetch_array($result));
}

?>
    <th BGCOLOR=#6C83B0 width="5" align="center"><font color=#FFFFFF size='2'><b>Total</b></th>
    <th BGCOLOR=#6C83B0><font color=#FFFFFF size='2'><b></b></th>
    <th BGCOLOR=#6C83B0 width="1"><font color=#FFFFFF size='1'><b></b></th>
</tr>
<?

//Rows

$result = mysql_query($query);
if ($row = mysql_fetch_array($result)) {

  do {
include("../lib/browser_sales_daily.php");
    } while ($row = mysql_fetch_array($result));

}

?>
</tbody></table>
<?php
//####################
//browser_sales_daily.php
//####################

print <<<EOD
<td BGCOLOR=#EDEDED align="left">
<font size="2">$row[Name]</font>
</td>
EOD;

// <<<< HOW TO GENERATE ROWS HERE WITH A LOOP????? >>>

print <<<EOD
</tr>
EOD;
?>

Markku

Offline

Board footer

Powered by FluxBB