2015-11-26 98 views
0

嗨,我有定製Moodle的網頁...我想轉換PHP代碼的Moodle ...誰能幫我..這是我的代碼...轉換PHP代碼的Moodle

<form action="index.php"> 
    <select name="id"> 
<?php 
$result = mysql_query("SELECT Course_Name,LMS_Course_ID FROM m_tl_mastercourse"); 
    while ($row = mysql_fetch_array($result)) { 
?> 

     <option value="<?php echo $row['LMS_Course_ID']; ?>"><?php echo $row['Course_Name']; ?></option> 
    <?php 

     } 
?> 
<input type="submit" value="SUBMIT" /> 
</select> 
    </form> 

回答

1

這裏代碼。通常表單類被放置到一個單獨的文件中,這提高了可讀性。

<?php 

include(__DIR__.'/config.php'); // Or '/../config.php' or '/../../config.php', you'll figure it out. 
require_once($CFG->libdir.'/formslib.php'); 

class mysuperform extends moodleform { 
    public function definition() { 
     global $DB; 
     $mform = $this->_form; 
     $options = $DB->get_records_menu('tl_mastercourse', array(), '', "lms_course_id,course_name"); // Consider adding sorting here! 
     $mform->addElement('select', 'id', '', $options); // Third argument is the label, it was empty in your example. 
     $this->add_action_buttons(false); 
    } 
} 

$PAGE->set_url(new moodle_url('/yourpageurl.php')); 

// Call require_login(), require_capability() and other access validation here. 

$PAGE->set_context(context_system::instance()); // Only needed if it is not inside a course. 

$form = new mysuperform(); 
if ($form->is_cancelled()) { 
    // Do stuff, redirect. 
} else if ($data = $form->get_data()) { 
    // Form was submitted, do stuff, redirect. 
    echo "<pre>"; 
    print_r($data); 
    exit; 
} 

echo $OUTPUT->header(); 
$form->display(); 
echo $OUTPUT->footer();