Author Topic: get category id from database  (Read 1726 times)

yureka

  • Beginner
  • *
  • Posts: 5
get category id from database
« on: January 19, 2013, 17:27:59 pm »
Hello, I wrote the following code to get virtuemart category id with name "app" from database. but its not displaying anything.please help. I use joomla 2.5 and latest version of virtuemart
Code: [Select]
$db =&  JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('virtuemart_category_id');
$query->from('#__virtuemart_categories_en_gb');
$query->where('category_name = "app" ');   //put your condition here   
$db->setQuery($query);
$o= $db->loadObjectList();
echo $o[0]->virtuemart_category_id;

my database table prefix is nyhar_ . I wrote the above code in category view page and it doesn't show anything.Please help me.

Peter Pillen

  • Jr. Member
  • **
  • Posts: 223
Re: get category id from database
« Reply #1 on: January 19, 2013, 18:51:40 pm »
I was not able to test this in any way, but maybe it gets you a bit further
Code: [Select]
<?php
$condition="app";

$o = JFactory::getDBO();
$o->setQuery("SELECT virtuemart_category_id FROM #__virtuemart_categories_en_gb WHERE category_name = ".$condition.";");
$o->query();
$records_containing_condition = $o->loadAssoc(); 

echo $records_containing_condition[0];
?>

yureka

  • Beginner
  • *
  • Posts: 5
Re: get category id from database
« Reply #2 on: January 19, 2013, 20:34:35 pm »
Thanks for your answer.. But I guess there is some little fault in it...its not printing anything.  Anyway I got the answer:
Code: [Select]
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__virtuemart_categories_en_gb');
$query->where('category_name = "app" ');   
$db->setQuery($query);
$o= $db->loadResult();
echo $o;