About get_records()

About get_records()

על ידי wowow turk בתאריך
מספר תגובות: 17
Hi. What I want to do is to get glossaries from glossary_entries table. For testing, I've set up some Glossary activities on my local machine.

get_records('glossary_entries','glossaryid','1','id,concept,definition')
works fine.

However,
get_records('glossary_entries','glossaryid','2','id,concept,definition')
doesn't work.

While
get_records('glossary_entries','userid','2','id,concept,definition')
works well.

Does anyone know the reason why the second one doesn't work? Thanks!
ממוצע דרוגים: -
בתגובה ל: wowow turk

Re: About get_records()

על ידי wowow turk בתאריך
After adding a new user who is assigned roles as Course creator and teacher, I've add some glossaries with the account.

Unfortunately,
get_records('glossary_entries','userid','4','id,concept,definition')
doesn't work, where the 4 is the userid of the new account.

In my setups, it seems the get_records() command can only work with the default id's, such as 2 for userid or 1 for glossaryid.

Does anyone know how to deal with this issue? Thanks a lot! 満面の笑顔
בתגובה ל: wowow turk

Re: About get_records()

על ידי Jeremy Crabtree בתאריך
Do you have access to the actual database you're using? That is, do you have access to something like phpMyAdmin, or some kind of query browser (like the MySQL GUI Tools, or the similar tool for MSSQL server), if so open it up and actually look at that table in the database. It sounds like the data your looking for either doesn't exist, or doesn't exist where you are looking for it.
בתגובה ל: Jeremy Crabtree

Re: About get_records()

על ידי wowow turk בתאריך
Thanks for your advice, Jeremy!賛成

Yes, I have phpMyAdmin installed on my PC. As the attachment file shows, I think all data I was looking for is sitting there in the table.

get_records('glossary_entries','glossaryid','1','id,concept,definition');
get_records('glossary_entries','userid','2','id,concept,definition');
are working properly.

get_records('glossary_entries','glossaryid','2','id,concept,definition');
get_records('glossary_entries','userid','4','id,concept,definition');
are not working悲しい

Any comments will be appreciated...赤面
צרופה phpMyAdmin.GIF
בתגובה ל: wowow turk

Re: About get_records()

על ידי Iñaki Arenaza בתאריך
תמונה של Core developers תמונה של Documentation writers תמונה של Peer reviewers תמונה של Plugin developers
What do you mean by 'are not working'? Doesn't return anything? Does it returns something not expected? Do you get an error?

Sauldos. Iñaki.
בתגובה ל: Iñaki Arenaza

Re: About get_records()

על ידי wowow turk בתאריך
Thanks for your question!

Those return null, no errors.

I've been building some mini Flash games with Flex Builder and AMFPHP. Actually, one of them are working properly now through
get_records('glossary_entries','userid','2','id,concept,definition');

However, any combination other than 'userid' and '2' have problems, for instance:

count(get_records('glossary_entries','userid','4','id,concept,definition'));
returns 3, which is correct. But
get_records('glossary_entries','userid','4','id,concept,definition');
returns 3*3*nulls, where the latter 3 means to 'id', 'concept' and 'definition'.

count(get_records('glossary_entries','glossaryid','2','id,concept'));
returns 5, which is correct. But
get_records('glossary_entries','glossaryid','2','id,concept');
returns 5*3*nulls.

count(get_records('glossary_entries','glossaryid','3','id,concept'));
return 10, which is correct. But
get_records('glossary_entries','glossaryid','3','id,concept');
returns 10*3*nulls.

Most Interesting One
count(get_records('glossary_entries','glossaryid','1','id,concept,definition'));
returns 13, which is correct. But
get_records('glossary_entries','glossaryid','1','id,concept,definition');
returns 8*3*data AND 5*3*nulls.

The id's of the 8 returned entries are 2, 3, 4, 5, 6, 7, 8, 9 respectively. And the id's of the rest are 26, 27, 28, 29, 30.
/Most Interesting One

Oops... I've just found a problem on userid=2 one...
count(get_records('glossary_entries','userid','2','id,concept,definition'));
returns 26, which is correct. But
get_records('glossary_entries','userid','2','id,concept,definition');
returns 26*3*data AND 3*3*nulls. These nulls have 4 as their userid.

