简体   繁体   中英

Moodle Datatable

I am trying to create a datatable in Moodle. However, I have problem fetching data from the database and putting the data into the datatable. Can someone help me? Here is my index.php

<?php
require_once '../../config.php';
global $USER, $DB,$CFG;

$PAGE->set_url('/local/glossary/index.php');
$PAGE->set_context(context_system::instance());
$PAGE->requires->jquery();
//$PAGE->requires->js('/local/glossary/assets/glossary.js');
require_login();

$strpagetitle=get_string('glossary','local_glossary');
$strpageheading=get_string('glossary','local_glossary');
$PAGE->set_title($strpagetitle);
$PAGE->set_heading($strpageheading);

$sql = "SELECT id, term, definition FROM mdl_local_glossary";
$result = $DB->get_records_sql($sql);
$result = new stdClass();

echo $OUTPUT->header();
echo $OUTPUT->render_from_template('local_glossary/searchbar',[]);
echo $OUTPUT->footer();

I can see a couple of immediate issues with what you have written.

Firstly, you've hardcoded the 'mdl_' prefix, so this code will fail on any sites that use a different prefix (including any automated testing sites).

$sql = "SELECT id, term, definition FROM {local_glossary}";

Will fix it, or even better:

$result = $DB->get_records('local_glossary', [], 'id, term, definition');

The second problem, is that you retrieve the results (into an array called $result), then immediately overwrite these results with: $result = new stdClass();

If you want to pass the data into a template, then try something like:

echo $OUTPUT->render_from_template('local_glossary/searchbar', ['results' => $result]);

(after removing the unwanted $result = new stdClass(); line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM