# Forums | Mahara Community

## Developers / Assign database results to array

Deleted user
Posts: 19

07 July 2013, 12:15 PM

Hi,

I have created a new menu item in mahara and have added an additional table with records in the database.  The problem is that I am trying to use get_record() to retrieve the record from the database and then want to use these results in an array.

When I setup the pieform elements array as below, I am trying to let the template show the default values in the input field but I have tried the default value assignment but this did not work. What is the correct way to assign these results to the array default value?

$br = 'Bronze';$co = get_record('course', 'coursename', $br);$elements = array(
'coursename' => array(
'type'    => 'text',
'defaultvalue' => $co->coursename, 'rules' => array('required' => true), ), I am not sure why this did not work. Are there any ideas to get this working please? Delvon Posts: 348 ##### Re: Assign database results to array 07 July 2013, 7:29 PM Hi$br = 'Bronze';
$co = get_record('course', 'coursename',$br);

The upper two lines of code tell me that you want to get record from table 'course' where field 'coursename' equals 'Bronze'.

Then you want to assign this value as default value. But why bother with get_record function if you already have the default value stored in variable $br. You could do:$elements = array(
'coursename' => array(
'type'    => 'text',
Posts: 896
##### Re: Assign database results to array

15 July 2013, 4:32 PM

Hi Delvon,

It looks like you're using pieforms correctly in  the example. The value you passed in as 'defaultvalue' should show up as the default value of the text field.

To expand on what Gregor said, the way you're calling get_record() is kind of redundant. It's essentially running this query: "SELECT coursename FROM course WHERE coursename=?;". In other words, you're passing in coursename as a parameter to the SQL query, and then you're retrieving the same coursename from the result (or boolean false, if there was no record matching that coursename).

Possibly if you're seeing nothing in your form, it's because there was no record in the database matching that coursename, and as a result, get_record() returned boolean false.

Cheers,
Aaron

4 results