how to get category name from category id? - joomla

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";

Related

How to get category name by category id in Magento

How to get category name by category id in magento on category page?
Thanks in advance please suggest me.
Please use following code for category name by category id. Also get thumbnail etc.
$categoryId = 5; // Change category id according to you or use dynamic category variable
// display name and other detail of all category
$_category = Mage::getModel('catalog/category')->load($categoryId);
echo $categoryName = $_category->getName();
echo $categoryDescription = $_category->getDescription();
echo $categoryUrl = $_category->getUrl();
echo $categoryThumbnail = $_category->getThumbnail();
echo $categoryLevel = $_category->getLevel();
echo $parentCategoryId = $_category->getParentId();
On category page, you can get information like this :
$category = Mage::registry('current_category');
echo $category->getName();
It works in Category Model.
$Category=Mage::getModel('catalog/category')->load($cat);
$cat_name=$Category->getName();

get qty of products in an order with several products

Hi i would like to know how to get the qty_ordered of a specific product in an order with several products.
i have tried these
$orders = Mage::getModel('sales/order')->getCollection()->
addAttributeToSelect('shipping_description')->
addAttributeToSelect('increment_id')->
addAttributeToSelect('base_grand_total')->
addAttributeToSelect('total_qty_ordered')->
addAttributeToSelect('shipping_address_id')->
addAttributeToSelect('billing_address_id')->
addAttributeToSelect('created_at')->
addAttributeToSelect('shipping_incl_tax')->
addAttributeToFilter('status', 'pending');
but addAttributeToSelect('total_qty_ordered')-> which total? which product?
"total_qty_ordered" give total no orders qty of all product in an order
If,you want item qty of product then
you can try
<?php $order_id = 2314; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id); //load order by order id
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){ //item detail
echo $item->getItemId();
//product id
echo $item->getSku();
echo $item->getQtyOrdered();
//ordered qty of item
echo $item->getName();
// etc.
} ?>
for me it worked like this:
<?php
$_order = $this->getOrder(); //call the order
$order_qty = floor($_order->getData('total_qty_ordered')); //get qty of all items rounded to full number (without 3.0000 or so)
echo $order_qty;
?>
used it on template/sales/order/totals.phtml
You can Try this also:-
$orderCollection = Mage::getModel('sales/order');
$order_data = $orderCollection->getAllVisibleItems();
foreach ($order_data as $key) {
$id = $key->getProductId();
$orderItems = Mage::getResourceModel('sales/order_item_collection')
->addAttributeToFilter('product_id', $id);
foreach ($orderItems as $key ) {
echo $number_of_prod = $key->getQtyOrdered();
}
}

Getting Category Active Status from catalog/category model

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();

Getting title of a category that has id 80

$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.

Display category name in template

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.

Resources