How to get section id?

Everything to do with Joomla! 1.5 templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Locked
DoRullings
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Sat Aug 26, 2006 11:18 am
Location: Norway

How to get section id?

Post by DoRullings » Mon Jun 23, 2008 6:20 pm

Hi

Can anyone tell me how I can get the current section id in Joomla 1.5. There must be an easy way to do this, but I've search for two hours without finding any documentation that have pointed me to an solution yet.

Regards
Thomas

pauldrijs
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 129
Joined: Sat May 10, 2008 10:33 pm

Re: How to get section id?

Post by pauldrijs » Mon Jun 23, 2008 11:12 pm

Not sure if this is best way. Assuming you need to get it dynamically in code. I can give you a pointer but I do not have the coding expertese to say how to do the code. Basically it is in the site sql database under jos_sections (you can see this using phpMyAdmin). Each section is listed in that table with its name and attributes etc. There are examples of code on how to extract info from the sql database in http://www.phpeveryday.com/articles/Joo ... --P78.html .I don't understand this stuff myself yet but am learning!

Of course if you are not looking for it dynamically then it is a column in the backend control panel under "Section Manager".

Hope it helps
Paul.

DoRullings
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Sat Aug 26, 2006 11:18 am
Location: Norway

Re: How to get section id?

Post by DoRullings » Tue Jun 24, 2008 10:24 pm

Hi pauldrijs

Thanks for your reply!

I've been looking at your way to do it, and I found a way to get the section id with this query:

Code: Select all

			
function getSection($iId) {
$database = &JFactory::getDBO();
			
$sql = "SELECT s.name,s.title ";
$sql .= "FROM #__sections as s join #__content as c on c.sectionid = s.id ";
$sql .= "WHERE c.id = $iId ";
			
$database->setQuery( $sql );
return $database->loadRow();
}
where I can get the section id by send query by content id, but I have no idea how to get the content id.

It should be an easy way to get the section id for template makers, like for instance this way:

Code: Select all

$mainframe->sectionid
Does anybody know if this is possible in any way or can help me out with the code snippet above?

Thanks,
Thomas

User avatar
bullettime
Joomla! Apprentice
Joomla! Apprentice
Posts: 42
Joined: Fri Jun 06, 2008 12:58 pm
Contact:

Re: How to get section id?

Post by bullettime » Thu Jun 26, 2008 11:40 am

Yes, there is a easy way. Just get in the Section manager and check the last column titled "Section ID"

This is what you wanted?
"Setting an example is not the main means of influencing others; it is the only means." (Albert Einstein)
My projects about technology news - http://www.foxtech.org and about [SPAM] Arena - http://warcraftarena.net

DoRullings
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Sat Aug 26, 2006 11:18 am
Location: Norway

Re: How to get section id?

Post by DoRullings » Thu Jun 26, 2008 12:04 pm

Hi bullettime.

No, that is not what I want. I need to get it dynamically into the template so I can show content based on section ID. Thanks for your answer anyway though!

I have to say that I find it strange that no one cares to tell me this. I know this is an open source community and everyone is doing this on their spare time, but this simple information seems awfully hard to get. Is this some kind secret trick that no one want to share, or does it not exist? ;)

Please help me anyone!

Thanks,
Thomas

jfox77
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Sun Feb 03, 2008 6:41 am

Re: How to get section id?

Post by jfox77 » Thu Jul 10, 2008 2:22 pm

I've racked my brain on this one several times and have yet to find a simple solution. You would really think this is some sort of global variable that you could easily pull within your code!

It's not super pretty, but this worked for me:

Code: Select all

					<?php 
					$query = 'SELECT sectionid FROM #__content WHERE id = ' . $id; 
					$database->setQuery( $query ); 
					$section_id = $database->loadResult(); 
					
					// now check the section id:
					if ($section_id == 5){
						mosLoadModules('user5',-1);	//display my special subnav module for pages within this section			
						mosMainBody(); //display the actual page content
					}
					else{
						mosMainBody(); //just display the actual page content and nothing else
					}
					?>
Last edited by jfox77 on Mon Jul 14, 2008 9:56 am, edited 1 time in total.