Then
get_records('glossary_entries','userid','2','id,concept,definition');
returns not only those entries which userid is 2, but also those which user id is 4...(actually it dumps all entries on my table...)

H E L P M E . . .複雑な
בתגובה ל: wowow turk

Re: About get_records()

על ידי Martin Dougiamas בתאריך
תמונה של Core developers תמונה של Documentation writers תמונה של Moodle HQ תמונה של Particularly helpful Moodlers תמונה של Plugin developers תמונה של Testers
Are you really trying to sort by id,concept,definition? That seems unusual (the 4th parameter is a sort) though maybe not the cause of the issue.

Still, please try this and let us know:


$results = get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition');
print_object($results);


Note that the first field returned (id) MUST be unique.
בתגובה ל: Martin Dougiamas

Re: About get_records()

על ידי wowow turk בתאריך
Hi, Martin. Thanks for your advice! I didn't realize the 4th parameter was a sort until you gave me directions 赤面 As for the id, they are unique.

Now it's time to try the one you gave me!
get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition');
returns nulls...

get_records('glossary_entries', 'glossaryid', 2, 'id', 'id, concept,definition');
get_records('glossary_entries', 'glossaryid', 2, '', 'id, concept,definition');
get_records('glossary_entries', 'glossaryid', 2, 'id, concept,definition');
all returns nulls like the above one...


