The Joomla! Forum ™



Forum rules


Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.



Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Fri Oct 05, 2007 4:54 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Aug 22, 2007 6:03 pm
Posts: 10
I need a way to display the category name on every article in the section.  Is there an easy way to do this?

I've been researching, but all I could find were things related to catID (which seems to have disappeared in 1.5) and mainframe (which doens't seem to help).

Thanks!


Top
 Profile  
 
PostPosted: Sat Oct 06, 2007 5:27 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Sep 26, 2007 10:39 pm
Posts: 20
Location: Moscow, Russia
Had a similar problem a couple of days ago http://forum.joomla.org/index.php/topic,216820.0.html

Here is the solution I found (maybe there's a better way):
Code:
<?php
$db = &JFactory::getDBO();            
         
$option   = JRequest::getCmd('option');
$view   = JRequest::getCmd('view');

$temp   = JRequest::getString('id');
$temp   = explode(':', $temp);
$id   = $temp[0];
                     
/* Checking if we are making up an article page */
if ($option == 'com_content' && $view == 'article' && $id)
{            
   /* Trying to get CATEGORY title from DB */
   $db->setQuery('SELECT cat.title FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);   
   $category_title = $db->loadResult();
               
   /* Printing category title*/
   if ($category_title)
   {
       echo $category_title;         
   }               
}
?>

_________________
Please read forum rules regarding signatures: viewtopic.php?t=65


Last edited by maddrive on Sat Oct 06, 2007 5:31 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Sat Oct 06, 2007 11:46 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Aug 22, 2007 6:03 pm
Posts: 10
I actually ended up finding a hack to use in my template, using the $mainframe variable for each page.

Here's what I used:

Code:
<?php $pathway =& $mainframe->getPathway();
$items   = $pathway->getPathWay();
if ($items[0]->name) {
   echo stripslashes(htmlspecialchars($items[0]->name));
} else {  //ie on the homepage
   echo "Welcome!";
?>


Top
 Profile  
 
PostPosted: Tue Dec 16, 2008 7:28 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon May 08, 2006 7:27 pm
Posts: 27
Location: Halifax, Nova Scotia, Canada
Perhaps my solution is far too simplistic, but this is how I'm doing this:
Code:
$contentid = JRequest::getVar('id', '');
$my_query = "SELECT cc.id category FROM jos_content c, jos_categories cc WHERE c.catid = cc.id and c.id = $contentid";
$my_result = mysql_query ($my_query);
if ($my_result)
{
  $my_row = mysql_fetch_assoc($my_result);
  $category = ($my_row[category]);
}
else $category = 0;

_________________
sarah adams
web developer & programmer

Signature rules: Literal URLs only - viewtopic.php?f=8&t=65


Top
 Profile  
 
PostPosted: Tue Dec 16, 2008 9:19 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sat Jan 12, 2008 3:22 pm
Posts: 8
@sarahwbs

well, it works for your site, so it's good for your site, even if it's simplistic :)

It's the same code as [drive]', except that:

- $db->setQuery blabla is the Joomla way to it. For example, my tables have another prefix than jos_ in the configuration (#__ will be replaced by that prefix). Additionally, some may use (some day ?) other Databases then MySQL, as $db aims to be a generic interface for all.

- [drive] takes some other precautions, e.g. making sure that the id is an article (and not a contact, for example)


Top
 Profile  
 
PostPosted: Thu Dec 18, 2008 10:49 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Thu Dec 18, 2008 10:48 am
Posts: 1
This works great for me:
$itemID = JRequest::getVar('Itemid');

global $database;
$id = intval( mosGetParam( $_REQUEST, 'id', 0 ) );
$sql = 'SELECT c.title, s.title, cat.title '
. 'FROM #__categories AS cat, #__content AS c, #__sections AS s '
. 'WHERE cat.id = c.catid AND c.id = ' . $id . ' AND c.sectionid = s.id';
$database->setQuery( $sql );
$row = $database->loadRow();
$title = $row[0];
$section = $row[1];
$category = $row[2];


Top
 Profile  
 
PostPosted: Mon Jan 19, 2009 3:34 pm 
Joomla! Intern
Joomla! Intern

Joined: Fri Mar 30, 2007 4:29 pm
Posts: 65
[drive] wrote:
Had a similar problem a couple of days ago

[drive], how would you modify your code to display the section's name?


Top
 Profile  
 
PostPosted: Wed Jan 28, 2009 6:38 pm 
Joomla! Intern
Joomla! Intern

Joined: Fri Mar 30, 2007 4:29 pm
Posts: 65
I found the solution to my question here. This is how you display the section name in a category blog layout.

http://forum.joomla.org/viewtopic.php?p=1009495#p1009495


Top
 Profile  
 
PostPosted: Fri Feb 06, 2009 1:31 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Wed Apr 30, 2008 5:11 pm
Posts: 3
This is a modification to "[drive]" version, it grabs both the Category alias and Section alias, you can change it yourself if you wish to use the title, just add cat.title, sec.title to the SELECT section of the SQL query. Then add additional variable names and place the items into them from the $category array.


Code:
$db = &JFactory::getDBO();           
$temp   = JRequest::getString('id');
$id      = $temp;
$db->setQuery('SELECT cat.alias, sec.alias FROM #__categories cat LEFT JOIN #__content cont ON cat.id = cont.catid LEFT JOIN #__sections sec ON sec.id = cont.sectionid WHERE cont.id='.$id);   
$category = $db->loadRow();      
if (is_array($category)) {
    $cat = $category[0]; 
    $sec = $category[1];         
}           


This has been tested to work in Joomla 1.5.X only.


Top
 Profile  
 
PostPosted: Wed Mar 04, 2009 4:38 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Dec 16, 2005 12:08 am
Posts: 5
Thanks emodeme, that was really useful

I wanted a quick way to change stylesheet on pages in certain categories, put directly in my template index.php header (not the most efficient but quick)

However when there was no category ID (when someone views the front page) the SQL threw out an error so I added this line into line 3

Code:
if ($temp == '') { $temp = '56';}


item id 56 was something on my front page

Thanks


Top
 Profile  
 
PostPosted: Thu Apr 23, 2009 1:32 pm 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Fri May 02, 2008 10:35 pm
Posts: 366
This is great stuff, but how do I put the same info (category or section name) on category blog pages?

_________________
Best regards, Kristoffer Sandven

http://www.joomlablogger.net/
http://www.twitter.com/joomlablogger


Top
 Profile  
 
PostPosted: Fri Jun 05, 2009 9:01 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Apr 22, 2009 5:03 pm
Posts: 10
I wanted to control a "mini" google map in a module depending on which category (actually location) was selected, and I neede this work on both the category blog and article pages.
Based on the code above and a bit of investigation, I ended up with:
Code:
<?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 cat.title FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);   
      $category_title = $db->loadResult();
    }
    /* Checking if we are making up a category page */
    if ($view == 'category')
    {           
      /* Trying to get CATEGORY title from DB */
      $db->setQuery('SELECT cat.title FROM #__categories cat WHERE cat.id='.$id);   
      $category_title = $db->loadResult();
    }
    /* Printing category title*/
    if ($category_title)
    {
      echo $category_title;         
    }
  }               