DoRullings
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Sat Aug 26, 2006 11:18 am
Location: Norway

Re: How to get section id?

Post by DoRullings » Sat Jul 12, 2008 10:40 pm

Thanks jFox: I' m on holiday, but will check it out as soon as I'm back. Thanks mate!

columnae74
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Aug 05, 2008 1:30 pm

Re: How to get section id?

Post by columnae74 » Tue Aug 05, 2008 1:37 pm

Hi ppl,

this is what i used (Joomla 1.5.5) to use the section ID (or the section TITLE if you replace s.id with s.title in the SQL query):

<?php
function getSection($iId) {
$database = &JFactory::getDBO();

$sql = "SELECT s.id ";
$sql .= "FROM #__sections as s join #__content as c on c.sectionid = s.id ";
$sql .= "WHERE c.id = $iId ";
echo $sql;
$database->setQuery( $sql );
$row=$database->loadResult();
return $row;
}
$section=getSection(JRequest::getInt( 'id'));
?>

in fact, this way I can make a style sheet for each section.
Assigning a different template or menu item for each article was such a pain in the a$$.

So I added this in in the <head> segment of the template:
<link rel="stylesheet" href="<?php echo $tmpTools->templateurl(); ?>/sectionstyles/<?php echo $section; ?>.css" type="text/css" />

of course, you can also display the section ID/title by using a simple
echo $section;

works like a charm.
Have phun.
Last edited by columnae74 on Tue Aug 05, 2008 2:44 pm, edited 3 times in total.

andret
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Aug 05, 2008 12:33 pm

Re: How to get section id?

Post by andret » Tue Aug 05, 2008 2:05 pm

i want to basically do this: <?php echo $item->section_id; ?> (but on the homepage).

This is the complete code but it doesnt show me the section id:

<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<ul class="latestnews<?php echo $params->get('moduleclass_sfx'); ?>">
<?php foreach ($list as $item) : ?>
<li class="latestnews<?php echo $params->get('moduleclass_sfx'); ?>_<?php echo $item->sectionid; ?>">
<a href="<?php echo $item->link; ?>" class="latestnews<?php echo $params->get('moduleclass_sfx'); ?>">
<?php echo $item->text; ?></a>
</li>
<?php endforeach; ?>
</ul>

I'd appreciate help on this one too

André

jfox77
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Sun Feb 03, 2008 6:41 am

Re: How to get section id?

Post by jfox77 » Tue Aug 05, 2008 3:18 pm

Hi Andre,
Keep in mind that the "homepage" is actually a component that is made up of one or more other pages that have been flagged to display on the frontpage. It's not really a "page" itself.

So, you may need to grab the list of all of the id's that are set to display on the front page and then get the section id for each of those actual pages (even if there is only one page set to display on the front page).

Good luck!
Julie

andret
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Aug 05, 2008 12:33 pm

Re: How to get section id?

Post by andret » Tue Aug 05, 2008 3:46 pm

Hi Julie,

Many thanks for that information! At the moment, from the code, you can see that each item is auto generated depending on if its set to show on the homepage. How can i pull each individual item's section id as well as the information about that post (text, link etc).

I'm still very new to joomla so im not sure on how things work logically yet :)

Thanks in advance once again!

André

User avatar
sem101
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Jun 28, 2006 9:43 pm
Location: NY

Re: How to get section id?

Post by sem101 » Wed Aug 06, 2008 3:49 pm

I'm having a hard time getting the Section ID also. My Section ID for my "Blog" section is ID 3. I wish to simply NOT show page titles IF the user is in the Blog section ID 3 (because some article titles can get really long, if you must know ;)). Repeat: ID 3! Haha, SECTION ID 3! Oh, and I do not wish to query the DB.

Code: Select all

<?php if (JRequest::getVar('view') == 'com_content' && JRequest::getVar('sectionid') != 3) { 
	echo $defaultPageTitleFunction; 
        } else { 
         echo "Very CUSTOM Blog Titles"; 
	} ?>
I've tried "secid","sectionid","section_id","whatsmysectionid.com" -- Nothing works :( Help? TIA
Joomla 1.5.14, MySQL 5.0.45, PHP 5.2.6

