Web Hosting Talk







View Full Version : Nested loop has me crossed eyed....


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

mattle
01-31-2010, 12:18 AM
(please use the tags for formatting and syntax highlighting purposes.)

I'm not quite clear on this...if you're dynamically generating your header cells with the field names from the query, doesn't every column contain a column name?!?

Your problem is the nested foreach in your second code block. You're looping through the entirety of rows returned--printing a cell each time--for every column name. Your total number of cells is a Cartesian product here.

You should just loop through the fields and then check for an existing array key with the same value as the field name. So, basically, get rid of the foreach and change your condition to:

[php]
if (isset($rowsreturned[$field->name]))
echo "<td>{$rowsreturned[$field->name]}</td>";
else
echo "<td>&nbsp;</td>";

hooperg
01-31-2010, 10:18 AM
Yes every column has a name, and is printed out before this what im doing is running through the same block as the header cells but for a footer. Big difference is this time im seeing if an array has a key the same name as the header, and if so print the arrays value.

Since the array being searched for is dynamic,


$rowsreturned = array('colum1' => 0,
'colum2' => 5,
'colum3' => 3,
'colum4' => 1);


or


$rowsreturned = array('colum1' => 0,
'colum2' => 5,
'colum3' => cats,
'colum4' => dogs);


I cant just get a total on each column, and
my array may not have totaled all columns depending on
user preference