Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用查询填充Moodle表单中的select元素_Php_Mysql_Moodle - Fatal编程技术网

Php 使用查询填充Moodle表单中的select元素

Php 使用查询填充Moodle表单中的select元素,php,mysql,moodle,Php,Mysql,Moodle,我正在使用创建一个Moodle表单。我尝试使用查询填充表单中的下拉元素 class my_form extends moodleform { function definition() { $mform =& $this->_form; $mform->addElement('html', '<h2>' . get_string('header', 'local_data') . '</h2>');

我正在使用创建一个Moodle表单。我尝试使用查询填充表单中的下拉元素

class my_form extends moodleform {

    function definition() {
        $mform =& $this->_form;

        $mform->addElement('html', '<h2>' . get_string('header', 'local_data')  . '</h2>');

        global $USER;
        $userid = $USER->id;
        $myCourseIds = getMyCourses($userid);

        $mform->addElement('select', 'courseid', get_string('courseid', 'local_data'), $myCourseIds);


        $this->add_action_buttons(true, get_string('send', 'local_data'));
    }

}
返回的错误是常规数据库错误。我注意到页面中的示例使用了一个全局变量$FORUM\u TYPES,我在查询中使用了该变量

以下是错误消息:

检测到编码错误,必须由程序员修复:PHP可捕获致命错误。

所以我的问题是——

  • 我可以使用一个变量将数据库查询的结果存储在Moodle forms API的select元素中吗

问题在于我没有将查询结果转换为具有键和值对的数组

class my_form extends moodleform {

    function definition() {
        $mform =& $this->_form;

        $mform->addElement('html', '<h2>' . get_string('header', 'local_data')  . '</h2>');

        global $USER;
        $userid = $USER->id;
        $myCourseIds = array();
        $selectArray = array();
        $myCourseIds = getMyCourses($userid);
      // push query results into selectArray as key, value
        foreach($myCourseIds as $myCourseId) {
            $key = $myCourseId->idnumber;
            $value = $myCourseId->shortname;
            $selectArray[$key] = $value;
        }

        $mform->addElement('select', 'courseid', get_string('courseid', 'local_data'), $selectArray);    

        $this->add_action_buttons(true, get_string('send', 'local_data'));
    }

}
类我的表单扩展了moodleform{
函数定义(){
$mform=&$this->\u表单;
$mform->addElement('html','.get_string('header','local_data'));
全球$用户;
$userid=$USER->id;
$myCourseId=array();
$selectArray=array();
$myCourseIds=getMyCourses($userid);
//将查询结果作为键、值推送到selectArray中
foreach($myCourseId作为$myCourseId){
$key=$myCourseId->idnumber;
$value=$myCourseId->shortname;
$selectArray[$key]=$value;
}
$mform->addElement('select','courseid',get_string('courseid','local_data'),$selectArray);
$this->add_action_按钮(true,get_字符串('send','local_data'));
}
}

您无需在Form functiondefinition()中转换数组,即可获得相同的结果。 因为在查询中,您只选择了两列(idnumber和shortname),所以您可以使用get\u records\u sql\u菜单函数,而不是在getMyCourses()函数中使用get\u records\u sql

get_records_sql_菜单将返回数组,其中元素已成对匹配key->value,其中select中的第一列为key,第二列为value


我也用错误的帐户登录到Moodle,并且在我试图检索的课程中没有让自己成为模块负责人!
class my_form extends moodleform {

    function definition() {
        $mform =& $this->_form;

        $mform->addElement('html', '<h2>' . get_string('header', 'local_data')  . '</h2>');

        global $USER;
        $userid = $USER->id;
        $myCourseIds = array();
        $selectArray = array();
        $myCourseIds = getMyCourses($userid);
      // push query results into selectArray as key, value
        foreach($myCourseIds as $myCourseId) {
            $key = $myCourseId->idnumber;
            $value = $myCourseId->shortname;
            $selectArray[$key] = $value;
        }

        $mform->addElement('select', 'courseid', get_string('courseid', 'local_data'), $selectArray);    

        $this->add_action_buttons(true, get_string('send', 'local_data'));
    }

}