hlarsen
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Fri Sep 15, 2006 6:46 pm

Re: How to get section id?

Post by hlarsen » Wed Aug 06, 2008 4:15 pm

Hi,

I'm using this in my template -

Code: Select all

$secid = JRequest::getVar( 'id', 0 );
Cheers

andret
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Aug 05, 2008 12:33 pm

Re: How to get section id?

Post by andret » Wed Aug 06, 2008 5:40 pm

Ok so far no1 has actually posted the answer to this question.

Once again, I have a number of modules showing on the homepage (different pages in different areas) and i have a latest news section. Each page that is highlighted to show on the homepage in this area falls within a different section.

Here is the code:
<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<ul class="latestnews">
<?php foreach ($list as $item) : ?>
<li class="latestnews">
<a href="<?php echo $item->link; ?>" class="latestnews">
<?php echo $item->text; ?></a>
</li>
<?php endforeach; ?>
</ul>

i want each item (there are 6 in total) to ECHO the Section ID that the page belongs to in the overall hierarchy so that i can style each news item using CSS.

For example, my output will be <li class="latestnews_SECTIONID"> and EACH latestnews_SECTIONID will be in my css files so that if the ID is 5, it makes that news item yellow, if its 6, then pink etc.

Surely this cant be that hard! It should not matter whether you are trying to display the section id of a page anywhere within your site, the page still belongs to a section.

The code, in my mind, LOGICALLY would be something like: <?php echo $item->section_id; ?> as the post has to belong to a section.

Someone with the knowledge should be able to help.

I look forward to constructive help and criticism as i can see there are others on this post that are still at a loss like myself.

Thanks in advance
André

renatoi
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Sep 01, 2008 3:51 pm

Re: How to get section id?

Post by renatoi » Mon Sep 01, 2008 3:56 pm

I also tried to get the section ID without doing SQL, but I couldn't find a simple solution.

Using SQL, you might want to get the SECTION ID if you are in a category content or in an article content. So I made this dirty solution and it worked for me:

Code: Select all

function getSection($iId,$view){
	$database = &JFactory::getDBO();
	$sql = "SELECT s.id ";
	if($view == 'category'){
		$sql .= "FROM #__sections as s join #__categories as c on c.section = s.id ";
	} else {
		$sql .= "FROM #__sections as s join #__content as c on c.sectionid = s.id ";
	}
	$sql .= "WHERE c.id = $iId ";
	$database->setQuery( $sql );
	$row=$database->loadResult();
	return $row;
}
$section=getSection(JRequest::getInt('id'),JRequest::getVar('view'));
echo ">>>> ".$section;
Hope it helps someone.

DoRullings
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Sat Aug 26, 2006 11:18 am
Location: Norway

Re: How to get section id?

Post by DoRullings » Tue Sep 02, 2008 7:42 am

Hi all

I came up with a solution very similar to renatois, but why are you using joins in your query?

Here is my solution:

Code: Select all

	function getSection($iId) {
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 'NONE') == "section"){
			return JRequest::getVar( 'id', 'NONE');
		}else if(Jrequest::getVar( 'view', 'NONE') == "category"){
			//echo 'hit category';
			$sql = "SELECT section FROM #__categories WHERE id = $iId ";
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}else if(Jrequest::getVar( 'view', 'NONE') == "article"){
			//echo 'hit article';
			$sql = "SELECT sectionid FROM #__content WHERE id = $iId ";
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}
	}
	$sectionID=getSection(JRequest::getVar( 'id', 'NONE'));
This is quick and dirty and could be optimized.

I still think it shouldn't be necessary to query the database to get the section id, or most other ids for that matter. Is there any of the core developers or someone else with this knowledge who can give us an idea if this is the correct way to do this?

Cheers,
Thomas

renatoi
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Sep 01, 2008 3:51 pm

Re: How to get section id?

Post by renatoi » Tue Sep 02, 2008 12:57 pm

I don't know if what I did makes sense, but I tried using:

get_defined_vars();
get_defined_functions();
get_defined_constants();

