$db = JFactory::getDBO();
$db->setQuery('SELECT title FROM #__categories WHERE id = 80');
$category = $db->loadResult();
echo $category;
Can anyone tell me why this does not return the title of category with id 80?
and/or is there a better way of doing this? I have an item that shows the id but not the name/title
Try using the following which uses Joomla 2.5 coding standards:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')
->from('#__categories')
->where('id = 80');
$db->setQuery($query);
$result = $db->loadResult();
echo $result;
As nibra mentioned, you can also check to see if it exists like so:
if($result){
echo $result;
}
else {
echo "title with this ID was not found";
}
Your code works, if there is a category with the id 80. So
if $category is null, there is no such category;
if $category is something else, that is the title of your category.
Beide that, the better way to access the database in Joomla! is as Lodder pointed out.
Related
I am new in joomla.In my module i have to give select category option. I tried to get category name from back-end by following code but it doesn't work.Code..
$backId=$params->get('mycategory');
$db->setQuery('SELECT cat.title FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$backId);
$category_title = $db->loadResult();
if ($category_title)
{
echo $category_title;
}
how can i get the category name that i selected.Thanks...
Hope this may be work..
$db = JFactory::getDbo();
$id = jrequest::getint('id');
//match id to cat.id from _categories set to variable $id
$db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.id='$id'");
$category_title = $db->loadResult();
echo "$id";
echo "$category_title";
I am trying to build an array of the active categories on level2.
The problem I have is that the model is returning categories that are no longer active and I do not see a way to filter them.
$storeId = Mage::app()->getStore()->getId();
$category = Mage::getModel('catalog/category')->setStoreId($storeId);
$categoryCollection = $category->getCollection();
$categoryCollectionIds = $categoryCollection->getAllIds();
$level2Categories = array();
foreach($categoryCollectionIds as $categoryId){
$category->load($categoryId);
if($category->getLevel() == '2'){
$level1Categories[$categoryId] = $category->getName();
}
}
echo "<pre>";
print_r($level1Categories);
echo "</pre>";
Any ideas on how to achieve this?
You can filter your category collection to display active categories using the method addIsActiveFilter()
$categoryCollection = $category->getCollection()->addIsActiveFilter();
What would be the code to display articles from category (specified by ID)?
In Wordpress this is fairly easy by doing:
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_title();
endwhile;
?>
I'm looking for similar code for Joomla.
$db = JFactory::getDbo();
$id = 50;//example
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->where('catid="'.$id.'"');
$db->setQuery((string)$query);
$res = $db->loadObjectList();
foreach($res as $r){
echo '<h3>'.$r->title.'</h3>';
echo $r->introtext;
}
You can use next code template:
$categoryId = $[category id];
require_once("components/com_content/models/category.php");
$category = new ContentModelCategory();
$category->hit($categoryId);
$articles = $category->getItems();
print_r($articles);
There is no direct code for getting this in joomla like word press.
If you want to check the code you can check the code for achieving this by following path.
components/com_content/view/category/view.html.php
and
components/com_content/view/category/tmpl/blog.php
According to my Guess your requirement is to display the articles from same category.
then in joomla you dont need to edit in any code.
for achieving this you can simply create a menu.
and menu layout type should be category blog and Choose your category from the right side category options.
This will get the complete article from that category.
If you want to manage it in your style you can add style based on blog.php file .
Hope this will help you..
Try this:
$articles = JFactory::getDBO()->setQuery("SELECT * FROM #__content WHERE catid = '21'")->loadObjectList();
Of course replace '21' with id of your category.
This is quite easy in Joomla. You can find the code in /components/com_content/models/category.php under getItems()
Below is an example
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->setState('params', JFactory::getApplication()->getParams());
$model->setState('filter.category_id', $category_id);
$articles = $model->getItems();
I have inserted the following line into my Joomla template:
<jdoc:include type="component" />
This will bring across the whole post, including the title of the article. How can I bring the featured article title across seperate to it's body/content for styling purposes?
Thank you.
I just tested this code and it seems to work on Joomla 2.5.3:
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
echo $article->get("introtext"); // and/or fulltext
}
$catId = 80;
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "'";
$db = &JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
foreach($articles as $article){
$article_id=$article->id;
$article_title=$article->title;
$article_content=$article->get("introtext");
I am trying to display a category name in a module position.
I tried:
<?php echo $listing['Category']['title'];?>
It did not work.
I followed this link, but it shows the article title and I need the category one.
I'm working on Joomla 1.7.
Much more simple answer:
<?php echo $this->escape($this->item->category_title);?>
As per the posters comment in the OP:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult();
echo $category;
?>
Travega is really close, his code works on pages, but not on category pages.
When you use $id = JRequest::getString('id'); on a category page (such as a category blog or list page) the id of the category is returned. This means we need more context of the id variable, in this case the 'view'.
Here is my modified version of travega's code:
function getCategoryName() {
//Modified from: http://stackoverflow.com/questions/8928967/joomla-display-catagory-name-in-template
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$view = JRequest::getString('view');
if ($view == 'category') {
$sql = "SELECT title FROM #__categories WHERE #__categories.id = $id";
} else {
$sql = "SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = $id";
}
$db->setQuery($sql);
$category = $db->loadResult();
return $category;
}
Other relevant info:
I've only tested this on Joomla 2.5.3 on cat blog and cat list pages. I've not tested it on anything other than com_content component. This means it probably won't work on weblink, contact, etc pages as you may again loose the context.