Fetching data from moodle database - php

I am trying to query some data from the moodle database. I am using the data manipulation API. Here is my code. When I run it on the browser, i get a blank screen. i reall dont know what the $enrolids is. Is it an associative array or what. Even when I try create an associative array, I still get a blank screen.
<?php
require_once("../config.php");
$userid=$_GET['userid'];
//Get the enrolids from the mdl_user_enrolments table
$enrolids=$DB->get_records_sql('SELECT enrolid FROM {user_enrolments} WHERE userid=?', array($userid));
echo $enrolids['enrolid'];
?>
Will appreciate the help....

I figured it out:
To anyone with the same problem. Here is the solution.
Moodle is object oriented. Meaning, the queries made, return an Array of stdclass objects i.e:
Array ( [1] => stdClass Object ( [id] => 1 [status] => 0 [enrolid] => 5 [userid] => 3 [timestart] => 0 [timeend] => 2147483647 [modifierid] => 0 [timecreated] => 0 [timemodified] => 0 ) [2] => stdClass Object ( [id] => 2 [status] => 0 [enrolid] => 6 [userid] => 3 [timestart] => 0 [timeend] => 2147483647 [modifierid] => 0 [timecreated] => 0 [timemodified] => 0 ) )
Use the -> notation to fetch whatever value you desire i.e If the above array is stored in a variable e.g. $arr, do the following to access status for example
$arr[1]->status;
CHEERS....

Related

Display a single element of an array

I think it is a very simple question but for a long time I am trying to figure this out:
I need to store the number 18 ([term_id]) from the following array in a variable, how can I do this?
Array (
[0] => WP_Term Object (
[term_id] => 18
[name] => Im Rebgarten
[slug] => im-rebgarten
[term_group] => 0
[term_taxonomy_id] => 18
[taxonomy] => give_forms_category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[object_id] => 900
)
)
Kind regards
You can simply access the variable from object by -> notation like
$array = array();//Your current array
$term_variable = $array[0]->term_id; //store the term_id into the variable
print_r($term_variable); //prints the variable

Reduce memory size of my PHP array

I've just come across the problem of the large memory size of PHP arrays. I'm trying to store a fairly small amount of data per object, in a bunch of small arrays. The storage looks like this.
[Stats] => Array
(
[Apps] => Array
(
[Career] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Team] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Season] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
)
[SubApps] => Array
(
[Career] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Team] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[Season] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
)
I need 20 or more of the middle arrays shown here (e.g 'Apps', 'SubApps' etc). It's an array of arrays of arrays, with the final arrays always being exactly 6 values long.
Since it's just 18 values, I'd assumed that it wasn't much memory, but I've found out that PHP uses about 144 bytes per value, so it's turning out to be huge, especially since I have about 3000 such objects. It's using about 30k per object, just on 20 of these array blocks.
So I'm looking for a memory efficient solution. Since my final array is always 6 values, I'm thinking that maybe I can pack/unpack. I also looked into SplFixedArray but it doesn't suit my needs in some ways and doesn't save much memory. I also want the values to show in a readable format in my database in case I need to edit them manually in the db (one method I tried saved memory but the values were garbled in the db).
UPDATE - Solution. I found a simple solution to this. The arrays are JSON_encode'd for saving to the database. When I load them, they're decoded. Initially I was decoding the whole batch at once, hence the massive memory usage, but the simple solution was to keep them encoded until needed, then decode - process - encode. This way, only one is ever decoded into the array and the rest remain encoded, which appears to use very small amounts of memory.

PhP MultiDimensional Array fetching

I am not really familiar on how php handles array, in .NET I can access array using this method
array[x][y];
My question is:
I am retrieving records from the database and returning it to the $res_merchant_field
$res_merchant_field = $this->CI->merchantfield_model->merchantfield_list( $str_where );
and $res_merchant_field will be populated with this record:
Array
(
[0] => stdClass Object
(
[MFID] => 1
[MFName] => Bill No
[FTID] => 1
[DTID] => 1
[MFRequired] => 1
[MFDefaultValue] =>
[MFDueDate] => 0
[MFToBePaid] => 0
[MFMaxLength] => 12
[MFOrderNo] => 1
[MFStatus] => 1
)
[1] => stdClass Object
(
[MFID] => 2
[MFName] => Gallons Consumed
[FTID] => 1
[DTID] => 2
[MFRequired] => 1
[MFDefaultValue] =>
[MFDueDate] => 0
[MFToBePaid] => 0
[MFMaxLength] => 5
[MFOrderNo] => 2
[MFStatus] => 1
)
[2] => stdClass Object
(
[MFID] => 3
[MFName] => Amount Due
[FTID] => 3
[DTID] => 1
[MFRequired] => 1
[MFDefaultValue] =>
[MFDueDate] => 0
[MFToBePaid] => 1
[MFMaxLength] => 15
[MFOrderNo] => 3
[MFStatus] => 1
)
)
How can I access and fetch the record from that array with this condition:
it will look through all the array find specific index, lets say index 0 which is MFID,
after getting the MFID and comparing it with another variable, if it is true,
it will get the DTID for that array MFID.
example:
get MFID = 1, the DTID will be 1, if I get the MFID = 3, the DTID will be 1.
or how can I access the array like $array[x][y]?
Thanks in advance.
The problem is that the second level is not an array but instead an object, to access a property you will have to use this format.
$array[$x]->$y;
Unfortunately you cannot access a property by an index do o get the MFID of the 0th Item you will need to say
$array[0]->MFID;

Issue getting info out of array

I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

multi dimensional array in random order

I want to make it so that my multi dimensional array is in a random order. How would you do it?
// This is how the array looks like
print_r($slides);
Array
(
[0] => Array
(
[id] => 7
[status] => 1
[sortorder] => 0
[title] => Pants
)
[1] => Array
(
[id] => 8
[status] => 1
[sortorder] => 0
[title] => Jewels
)
[2] => Array
(
[id] => 9
[status] => 1
[sortorder] => 0
[title] => Birdhouse
)
[3] => Array
(
[id] => 10
[status] => 1
[sortorder] => 0
[title] => Shirt
)
[4] => Array
(
[id] => 11
[status] => 1
[sortorder] => 0
[title] => Phone
)
)
// This how the result is if I use array_rand()
print_r(array_rand($slides, 5));
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
// This how the result is if I use shuffle()
print_r(shuffle($slides));
1
shuffle() is the way to go here. It prints 1 because shuffle changes the array in-place and returns a boolean, as it is written in the documentation:
Returns TRUE on success or FALSE on failure.
I suggest to also read the documentation of array_rand():
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
Always read documentation if you use built-in functions. Don't just assume how the work. I bet it took more time to write the question than looking this up.
Instead of
print_r(shuffle($slides));
do
shuffle($slides);
print_r($slides);
You see shuffle() shuffles the array in-place
i am not sure how you want it to display but you can loop the array and use php rand(0,arraylen) function to parse the array.
It works perfect. print_r(shuffle($slides))) gives the output of TRUE, since the return value of shuffle is a boolean and not an array.
See the working example here: http://codepad.org/B5SlcjGf

Categories