I have the below code to display db column data as table headings
$reportID=$_GET["reportID"];
$result=mysql_query("SELECT * FROM reportFields")or die('Could not connect:loggod line 30 iD' . mysql_error());
$num_rows = mysql_num_rows($result);
echo " <tbody> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$field = $row ['field'];
echo "
<th>$field</th>
"; }
if ($i % 4 == 0) {
echo ''; // it's time no move to next row
}
This works fine, however I am wondering how I get each $field as a separate variable?
Can anyone help?
Also, sorry if this is a vague question, but I am a newbie and probably trying to do something confusing!
Thanks in advance
答案 0 :(得分:1)
It is very unclear question. And you have very weird code.
Just my guess about what you are asking for:
$result=mysql_query("SELECT * FROM reportFields")or die('Could not connect:loggod line 30 iD' . mysql_error());
echo "<table><thead><tr>";
$i=0;
$fields = array();
while ($i < mysql_num_fields($result)) {
$metaField = mysql_fetch_field($result, $i);
$fields[] = $metaField;
echo '<th>'.$metaField->name.'</th';
$i++;
}
echo "</tr></thead>";
echo "<tbody>";
while ($row = mysql_fetch_array($result) )
{
echo '<tr>';
foreach( $fields as $field)
echo '<td>'. $row [$field->name].'</td>';
echo '</tr>';
}
echo "</tbody></table>";
答案 1 :(得分:0)
Not sure this is the answer you are looking for but you can do this
$field = [$row ['field']];
Now to echo out all the values
foreach($field as $v){
echo "<th>$v</th>";
}
答案 2 :(得分:0)
Based on what you wrote in the comments...
$reportID=$_GET["reportID"];
$query = "SELECT * FROM reportFields WHERE reportID=$reportID";
$result=mysql_query($query)or die('Could not connect:loggod line 30 iD' . mysql_error());
$i=1;
echo "<tbody><tr>"; // first row beginning
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$field = $row ['field'];
$reportID = $row ['reportID'];
echo "<td>$reportID</td>";
echo "<td>$field</td>";
if ($i % 4 == 0)echo '</tr><tr>'; // it's time no move to next row
$i++;
}
echo "</tr></tbody>";