Handling Multi-select quick form data

Handling Multi-select quick form data

by brian kremer -
Number of replies: 2

I made a report using a quick form to set parameters. There's a multi-select field to select one or more users. Here's the problem code when the form is submitted and the report is processed. 

} else if ($data = $mform->get_data()) {

...

$learnername = $DB->get_records_select('user', 'id IN (?)', array($data->learner));

...

This produces an error: 

Debug info: ERROR: invalid input syntax for integer: "Array"
SELECT * FROM mdl_user WHERE id IN ($1) 
[array (
0 => 
array (
0 => '81',
1 => '21',
2 => '2',
),
)] 
Error code: dmlreadexception

I can get rid of the error by removing the outer array, like this:

$learnername = $DB->get_records_select('user', 'id IN (?)', $data->learner);

But when I do this, $learnername always contains the first user selected only. I need it to handle as many users as were selected. Any ideas? 

Average of ratings: -
In reply to brian kremer

Re: Handling Multi-select quick form data

by Gavin Neale -

Hi Brian, I'm new to Moodle programming and may have been confused by what you are asking but this might help.

Instead of using get_records_select, how about get_records_list. For me it returns the records of all users when given an array of user ID's. e.g.

$learnername = $DB->get_records_list('user', 'id', $data->learner);

Hope that helps,

Gavin





Average of ratings: Useful (1)