They are native functions from PHP which brings all defined variables, functions or constants. None of them has brought me any information of sectionID. Maybe we need some extension to do something like that or Joomla Development Team really needs to make this native from JOOMLA.

User avatar
psantamaria
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 208
Joined: Tue Aug 26, 2008 8:14 pm
Location: Brisbane, Australia
Contact:

Re: How to get section id?

Post by psantamaria » Tue Sep 23, 2008 1:31 pm

Hi, I've been looking for a "non query data base solution" but I can't find it. So I used DoRulling solution with some little changes:

Code: Select all

function getSection($iId) {
  $database = &JFactory::getDBO();
  if(JRequest::getVar( 'view', 0) == "section"){
	 return JRequest::getVar( 'id', 0);
  }else if(Jrequest::getVar( 'view', 0) == "category"){
	 $sql = "SELECT section FROM #__categories WHERE id = $iId ";
	 $database->setQuery( $sql );
	 $row=$database->loadResult();
	 return $row;
  }else if(Jrequest::getVar('view', 0) == "article"){
	 $temp=explode(":",JRequest::getVar('id',0));
	 $sql = "SELECT sectionid FROM #__content WHERE id = ".$temp[0];
	 $database->setQuery( $sql );
	 $row=$database->loadResult();
	 return $row;
  }
}
$section_id=getSection(JRequest::getVar('id',0));
DoRulling solution worked for me at sections and categories, but not at articles, so I exploded the var to make it work properly.

Please if someone find something like getSectionId() please, let us know.

wztech
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Sun Sep 14, 2008 2:41 am

Re: How to get section id?

Post by wztech » Wed Oct 01, 2008 4:20 pm

There is a module we developed that shows what section Id and/or category Id a visitor is currently viewing. We have not publish it on JED yet. But once we publish the extension you can take a look at the code and get idea how to get section Id or even category Id on any joomla page. If you want to get update just sign up the newsletter on the site, we will send you the update once the module is published on JED.

Here is the link to the module demo (it is at the right column to the end of the page titled "Page Location Finder")
http://wzcreativetechnology.com/

vcardillo
Joomla! Intern
Joomla! Intern
Posts: 96
Joined: Thu Sep 13, 2007 12:57 am
Location: New York, USA
Contact:

Re: How to get section id?

Post by vcardillo » Fri Aug 14, 2009 6:55 pm

*posted in wrong thread*
- Vince Cardillo

whatsupmf
Joomla! Apprentice
Joomla! Apprentice
Posts: 14
Joined: Thu Jun 05, 2008 6:52 pm

Re: How to get section id?

Post by whatsupmf » Wed Nov 04, 2009 5:55 pm

Here's how I like to get the section id from within an article:

$db =& JFactory::getDBO();
$thisArticle=JRequest::getInt( 'id');
$query = 'SELECT sectionid FROM #__content WHERE id = ' . $thisArticle;
$db->setQuery($query, 0, 1);
$thisSectionID = $db->loadResult();
echo $thisSectionID;

tmancini
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Jun 25, 2009 5:06 pm

Re: How to get section id?

Post by tmancini » Mon Nov 30, 2009 8:51 pm

whatsupmf wrote:Here's how I like to get the section id from within an article:

$db =& JFactory::getDBO();
$thisArticle=JRequest::getInt( 'id');
$query = 'SELECT sectionid FROM #__content WHERE id = ' . $thisArticle;
$db->setQuery($query, 0, 1);
$thisSectionID = $db->loadResult();
echo $thisSectionID;
Worked like a charm, thanks :)
You've also indirectly solved "How do I get the ID of the current Article?"

Like so: $thisArticle=JRequest::getInt( 'id');

kokoruz
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Dec 11, 2009 4:42 pm

Re: How to get section id?

Post by kokoruz » Fri Dec 11, 2009 5:02 pm

