DoctorArnar
DoctorArnar

Reputation: 160

Getting title of a category that has id 80

$db = JFactory::getDBO();
$db->setQuery('SELECT title FROM #__categories WHERE id = 80'); 
$category = $db->loadResult();
echo $category;

Can anyone tell me why this does not return the title of category with id 80?

and/or is there a better way of doing this? I have an item that shows the id but not the name/title

Upvotes: 0

Views: 46

Answers (2)

Lodder
Lodder

Reputation: 19733

Try using the following which uses Joomla 2.5 coding standards:

$db = JFactory::getDbo();
$query = $db->getQuery(true);   
$query->select('title')
  ->from('#__categories')
  ->where('id = 80');   
$db->setQuery($query);
$result = $db->loadResult();

echo $result;

As nibra mentioned, you can also check to see if it exists like so:

if($result){
    echo $result;
}
else {
    echo "title with this ID was not found";
}

Upvotes: 3

nibra
nibra

Reputation: 4028

Your code works, if there is a category with the id 80. So

  • if $category is null, there is no such category;
  • if $category is something else, that is the title of your category.

Beide that, the better way to access the database in Joomla! is as Lodder pointed out.

Upvotes: 2

Related Questions