3

I know how I can retrieve all information from a article in the database. I am trying to do the same thing for a category. And just for clarification, I am talking about when view is equal to category (index.php?option=com_content&view=category). Below is how I can retrieve any information for a article, when I am on a article page, I want to do the same exact thing, but when I am on a category page. Thanks!

$input = JFactory::getApplication()->input;
$articleId = $input->getInt('id');
$article = JTable::getInstance('content');
$article->load($articleId);
$articleTitle = $article->get('title');

3 Answers 3

6

All I had to do was change content to category and I was able to access all data within the category table using the same method I did with the content.

$input = JFactory::getApplication()->input;
$catId = $input->getInt('id');
$cat = JTable::getInstance('category');
$cat->load($catId);
0
0

When you are on a category view (view=category), You can simply retrieve the current category information as follows

$this->category->id;
$this->category->title;
$this->category->alias;
$this->category->description;
1
  • That is only if you are in the actual view file (or the override for that view). I am not in the view like that. If you look at my example code for getting article database information, I am not using $this. Hope that explains it better. Imagine if you created a brand new php file and included it into the index.php of your template, how could you get the category database information inside of that new php file you made. Commented Jun 8, 2015 at 22:10
0

Try to add this function in your PHP file

function getCategory($id) {
    $app      = JFactory::getApplication();
    $database = JFactory::getDBO();
    $jinput   = $app->input;
    if (($jinput->get('view') == "category") || ($jinput->get('view') == "categories")) {
        return $id;
    } elseif ($jinput->get('view') == "article") {
        $sql  = "SELECT catid FROM #__content WHERE id = " . $id;
        $database->setQuery($sql);

        return $database->loadResult();
    }
}

$catId = getCategory($jinput->get('id', '', 'INT'));
7
  • This is just to get the view, which I already know how to do. I must not be explaining this very well. Let me try another approach. If you were to go into PHPMyAdmin, then go to the table categories, see all the columns? I want to be able to get the data from each of those columns when I am on a category page. Commented Jun 8, 2015 at 22:43
  • If you are on a category page (view) then my first answer will work with you. Can you please clarify where exactly you want to put your code and what you want print on your page. Commented Jun 8, 2015 at 23:08
  • 1
    All good, figured it out just instead of 'content' I have to use category. Thank you for trying tho! Commented Jun 9, 2015 at 1:42
  • This will result in $jinput being undefined
    – Lodder
    Commented Jun 9, 2015 at 12:28
  • Just to let everyone know, his answer in this post has nothing to do with my question. He thought I meant I needed to get the view, when I was really trying to get database information from the category table, without actually being in the category view file or override file. And there is a way easier way to get the view. I just use $view = JRequest::getVar('view'); Commented Jun 9, 2015 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.