将数组中的数据保存在变量中-Moodle
Save data from array in variables-Moodle
我正在尝试将从数据库中获取的数据显示为可读格式,但我得到的是
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);
}
?>
如何将每一列的数据保存到一个变量中,以便之后可以在 table 中显示它们?
你需要这样做(假设你的数组变量是$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
?>
我正在尝试将从数据库中获取的数据显示为可读格式,但我得到的是
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);
}
?>
如何将每一列的数据保存到一个变量中,以便之后可以在 table 中显示它们?
你需要这样做(假设你的数组变量是$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
?>