0

I am new to Joomla. I am trying to write a module which would fetch and display the contents from database. Now for fetching the contents from database, I need to get the current article id and category id of the current article.

I am able to get the id with article title append using JRequest::getVar('id'); ex : 16:abc-def-article-title.

I can sub-string the id out of it but I think there would be a better place of achieving it ? And also I need to get the category id as well.

Also please let me know how can I see what all variables does JRequest::getVar(') holds. Many Thanks !!

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
tiger
  • 671
  • 2
  • 11
  • 30

1 Answers1

4

First, try extract article id by method getInt():

JRequest::getInt('id', 0);

It's more safely, then getVar().

Then, to get category id, you can use this code in module file:

jimport('joomla.application.component.model');
$articlesModel = JModel::getInstance('ContentModelArticle');

$articleId = JRequest::getInt('id', 0);
$article = $articlesModel->getItem($articleId);
$categoryId = $article->catid;

Using Joomla! built-in models instead direct SQL queries is best way to understanding Joomla architecture and writing safety applications that will be easily maintained in the future.

Note: code samples are actually only for Joomla 2.5 (probably and for 1.6, 1.7, but i don't shure).

  • This is great, though throws a fatal error if you're on a category/blog listing page. Where did you find this code, haven't been able to find anything close to this and it would be super useful. – Gisto Sep 27 '12 at 00:52