General developer forum

Category Names using PHP

 
Picture of Ryan Carpenter
Category Names using PHP
 

In the context of an activity, the $COURSE module has lots of details, including the course name and the id of the category it belongs to. But, I can't seem to figure out how to programatically identify the name of the course category from that ID. Any suggestions?  Do I need to do a lookup with $DB?

 
Average of ratings: -
Picture of Ryan Carpenter
Re: Category Names using PHP
 

I found this post, which worked for me.

<?php
global $DB;
$category = $DB->get_record('course_categories',array('id'=>$COURSE->category));
echo $category->name     
?>

Edit: I initially thought this wasn't working, but that was only due to displaying it incorrectly.

 
Average of ratings: -