Retrieving Article and Category IDs in a template

109 views
Skip to first unread message

Matt Thomas

unread,
Feb 3, 2011, 11:52:47 AM2/3/11
to joomla-de...@googlegroups.com
Hi,

I was wondering if this is the most efficient way of returning article and category IDs from within a template:

$view = JRequest::getCmd('view');

if ($view == 'category')
$catId = JRequest::getInt('id');

if ($view == 'article')
$articleId = JRequest::getInt('id');

I see that JRequest::getInt('catid'); is used elsewhere, but that returns null when viewing a category blog.

This method above seems to work. Any suggestions?

Best,

Matt Thomas
betweenbrain | Construct Template Framework | Sub-Templates Explained

Randy Williams

unread,
Feb 3, 2011, 12:01:20 PM2/3/11
to joomla-de...@googlegroups.com
Don't know if it's efficient or elegant, but this is what I do:

    <?php
    $db = &JFactory::getDBO();          
      $option = JRequest::getCmd('option');

      $view = JRequest::getCmd('view');
      $temp = JRequest::getString('id');
      $temp = explode(':', $temp);
      $id = $temp[0];
      if ($option == 'com_content' && $id)
          {
            /* Checking if we are making up an article page */

            if ($view == 'article')
        {          
              /* Trying to get CATEGORY title from DB */
              $db->setQuery('SELECT `catid` FROM #__content WHERE id='.$id);  
              $catid = $db->loadResult();
        }
            /* Checking if we are making up a category page */

        if ($view == 'category')
        {          
              /* Trying to get CATEGORY title from DB */
              $db->setQuery('SELECT `catid` FROM #__content WHERE id='.$id);  
              $catid = $db->loadResult();
        }
    /* Printing category title*/
    if ($catid)
    {
      //echo $catid;        
    }
  }              
?>
    <?php
    $menu = & JSite::getMenu();
    $isHome = ($menu->getActive() == $menu->getDefault())?"ishome":"isNotHome";
    $menusectID = "menusectID" . JRequest::getVar( 'Itemid', 0 );
    ?>
</head>
<body class="<?php echo $isHome; ?> <?php echo $menusectID; ?> <?php echo 'categoryid' . $catid; ?>">

I know that's more than what you're seeking.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.



--
Harper Vance Web Services
3425 Grayling Place
Toledo, OH 43623
419-754-6166

Randy Williams
http://harpervance.com
Joomla Development, Hosting, and Consulting

Mark Dexter

unread,
Feb 3, 2011, 12:02:47 PM2/3/11
to joomla-de...@googlegroups.com
You definitely don't want to access the database for something like this. Using JRequest is very fast, since the information is already in memory. Mark

Matt Thomas

unread,
Feb 3, 2011, 1:21:36 PM2/3/11
to joomla-de...@googlegroups.com
Hi Randy and Mark,

Thanks for the responses. I agree that it would be better, from a performance perspective, to avoid database queries.

@Mark is JRequest::getInt('catid'); supposed to return the category ID, or do we need to use JRequest::getInt('id'); in conjunction with views?

Thanks!

Ian MacLennan

unread,
Feb 3, 2011, 2:12:06 PM2/3/11
to joomla-de...@googlegroups.com
It depends on the view.

For article view, you have id and catid.

For category views, you only have id.

Ian

Matt Thomas

unread,
Feb 3, 2011, 2:13:27 PM2/3/11
to joomla-de...@googlegroups.com
Thanks Ian!

It sounds like my initial approach is the way to go then. I appreciate the confirmation.

Phil Snell

unread,
Feb 3, 2011, 2:25:56 PM2/3/11
to joomla-de...@googlegroups.com
I found that catid will not be available with SEF on.  But with the combination of view and id, you can figure out the rest of the ids.

Matt Thomas

unread,
Feb 3, 2011, 2:27:52 PM2/3/11
to joomla-de...@googlegroups.com
Thanks Phil! That may be why it was working before and yet not anymore.

Matt Thomas

unread,
Feb 4, 2011, 9:42:19 AM2/4/11
to joomla-de...@googlegroups.com
Hi Everyone,

As a follow up, I wanted to share some further thoughts on this. Unfortunately, I concluded that the way I was using the combination of view and ID won't work. It doesn't return the proper category ID when viewing an article (and section in 1.5). The final result that seems to work best is along the lines of what Randy proposed:


$articleId = JRequest::getInt('id');

$view      = Jrequest::getCmd('view');

function getCategory($iId) {
    $database = &JFactory::getDBO();
      if(Jrequest::getCmd('view', 0) == "section") {
            return null;
        }
      elseif(Jrequest::getCmd('view', 0) == "category") {
            return JRequest::getInt('id');
        }       
      elseif(Jrequest::getCmd('view', 0) == "article") {
            $temp=explode(":",JRequest::getInt('id'));
            $sql = "SELECT catid FROM #__content WHERE id = ".$temp[0];
            $database->setQuery( $sql );
            $row=$database->loadResult();
            return $row;
        }       
    }
$catId=getCategory(JRequest::getInt('id'));   


        if ($view == 'article')
        echo 'Article ID: '.$articleId.'<br/>';

        if ($catId)
        echo 'Category ID: '.$catId.'<br/>';


Furthermore, I confirmed Phil's finding that JRequest::getInt('catid');  isn't available with SEF enabled.

Mark Dexter

unread,
Feb 4, 2011, 11:39:42 AM2/4/11
to joomla-de...@googlegroups.com
Matt, is this for 1.5 or 1.6? Mark

Matt Thomas

unread,
Feb 4, 2011, 11:50:04 AM2/4/11
to joomla-de...@googlegroups.com
Hi Mark,

This is actually for both :)

Mark Dexter

unread,
Feb 4, 2011, 12:38:48 PM2/4/11
to joomla-de...@googlegroups.com
In looking at this a bit more, it appears that the category id is not saved in the request for an article. So I guess you do need to go back to the database for it. It's too bad, since earlier in the cycle we got the category in the query. But evidently we don't save it anywhere that you can get at it by the time the template index.php file is loaded.

Seems like this could be changed in the future to eliminate the need for another trip to the database. Mark

Matt Thomas

unread,
Feb 4, 2011, 12:46:12 PM2/4/11
to joomla-de...@googlegroups.com
Hi Mark,

Thank you very much for taking a closer look at this. I agree that this presents a great opportunity to improve things. Hence, I opened this ticket earlier today: http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=24752

Mark Dexter

unread,
Feb 4, 2011, 1:00:47 PM2/4/11
to joomla-de...@googlegroups.com
Hi Matt. I'm not sure the best way to accomplish this. We probably don't want to mess with the SEF URL's, so I'm not sure about putting it in the request. One option might be to save the category somewhere in the JDocumentHTML object, which we have access to when the template is loaded.

On the other hand, a quick trip to the database is not the end of the world and may not be a bad option, all things considered.

Are there other things from the component query that would be handy to have available when you are executing the template index.php file?

Mark

Matt Thomas

unread,
Feb 4, 2011, 2:21:58 PM2/4/11
to joomla-de...@googlegroups.com
Hi Mark,

Thanks. I agree that we don't want to mess with the SEF URLs. Saving it on the side, such as in the JDocumentHTML object, is a good idea. Everything else that I normally use is generally readily available without database queries. They include article ID, menu ID, alias, component name, user status... etc.

In the end, this trip to the database isn't a big deal. But, if there are modules and other extensions doing it too, it might be nice to reduce the redundancy, if there is any.

Thanks again for looking at this!
Reply all
Reply to author
Forward
0 new messages