Get multiple records of all the custom fields

Get multiple records of all the custom fields

by lyn sia -
Number of replies: 1

Hi,

I'm new in moodle and I  want to get the branch, position and date hired of the user. But it seems that I only get only one record. I want to get all of this fields and display it to email confirmation.

This is all I've got:

function user_profile($user) {
global $CFG, $USER, $DB;
$userid = $user->id;
if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $userid);
if ($formfield->is_visible() and !$formfield->is_empty()) {
echo "<dt>{$formfield->field->name}</dt>";
echo "<dd>{$formfield->display_data()}</dd>";
}
}
}
}
}
}

Output: 

Branch: Alabang

I appreciate all the help I can get. Thank you.

Average of ratings: -
In reply to lyn sia

Re: Get multiple records of all the custom fields

by Saurabh sb -

You can find about this problem here : http://docs.moodle.org/dev/Data_manipulation_API#moodle_database::get_records.28.29

If in hurry try this(with your field names) :

///Get all records where foo = bar, but only return the fields foo,bar,jon,doe
$result = $DB->get_records($table,array('foo'=>'bar'),null,'foo,bar,jon,doe');
///The previous example would cause data issues unless the 'foo' field happens to have unique values.