?>

Note that the "explode" (from [drive]'s post, but omitted later) is required for those pages which have number:alias in the id parameter.
I'm fairly new to this, so I;d be interested if anyone can offer improvements.
Regards,
Ken


Top
 Profile  
 
PostPosted: Fri Aug 20, 2010 4:47 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Tue Dec 12, 2006 8:37 pm
Posts: 128
[drive] wrote:
Had a similar problem a couple of days ago index.php/topic,216820.0.html

Here is the solution I found (maybe there's a better way):

I also needed to solve this issue, my task was to retrieve page meta keys and/or page title. Here is the code I use (without querying the database)
Code:
    function getTitleMeta() {
       
        $option   = JRequest::getCmd('option');
        $doc =& JFactory::getDocument();
       
        if ($option == 'com_content' && $doc) {     
            $result['metakeys'] = '';
            $result['metakeys'] = $doc->getMetaData('keywords');
            $result['pagetitle'] = '';
            $result['pagetitle'] = $doc->getTitle();
            return $result;
        } else {
            return FALSE;
        }
    }

Maybe it would be useful for somebody.

_________________
www.justPHP.net - HACKS for Hot Property, Custom 404 Error Pages, Templates for Joomla etc.
Developing: www.zerno.org.ua


Top
 Profile  
 
PostPosted: Thu Nov 03, 2011 8:02 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Dec 12, 2008 7:54 pm
Posts: 15
This works in 1.7 also.

To take it a step further, how could we incorporate the Parent level +1 of the top level Cat?

For example, if we have:

Cat A
Sub Cat 1
Sub Cat 2
Sub Cat 3

Any article/category in Sub Cat 1 would show the Sub Cat 1 title, not Cat A


Top
 Profile  
 
PostPosted: Wed Mar 07, 2012 9:30 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Mar 07, 2012 9:09 pm
Posts: 5
I have yhe same pronblem here.. I would like to get the category level 1 name.
Does anyone know the solution?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 



Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group