More testing
return count($glossary = get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition'));
returns 5

$glossary = get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition');
returns
||||||||||||


return count(get_records('glossary_entries', 'glossaryid', 1, 'concept', 'id, concept,definition'));
returns 13

get_records('glossary_entries', 'glossaryid', 1, 'concept', 'id, concept,definition');
returns
2|第一声|3|第二声|4|第三声|5|第四声|6|茶|7|拿|8|一|9|测试|||||||||||||
/More testing


P.S.
All result is shown on my Flash, because I'm developing my mini Flash games with Flex Builder and AMFPHP. And Flex Builder can't deal with objects given by AMFPHP directly, So I put all elements into one variable before returning it to Flex Builder, like below:

$glossary = get_records('glossary_entries', 'glossaryid', 1, 'concept', 'id, concept,definition');
$Str = "";
for($i=2; $i<=count($glossary)+2; $i++){
$Str .= $glossary[$i]->id . "|";
$Str .= $glossary[$i]->concept . "|";
$Str .= $glossary[$i]->definition . "|";
}
return $Str;

I'm new to programming...
בתגובה ל: wowow turk

Re: About get_records()

על ידי wowow turk בתאריך
Thank you all! I've solved the issue with a stupid approach.

Step 1: Get *ALL* entries in the table
Step 2: Get the needed entries, like below

$Str = '';
$glossary = get_records('glossary_entries','','','','id,glossaryid,concept');
for($i=2; $i<=count($glossary)+2; $i++){
if($glossary[$i]->glossaryid==2){
$Str .= $glossary[$i]->id . "|";
$Str .= $glossary[$i]->concept . "|";
}
}
return $Str;

Bingo! My Flash shows what I wanted now!

Well, I guess my server doesn't like this approach 思慮深い
בתגובה ל: wowow turk

Re: About get_records()

על ידי Martin Dougiamas בתאריך
תמונה של Core developers תמונה של Documentation writers תמונה של Moodle HQ תמונה של Particularly helpful Moodlers תמונה של Plugin developers תמונה של Testers
Hmm, something is really wrong with all this behaviour (but I don't know what could be causing it).

If this sort of problem was happening for all of us then Moodle wouldn't function at all.

Perhaps you can post your whole (old) script here (as a forum attachment) so we can see exactly what you are doing.
בתגובה ל: Martin Dougiamas

Re: About get_records()

על ידי wowow turk בתאריך
Hi, Martin!

My old script was:
$glossary = get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition');
$Str = "";
for($i=2; $i<=count($glossary)+2; $i++){
$Str .= $glossary[$i]->id . "|";
$Str .= $glossary[$i]->concept . "|";
$Str .= $glossary[$i]->definition . "|";
}
return $Str;

That's All! This is a server-side script, thus, PHP.

As for the script on Flex Builder side is somewhat too complicated to copy & past here because of its nature of Flex Builder. FYI, I'm using AMFPHP for communicating with PHP. See here for AMFPHP.

But for testing, all I did on the Flex Builder is just dumping the returned strings on to Flash. So I think the script you introduced will do the same work except for the "|" part, which were added since Flex Builder, or Actionscript, can't access the returned object directly.

What will happen if you add some glossaries with different authors onto several Glossary activities and then execute your script? That is:

$results = get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition');
print_object($results);

Hope you would figure out this odd issue!恥ずかしい
בתגובה ל: wowow turk

Re: About get_records()

על ידי Martin Dougiamas בתאריך
תמונה של Core developers תמונה של Documentation writers תמונה של Moodle HQ תמונה של Particularly helpful Moodlers תמונה של Plugin developers תמונה של Testers
Ah, it's the for loop. $i is "id", not an incrementing number starting from 2 חיוך

Use foreach instead: כן

eg

$str = '';

if ($glossaries = get_records('glossary_entries', 'glossaryid', 2, 'concept', 'id, concept,definition')) {

foreach ($glossaries as $glossary) {
$str .= $glossary->id.'|'.$glossary->concept.'|'.$glossary->definition.'|';
}

}

echo $str;



You may have other problems if your definitions contain | but that's the gist of it.
בתגובה ל: Martin Dougiamas

Re: About get_records()

על ידי wowow turk בתאריך
Hope Martin would help me out once more 笑顔

Your script works perfectly if there is only one condition, say:

if ($glossaries = get_records('glossary_entries', 'userid', 2, 'id', 'id, concept,definition')) { blah blah...
will get me 75 entries, which is correct.

or

if ($glossaries = get_records('glossary_entries', 'userid', 4, 'id', 'id, concept,definition')) { blah blah...
will get me 3 entries, which is perfect.

However
if(($glossaries = get_records('glossary_entries', 'userid', 4, 'id', 'id, concept, definition')) || ($glossaries = get_records('glossary_entries', 'userid', 2, 'id', 'id, concept, definition'))) {blah blah...
will only get me 3 entries, which suppose to be 78 entries.

How to set more than one conditions with this approach? Thanks a lot! 赤面
בתגובה ל: wowow turk

Re: About get_records()

על ידי Dan Poltawski בתאריך
There are number of problems with this:

* Your or (||) will only execute the second statement if the first statement fails to return anything
* You cannot 'concatenate' two arrays of objects in this way
* You are attempting to do something in php which should really be done in sql

What you probably want is:


if( $glossaries = get_records_select('glossary_entries', 'userid = 2 OR userid = 4', 'id', 'id, concept, definiton') ){


But I would strongly recommend getting a good book on php and SQL to try and really understand the fundamentals.
בתגובה ל: Dan Poltawski

Re: About get_records()

על ידי wowow turk בתאריך
Thanks Dan for your advice! 笑顔

Unfortunately the script turned out not to work.

It only retrieves those entries that the first condition meets and the 2nd one, userid = 4 is ignored.


Currently I'm using
$glossary = get_records('glossary_entries','','','','id,,concept,definition');
with for loop and if statement to get what I want to get. With this approach, the server works just as I expected. However, I think foreach is more beautiful ヘトヘト

P.S. If there is a function which just execute sql command in moodle, I'd like to use it...
בתגובה ל: wowow turk

Re: About get_records()

על ידי Mike Churchward בתאריך
תמונה של Core developers תמונה של Plugin developers תמונה של Testers
You really need to spend some time looking into Moodle's database interface. You should check through 'lib/dmlib.php'.

Now I can absolutely guarantee you that the statement:
get_records_select('glossary_entries', 'userid = 2 OR userid = 4', 'id', 'id, concept, definiton')
does not ignore the second condition. If it did, Moodle would not function.

Did you add the '_select' part to the function name?
בתגובה ל: Mike Churchward

Re: About get_records()

על ידי wowow turk בתאריך
Hi Mike!

My bad, I did not add that part...

Thanks for your advice! I will add it and check how it works!

...
It is a little bit hard for me, as a non-programmer, to check through dmlib.php or even whole php and mysql while preparing contents in 3 different languages all by myself...ヘトヘト