I was able to retrieve the last record. The last record is repeating for title and attachment.
edit_active_form.php
<?php
require('config.php');
require_once($CFG->libdir.'/formslib.php');
$sid = required_param('id', PARAM_INT);
class edit_active_form extends moodleform {
function definition() {
global $DB, $sid;
$mform = $this->_form;
$fileoptions = $this->_customdata['fileoptions'];
$repeatarray = array();
$repeatarray[] = $mform->createElement('text', 'title', get_string('titleno', 'local_statistics'));
$repeatarray[] = $mform->createElement('filemanager', 'attachment', get_string('attachmentno', 'local_statistics'), null, $fileoptions);
$repeatarray[] = $mform->createElement('hidden', 'titleid', 0);
$repeatarray[] = $mform->createElement('hidden', 'attachmentid', 0);
if ($instance){
$repeatno = $DB->count_records('statistics', array('statid'=>$instance));
$repeatno += 2;
} else {
$repeatno = 1;
}
$repeateloptions = array();
// Get the title and attachment values from database.
$get_title = $DB->get_recordset_sql('SELECT * FROM {statistics} WHERE sid = '.$sid.'');
foreach($get_title as $gt) {
$repeateloptions['title['.$gt->id.']']['default'] = $gt->title;
$repeateloptions['attachment['.$gt->id.']']['default'] = $gt->attachment;
}
$repeateloptions['title']['type'] = PARAM_RAW;
$repeateloptions['attachment']['type'] = PARAM_INT;
$mform->setType('title', PARAM_CLEANHTML);
$mform->setType('titleid', PARAM_INT);
$mform->setType('attachment', PARAM_CLEANHTML);
$mform->setType('attachmentid', PARAM_INT);
$this->repeat_elements($repeatarray, $repeatno, $repeateloptions, null, true);
$this->add_action_buttons();
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
}
$mform = new active_form();
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();