Forums | Mahara Community

Developers /
Assign database results to array


anonymous profile picture
Account deleted
Posts: 19

07 July 2013, 12:15

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

Gregor Anželj's profile picture
Posts: 349

07 July 2013, 19:29

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',
     'defaultvalue' => $br,
        'rules'   => array('required' => true),
    ),

 

Or maybe you wanted to do something else?

HTH, Gregor

anonymous profile picture
Account deleted
Posts: 19

08 July 2013, 10:37

Hi Gregor,
The get_record function has to be used because it is based on a selected option by the user that the record will be retrieved from the database.
I may have given the wrong impression but $br was just there for test purpose but it will be based on a selection so the get_record has to be used..
Aaron Wells's profile picture
Posts: 896

15 July 2013, 16:32

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

Edits to this post:

4 results