Problem with a select box

Problem with a select box

by Marty Borisov -
Number of replies: 10

Hello,

I'm having a problem with a select box and inserting into the database. So, I created my own moodle form which has got several textbox, selectbox and radio buttons.
When I enter to my form and fill in all fields, press the button 'Save' I'm getting an error with inserting into the table. I understood what the problem is, but I can't find a way to solve it.

I have a select box showing the days - from Monday to Saturday. I created it using the following code:

$arrDays = block_presence_get_days();
$mform->addElement('select', 'selDay', get_string('day:', 'block_presence'), $arrDays);
$mform->addRule('selDay', null, 'required', null, 'client');

where the function block_presence_get_days() is the following one:

function block_presence_get_days() {
          return array(get_string('chday', 'block_presence'), //Choose a day
                             get_string('mon', 'block_presence'), //Monday
                             get_string('tue', 'block_presence'), //Tuesday
                             get_string('wed', 'block_presence'), //Wednesday
                             get_string('thu', 'block_presence'), //Thursday
                             get_string('fri', 'block_presence'), //Friday
                             get_string('sat', 'block_presence') //Saturday
);

For example I choose Saturday from the select box and I want to save my information via the button 'Save' (to insert a record into a table), but I use the function print_object() to check if the information will be correctly saved. And here is my surprise:

for the field selDay(for example) I have got the NUMBER 6 instead of the string Saturday. And my big question is why? I cannot understand how that happens. What I want is to have for [selDay] => Saturday, not the number of the element from the array. (the array starts counting from 0, and the seventh element is with number 6 and value Saturday). Could you please someone help me how to solve this problem? Thank you.

Average of ratings: -
In reply to Marty Borisov

Re: Problem with a select box

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Re-index the array... wink

e.g. return array('foo' => 'Burger', 'bar' => 'Fries');

In reply to Paul Holden

Re: Problem with a select box

by Marty Borisov -

Hello Paul,

I tried your proposal, but no change. The index is still being inserted into the table:

In reply to Marty Borisov

Re: Problem with a select box

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

function block_presence_get_days() {
          return array('' => get_string('chday', 'block_presence'), //Choose a day
                             'mon' => get_string('mon', 'block_presence'), //Monday
                             'tue' => get_string('tue', 'block_presence'), //Tuesday
                             'wed' => get_string('wed', 'block_presence'), //Wednesday
                             'thur' => get_string('thu', 'block_presence'), //Thursday
                             'fri' => get_string('fri', 'block_presence'), //Friday
                             'sat' => get_string('sat', 'block_presence') //Saturday
);

This will insert the string 'mon', 'tue', etc. when you select the version chosen via the 'get_string' on the right. Of course, you could put a 'get_string' on either side of the '=>' symbol to insert exactly what was selected from the drop-down into the database. However, that would be a really bad idea, as it would break as soon as the user changed languages, as the data in the database would no longer match any of the options in the selection (e.g. if they started in English and chose 'Wednesday', then switched to French, then the options available would be 'Lundi', 'Mardi', 'Mecredi', etc. and the value 'Wednesday' would not be found, however, sticking with non-translated values in the database would mean that the value was always present).

PS Apologies for any typos in the French week days - it's a long time since I studied GCSE French ...

Average of ratings: Useful (2)
In reply to Davo Smith

Re: Problem with a select box

by Marty Borisov -

Hello Davo,

thank you for the advice. I understood what you are getting at. I need some time to think and check how things work. Thank you. 

In reply to Davo Smith

Re: Problem with a select box

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Davo "Apologies for any typos in the French week days - it's a long time since I studied GCSE French ..."

Not too bad, only one typo in Mercredi.tongueout

Joseph (once a teacher, always a teacher)

In reply to Joseph Rézeau

Re: Problem with a select box

by Marty Borisov -

Hello again,

Things with static arrays work perfectly, but I am still having the same problem with dynamic array. Let me explain to you what I am doing:
I want all groups from certain course in order to put them in a select box. I get them via a sql query and save the result in a array. After that I put the array in 'select' element. When a user chooses a group from the drop-down element, I save his choice in the database, but it is saved its index(as above I had described) and after that this array will not contain the chosen group. Here is my code:

$results = $DB->get_records_sql("SELECT DISTINCT name
FROM {groups}
WHERE name LIKE CONCAT('C0', ?, '%')
AND NOT EXISTS (SELECT 1
FROM {group_form}
WHERE groupnumber = name
AND courseid = ?)
ORDER BY name", array($courseYear->idnumber, $courseid));

$arrayGroups = array(get_string('chGroup', 'block_presence'));

foreach($results as $result) {
array_push($arrayGroups, $result->name);
}

$mform->addElement('select', 'groupnumber', get_string('gr:', 'block_presence'), $arrayGroups);

Could you please help me to save the name of the group, not its index? Or maybe different kind of solution, I do not know. Maybe my logic is wrong. I do not have any other ideas how to fix it. Thank you.

In reply to Marty Borisov

Re: Problem with a select box

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

foreach($results as $result) {
$arrayGroups['whatever you want to save in the database'] = 'whatever you want to display in the drop down';
}

Average of ratings: Useful (1)
In reply to Marty Borisov

Re: Problem with a select box

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Marty "I'm getting an error with inserting into the table."

Could you copy the error message you are getting?

Joseph

In reply to Joseph Rézeau

Re: Problem with a select box

by Marty Borisov -

Hello Joseph,

the error was that a field (with type varchar) from my table didn't have a default value. But I fixed it. And now I don't have problem with inserting a record. The problem is what is inserted and why? (I can't understand that - as I had described above).