我想在HTML表格中显示来自moodle数据库的数据,但是对于每条记录,都会显示单独的表格:
代码:
$rec=$DB->get_records_sql('SELECT * FROM `mdl_schedules`');
$table = new html_table();
$table->head = array('Date','Time', 'A' , 'B', 'C','D', 'E', 'F');
foreach ($rec as $records) {
$id = $records->id;
$scheduledatet = $records->scheduledate;
$scheduletime = $records->scheduletime;
$session = $records->s;
$venue = $records->v;
$trainer = $records->t;
$category = $records->c;
$course = $records->course;
$link = $records->link;
$table->data = array(array($scheduledatet, $scheduletime, $a,$b,$c,$d,$e,'<a href="'.$link.'">View</a>'));
echo html_writer::table($table);
}
非常感谢任何参考或帮助。
答案 0 :(得分:4)
回声应该在循环之外:)
}
echo html_writer::table($table);
但您可能希望使用flexible_table
代替,以便您可以使用分页。
在/admin/localplugins.php中查看示例。
答案 1 :(得分:1)
我知道这是旧的,但是你只获得一条记录的原因是你没有添加到$table->data
数组。这是您想要的代码:
$rec=$DB->get_records('schedules');
$table = new html_table();
$table->head = array('Date','Time', 'A' , 'B', 'C','D', 'E', 'F');
foreach ($rec as $records) {
$id = $records->id;
$scheduledatet = $records->scheduledate;
$scheduletime = $records->scheduletime;
$session = $records->s;
$venue = $records->v;
$trainer = $records->t;
$category = $records->c;
$course = $records->course;
$link = $records->link;
$table->data[] = array($scheduledatet, $scheduletime, $a,$b,$c,$d,$e,'<a href="'.$link.'">View</a>');
}
echo html_writer::table($table);
我也稍微改了你的查询。如果您只是从表格中获取所有记录,那么$DB->get_records('TABLE_NAME_WITHOUT_PREFIX');
就可以了。