0

Hi i have customize moodle pages... i want to convert php code to moodle... can anyone help me.. this is my code...

<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 Answer 1

1

Here is the code. Usually form class is placed into a separate file, this improves readability.

<?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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.