Hi! I just create a new block and i want to display some data from the table course in this new block. as i am a beginner in moodle . Can you please explain to me how can extract these data?
thank you a lot.
The Data Manipulation API, used to query the database, is explained in detail on the developer wiki: http://docs.moodle.org/dev/Data_manipulation_API
Thank you
But i tried a hundred of time but it didn't work.
I wrote in a new php page inclued in my block:
<?php
global $DB;
$user = $DB->get_record('user', array('id'=>'1'));
echo $user;
?>
But it didn't display anything!! please help me. thank you
No it still didn't work!! :S
Shall i create a clone table in my new block?? or any other solution?
thank you
Maybe this is a silly question, but I assume you've included the Moodle config file somewhere in your PHP file?
If your PHP file is: [moodlefolder]/blocks/myblock/myfile.php, then at the start of it you should have a line that looks like this:
<?php
require_once(dirname(__FILE__).'/../../config.php');
Otherwise none of the Moodle functions (including all the $DB stuff) are going to be present in your file. In fact, depending on your PHP error output settings, you should be getting lots of 'undefined variable' errors. Note keeping an eye on your webserver error logs is a good idea here (/var/log/apache2/error.log is a typical location with Linux / Apache, but it will depend on your OS / webserver).
Davo
Thank you.
Yep i've put : include_once(dirname(__FILE__).'../config.php');
but when i saw the log file the error is :
[Wed Mar 28 16:50:22 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function get_record() on a non-object in /var/www/moodle/blocks/essaiblock/mainessai.php on line 9, referer: http://localhost/moodle/admin/index.php?adminedit=1
How can i fixe it. I am sorry I am a newbie that's why!!
Thank you a lot
$DB does not exist, which means you haven't included the config file correctly.
At the very least it should read:
require_once(dirname(__FILE__).'/../config.php');
(note the extra slash and always use require_once for config.php, so you get an error message immediately if you've mistyped the path)
The above assumes your path is [moodledir]/yourfolder/yourfile.php
If you've got it in a further subfolder (e.g. [moodledir]/yourfolder/yourfolder2/yourfile.php) then you will need to put:
require_once(dirname(__FILE__).'/../../config.php');
Thanks very much I'll correct it