1

I have the following array in my Moodle plugin:

Array (
    [0] => stdClass Object (
        [id] => 32
        [sessionid] => 5
        [sessiontimezone] => CET
        [timestart] => 1464071400
        [timefinish] => 1464102000 ) 

    [1] => stdClass Object (
        [id] => 33
        [sessionid] => 5
        [sessiontimezone] => CET
        [timestart] => 1465281000
        [timefinish] => 1465311600 )
)

How to get the data. Right now, when I make:

$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));

I get only data from the frist array [0]

How to get the data from every array key and then the single values? Thanks in advance.

1 Answer 1

1

The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.

If you have an array somewhere, then you can get fields from it by writing:

echo $myarray[0]->id;
echo $myarray[1]->id;
etc.

If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:

$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
    echo $session->id;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.