Using the solution that "DoRulling" provided seems to work in every instance except when Joomla displays Category content as a list. Not sure why it should matter as it still grabs the view and the id from the url string but I can't figure out why this view is causing me such grief. Any insight or help would be greatly appreciated.
function getSection($iId) {
$database = &JFactory::getDBO();
if(JRequest::getVar( 'view', 0) == "section"){
return JRequest::getVar( 'id', 0);
}else if(Jrequest::getVar( 'view', 0) == "category"){
$sql = "SELECT section FROM #__categories WHERE id = $iId ";
$database->setQuery( $sql );
$row=$database->loadResult();
return $row;
}else if(Jrequest::getVar('view', 0) == "article"){
$temp=explode(":",JRequest::getVar('id',0));
$sql = "SELECT sectionid FROM #__content WHERE id = ".$temp[0];
$database->setQuery( $sql );
$row=$database->loadResult();
return $row;
}
}
$section_id=getSection(JRequest::getVar('id',0));

User avatar
betweenbrain
Joomla! Guru
Joomla! Guru
Posts: 801
Joined: Wed Feb 28, 2007 5:40 am
Location: Connecticut, USA
Contact:

Re: How to get section id?

Post by betweenbrain » Wed Dec 16, 2009 7:42 pm

kokoruz wrote:Using the solution that "DoRulling" provided seems to work in every instance except when Joomla displays Category content as a list.
It seems as though psantamaria's solution does work in this situation. Just tested fine.
psantamaria wrote:

Code: Select all

function getSection($iId) {
  $database = &JFactory::getDBO();
  if(JRequest::getVar( 'view', 0) == "section"){
	 return JRequest::getVar( 'id', 0);
  }else if(Jrequest::getVar( 'view', 0) == "category"){
	 $sql = "SELECT section FROM #__categories WHERE id = $iId ";
	 $database->setQuery( $sql );
	 $row=$database->loadResult();
	 return $row;
  }else if(Jrequest::getVar('view', 0) == "article"){
	 $temp=explode(":",JRequest::getVar('id',0));
	 $sql = "SELECT sectionid FROM #__content WHERE id = ".$temp[0];
	 $database->setQuery( $sql );
	 $row=$database->loadResult();
	 return $row;
  }
}
$section_id=getSection(JRequest::getVar('id',0));
Best,

Matt
Best,

Matt Thomas

val10
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Aug 13, 2010 8:22 am

Re: How to get section id?

Post by val10 » Fri Aug 13, 2010 10:18 am

Can anyone help me how to get the state abbreviation from my sections. I have a new column for 50 US states (these states are my sections) and that column in database is state_abr

My sections are from 1-50 and I need to use either the section id or section abbreviation (state_abr) inside a javascript code which shows content on my site (my signature).

My detailed problem is here.
http://forum.joomla.org/viewtopic.php?f=469&t=540815

Please see the screenshot.
You do not have the required permissions to view the files attached to this post.

User avatar
betweenbrain
Joomla! Guru
Joomla! Guru
Posts: 801
Joined: Wed Feb 28, 2007 5:40 am
Location: Connecticut, USA
Contact:

Re: How to get section id?

Post by betweenbrain » Wed Nov 17, 2010 1:00 am

DoRullings wrote:

Code: Select all

	function getSection($iId) {
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 'NONE') == "section"){
			return JRequest::getVar( 'id', 'NONE');
		}else if(Jrequest::getVar( 'view', 'NONE') == "category"){
			//echo 'hit category';
			$sql = "SELECT section FROM #__categories WHERE id = $iId ";
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}else if(Jrequest::getVar( 'view', 'NONE') == "article"){
			//echo 'hit article';
			$sql = "SELECT sectionid FROM #__content WHERE id = $iId ";
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}
	}
	$sectionID=getSection(JRequest::getVar( 'id', 'NONE'));
For anyone who may be using this solution, I was recently notified that the use of

Code: Select all

getVar
could lead to an exploit. The revised code would be something like:

Code: Select all

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

Matt Thomas

User avatar
ahmad
Joomla! Guru
Joomla! Guru
Posts: 902
Joined: Fri Apr 07, 2006 4:02 pm
Location: Egypt
Contact:

Re: How to get section id?

Post by ahmad » Tue Oct 11, 2011 12:41 pm

@sem101
I did try to go to whatismysectionid.com !!
ROFL


Locked

Return to “Templates for Joomla! 1.5”