[thelist] PHP & Crosstabs [long] MORE CLEARLY MAYBE...
Andrew Forsberg
andrew at thepander.co.nz
Tue May 7 21:36:03 CDT 2002
On Wed, 2002-05-08 at 07:32, Jay Blanchard wrote:
> When you want to write out a table row you have to know where the data is
> coming from;
> <?
> while($dbrow = mysql_fetch_object($dbx)){
> print("<tr>\n");
> print("<td>");
> print($dbrow->RecordDate);
> print("</td><td align=\"right\">");
> print($dbrow->r100101); <--------------uses the "AS" from the query
[...]
Hi Jay
How about something like this:
// Only print out the <th> tags once
$headerPrinted = FALSE;
// Get the row as an array so we can use foreach()
// Retrieve the array as an associative array only, not
// with the numeric indices (otherwise each column will appear
// twice).
while($dbrow = mysql_fetch_array($dbx, MYSQL_ASSOC)) {
print("<tr>\n");
// Output the table header with the column names
if ($headerPrinted == FALSE) {
// the first column is aligned left
$align = "left";
foreach($dbrow AS $key => $value) {
// output the key for the array in the header row
print("<th align=\"$align\">" . $key . "</th>\n");
print("</tr>\n\n<tr>\n");
// every other column is aligned right
$align = "right";
}
// only output the header once
$headerPrinted = TRUE;
}
// align the first column to left
$align = "left";
foreach($dbrow AS $key => $value) {
// output the value for each key in the data rows
print("<td align=\"$align\">" . $value . "</td>\n");
// align right for every column but the first
$align = "right";
}
print("</tr>\n\n");
}
There's bound to be a way to improve on the redundancy of the $value in
the first foreach loop, and the $key in the second one. I also may have
misunderstood the question :-) -- is this the sort of thing you want?
Anyhow, hth,
Andrew
More information about the thelist
mailing list