Using Variable Variable in MySQL fetch_assoc()
Posted: Tue Aug 04, 2009 8:21 am
Through the following code I am attempting to display row header and row information from a database. The only thing displayed on the page however are the first and last for loops. I believe the problem is in the nested for loop where I am using $temp in the fetch_assoc() as $row. I know this is something simple, but I am unable to find the exact problem in other forums. Any help would be much appreciated.
Problem area?
Full code:
Result:
Problem area?
Code: Select all
for ($k=1; $k < 37; $k++) {
$temp = "h".$k;
$$temp.="<td>".$row[$temp]."</td>";
}
Code: Select all
$num_rows = $db->affected_rows;
$rdrow="<tr><td width=\"100\">Record Date</td>";
$temp = "h";
$$temp = "<tr>";
for ($i=1; $i < 37; $i++) {
$temp = "h".$i;
$$temp = "<tr><td>H".$i."</td>";
}
$comp="<tr><td>Comp</td>";
$mill="<tr><td>Mill</td>";
$proc="<tr><td>Processing</td>";
$ebrow="<tr><td>Entered By</td>";
for ($i=1; $i < $num_fields; $i++) {
$row = $result->fetch_assoc();
$rdrow.="<td width=\"100\">".$row['recorddate']."</td>";
for ($k=1; $k < 37; $k++) {
$temp = "h".$k;
$$temp.="<td>".$row[$temp]."</td>";
}
$comp.="<td>".$row['comp']."</td>";
$mill.="<td>".$row['mill']."</td>";
$proc.="<td>".$row['processing']."</td>";
$ebrow.="<td>".$row['enteredby']."</td>";
}
$db->close();
echo $rdrow.="</tr>";
for ($i=1; $i < 37; $i++) {
$temp = "h".$i;
echo $$temp.="</tr>";
}
echo $comp.="</tr>";
echo $mill.="</tr>";
echo $proc.="</tr>";
echo $ebrow.="</tr>";
Code: Select all
3 record(s) selected.
Record Date
H1
H2
H3
H4
H5
H6
H7
H8
H9
H10
H11
H12
H13
H14
H15
H16
H17
H18
H19
H20
H21
H22
H23
H24
H25
H26
H27
H28
H29
H30
H31
H32
H33
H34
H35
H36
Comp
Mill
Processing
Entered By