hooperg
01-30-2010, 09:23 PM
Im dynamically generating tables with the results of query's to a mysql db and Im trying to design one display script that can handle many different sql query's
I have the tables being generated like this..
$fields_num = mysql_num_fields($result);
echo "<tr>";
for($i=0; $i<$fields_num; $i++){
$field = mysql_fetch_field($result);
echo "<th>{$field->name} </th>";
}
echo "</tr>\n";
// printing table rows
$c=0;
while($row = mysql_fetch_row($result))
{
// alternate background in rows to help reading
if(isOdd($c)){echo "<tr class=\"alt\">"; } else { echo "<tr>"; }
$c++;
foreach($row as $cell)
STYLE=\"text-decoration:none\" color:#BDBDBD\">$cell</a> </td>";}
echo "<td> $cell </td>";
echo "</tr>\n";
}
this works great ! Now, the problem ....
I have a function that creates an array with the totals of
columns as the value and column names as names...
$rowsreturned = array('colum1' => 0,
'colum2' => 5,
'colum3' => 3,
'colum4' => 1);
What I need to do is see if the line about to be printed
contains a column name, and if so echo the value, if not
just an empty cell
// printing table footer
$result = $database->query($q);
//mysql_close();
$fields_num = mysql_num_fields($result);
for($i=0; $i<$fields_num; $i++){
$field = mysql_fetch_field($result);
foreach ($rowsreturned as $columb =>$total){
if ("{$field->name}"=="$columb"){
echo "<td>$total </td>"; //{$field->name}
} else {
echo "<td></td>";
}
}
}
The logic works, but the last row of the table has way too many cells, and I know I have reached my limit, I just cant figure out how to nest this correctly !!!
I have the tables being generated like this..
$fields_num = mysql_num_fields($result);
echo "<tr>";
for($i=0; $i<$fields_num; $i++){
$field = mysql_fetch_field($result);
echo "<th>{$field->name} </th>";
}
echo "</tr>\n";
// printing table rows
$c=0;
while($row = mysql_fetch_row($result))
{
// alternate background in rows to help reading
if(isOdd($c)){echo "<tr class=\"alt\">"; } else { echo "<tr>"; }
$c++;
foreach($row as $cell)
STYLE=\"text-decoration:none\" color:#BDBDBD\">$cell</a> </td>";}
echo "<td> $cell </td>";
echo "</tr>\n";
}
this works great ! Now, the problem ....
I have a function that creates an array with the totals of
columns as the value and column names as names...
$rowsreturned = array('colum1' => 0,
'colum2' => 5,
'colum3' => 3,
'colum4' => 1);
What I need to do is see if the line about to be printed
contains a column name, and if so echo the value, if not
just an empty cell
// printing table footer
$result = $database->query($q);
//mysql_close();
$fields_num = mysql_num_fields($result);
for($i=0; $i<$fields_num; $i++){
$field = mysql_fetch_field($result);
foreach ($rowsreturned as $columb =>$total){
if ("{$field->name}"=="$columb"){
echo "<td>$total </td>"; //{$field->name}
} else {
echo "<td></td>";
}
}
}
The logic works, but the last row of the table has way too many cells, and I know I have reached my limit, I just cant figure out how to nest this correctly !!!
