vignesh
vignesh

Reputation: 1

Converting php code to moodle

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>

Upvotes: 0

Views: 358

Answers (1)

Marina
Marina

Reputation: 491

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();

Upvotes: 1

Related Questions