我正在尝试将从数据库中提取的数据显示为可读格式,但我将其作为
我使用Array ( [1] => stdClass Object ( [id] => 1 [name] => Faculty of Law and Management [uni_id] => 12 ) [6] => stdClass Object ( [id] => 6 [name] => Faculty of Engineering [uni_id] => 13 ) [7] => stdClass Object ( [id] => 7 [name] => Faculty of Engineering [uni_id] => 13 ) ) 1
时会echo print_r($record);
。
代码
<?php
$record= $DB->get_records_sql('SELECT * FROM {faculty}');
if($record != NULL)
{
echo print_r($record);
}
?>
如何将每列的数据保存到变量中,以便我可以在表格中显示它们?
答案 0 :(得分:1)
您需要这样做(假设您的数组变量为$array
): -
<?php
echo "<table><tr><th>ID</th><th>Name</th></tr><tbody>"; // start table
foreach($array as $arr){ // start iteration
echo "<tr><td>".$arr->id ."</td><td>".$arr->id ."</td></tr>"; // fetch value and set into table rows.
}
echo "</tbody></table>";//end table
?>