Return to Snippet

Revision: 45000
at April 22, 2011 04:25 by beneberle


Updated Code
function getCategory() {
		/** function for determining the category ID of a given page, referenced from:
		* http://forum.joomla.org/viewtopic.php?f=466&t=302292
		*/
	  	$iId = JRequest::getVar('id',0);
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 0) == "section"){
			return JRequest::getVar( 'id', 0);
		}else if(Jrequest::getVar( 'view', 0) == "category"){
			$sql = "SELECT id 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 catid FROM #__content WHERE id = ".$temp[0];
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}
	}
	
	function getSection() {
		/** 
		function for determining the section alias of a given page
		*/
		$menu = &JSite::getMenu();
		if ($menu->getActive() == $menu->getDefault()){
			return 'home'; 
		}
	  	$iId = JRequest::getVar('id',0);
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 0) == "section"){
				$sql = "SELECT alias "
				. "FROM #__sections WHERE id = ".$iId
				;
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}else if(Jrequest::getVar( 'view', 0) == "category"){
			$sql = "SELECT a.alias "
				. "FROM #__sections as a, #__categories as b "
				. "WHERE (b.id = ".$iId ." "
				. "AND a.id = b.section)"
				;
			$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 #__sections as a, #__content as b WHERE b.sectionid = a.id AND b.id = ".$temp[0];
			$sql = "SELECT a.alias"
				. " FROM #__sections as a, #__content as b "
				. " WHERE (b.id = ".$temp[0] . " "
				. " AND a.id = b.sectionid)"
				;
			$database->setQuery( $sql );
			$row=$database->loadResult();
                        if (!$row)
			{
				return 'general'; // account for 'uncategorized' articles
			}
			return $row;
		}

Revision: 44999
at April 22, 2011 04:14 by beneberle


Updated Code
function getCategory() {
		/** function for determining the category ID of a given page, referenced from:
		* http://forum.joomla.org/viewtopic.php?f=466&t=302292
		*/
	  	$iId = JRequest::getVar('id',0);
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 0) == "section"){
			return JRequest::getVar( 'id', 0);
		}else if(Jrequest::getVar( 'view', 0) == "category"){
			$sql = "SELECT id 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 catid FROM #__content WHERE id = ".$temp[0];
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}
	}
	
	function getSection() {
		/** 
		function for determining the section alias of a given page
		*/
		$menu = &JSite::getMenu();
		if ($menu->getActive() == $menu->getDefault()){
			return 'home'; 
		}
	  	$iId = JRequest::getVar('id',0);
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 0) == "section"){
				$sql = "SELECT alias "
				. "FROM #__sections WHERE id = ".$iId
				;
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}else if(Jrequest::getVar( 'view', 0) == "category"){
			$sql = "SELECT a.alias "
				. "FROM #__sections as a, #__categories as b "
				. "WHERE (b.id = ".$iId ." "
				. "AND a.id = b.section)"
				;
			$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 #__sections as a, #__content as b WHERE b.sectionid = a.id AND b.id = ".$temp[0];
			$sql = "SELECT a.alias"
				. " FROM #__sections as a, #__content as b "
				. " WHERE (b.id = ".$temp[0] . " "
				. " AND a.id = b.sectionid)"
				;
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}

Revision: 44998
at April 22, 2011 04:00 by beneberle


Initial Code
function getCategory() {
		/** function for determining the category ID of a given page, referenced from:
		* http://forum.joomla.org/viewtopic.php?f=466&t=302292
		*/
	  	$iId = JRequest::getVar('id',0);
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 0) == "section"){
			return JRequest::getVar( 'id', 0);
		}else if(Jrequest::getVar( 'view', 0) == "category"){
			$sql = "SELECT id 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 catid FROM #__content WHERE id = ".$temp[0];
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}
	}
	
	function getSection() {
		/** 
		function for determining the section alias of a given page
		*/
		$menu = &JSite::getMenu();
		if ($menu->getActive() == $menu->getDefault()){
			return 'home'; 
		}
	  	$iId = JRequest::getVar('id',0);
		$database = &JFactory::getDBO();
		if(JRequest::getVar( 'view', 0) == "section"){
				$sql = "SELECT alias "
				. "FROM #__sections WHERE id = ".$iId
				;
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}else if(Jrequest::getVar( 'view', 0) == "category"){
			$sql = "SELECT a.alias "
				. "FROM #__sections as a, #__content as b "
				. "WHERE (b.id = ".$iId ." "
				. "AND a.id = b.sectionid)"
				;
			$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 #__sections as a, #__content as b WHERE b.sectionid = a.id AND b.id = ".$temp[0];
			$sql = "SELECT a.alias"
				. " FROM #__sections as a, #__content as b "
				. " WHERE (b.id = ".$temp[0] . " "
				. " AND a.id = b.sectionid)"
				;
			$database->setQuery( $sql );
			$row=$database->loadResult();
			return $row;
		}

Initial URL
http://forum.joomla.org/viewtopic.php?f=466&t=302292

Initial Description
getCategory returns the category ID, getSection returns the section alias. Great for CSS overrides

Initial Title
Joomla 1.5 - Get Category and Section Functions - Template Tools

Initial Tags
joomla

Initial Language
PHP