database query in joomla 2.5 virtuemart - joomla

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

Try this:
$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;
To select an id value, you can use $query->select('virtuemart_category_id');

Related

Joomla disable database error

I escape all input with db->escape method on joomla.
For example :
site.com/index.php?view=test&id=5'
In my model ,i use id in my query,
Query code:
$db= JFactory::getDBO();
$id = $db->escape($id);
$query="SELECT name FROM jos_test WHERE id=$id"
$database->setQuery($query);
$result = $database->loadResult();
Now,i add a ' to end of url
Joomla add a backslash before '.
But it's give syntax error and show table name.it is a security problem.
how to disable joomla sql syntax error?
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'' at line 1 SQL=SELECT `name` FROM `jos_test` WHERE id=5\'
You should be parsing your GET parameters using either
JRequest::getVar method if this is an old Joomla 1.5 or
using the JInput class in Joomla 2.5/3+.
$input=JFactory::getApplication()->input;
$id = $input->get('id', 0, 'INT');
This way you can be safe using them with database queries.
Also the correct way to do database queries is described here.
Finally be sure to disable error reporting before going public by editing the configuration.php file. Change the $error_reporting parameter to 'None'.
Try the following:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('name'))
->from($db->quoteName('#__test'))
->where($db->quoteName('id') . ' = ' . $id);
$db->setQuery($query);
$results = $db->loadResult();
This uses up to date coding standards for database queries.
Hope this helps
You should try something like this (from table names i suppose you're using Joomla 1.5):
$db = JFactory::getDBO();
$id = JRequest::getVar('id', 0, 'INT');
$query="SELECT name FROM #__test WHERE id = " . $id;
$db->setQuery($query);
$result = $db->loadResult();
In Joomla 3 you can use query builder and avoid depreached JRequest:
$db = JFactory::getDBO();
$id = JFactory::getApplication()->input->get('id', 0, 'INT');
$query = $db->getQuery(true);
$query->select('name')
->from('#__test')
->where('id = ' . $id);
$db->setQuery($query);
$result = $db->loadResult();

K2 Joomla: How to get category id from a single article page?

I am trying to grab the category id from a single item (article) K2 page. I tried doing:
JRequest::getVar( 'catid' )
But that returns blank. I am running this code from index.php and not item.php, so I don't have access to the $item object.
Any ideas?
for get current itemid modified query is
$item_id = JRequest::getVar('Itemid');
$db = & JFactory::getDBO();
$query = "SELECT catid FROM #__k2_items WHERE id =".$item_id;
$db->setQuery($query);
$result = $db->loadResult();
echo $result;
You could try use the following:
$db = JFactory::getDBO();
$query = "SELECT catid FROM #__k2_items WHERE id = 1";
$db->setQuery($query);
$result = $db->loadResult();
echo $result;
You will need to change id = 1 to whatever the id of your article is.
Please let me know if it works.

Get 'active from' attribute from category in magento 1.7

There are pre-defined attributes in Mangento 1.7 categories named
active from
active to
I can fetch Name of the current product's category using this:
$productId=$_helper->productAttribute($_product, $_product->getId(), 'id');
$product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
but I need to find the active from date of current category also.
How do I fetch this?
It is probably worth noting that these values are for the active state of a custom design for the category, and not the actual active state of the category itself...
As an array together:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$_customDesignDates = $_cat->getCustomDesignDate();
Or individually:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$fromDate = $_cat->getData('custom_design_from');
$toDate = $_cat->getData('custom_design_to');

Pagination in Codeigniter with join

I had my pagination working but then the structure of the database changed so it now doesn’t work because I have to join two tables to get the results I once had…
My db script is:
$query = $this->db->select('*');
$query = $this->db->from('user_entry');
$query = $this->db->join('user_details', 'user_entry.UserID = user_details.id');
$query = $this->db->limit($limit, $offset);
$query = $this->db->get();
If you could help me, I would be so grateful - thanks.
I still don't think there's enough information.. but pagination needs to know the total rows in order to create the links. Assuming this code is in your model and you're calling the model function from your controller in order to set the pagination config variables:
$this->db->select('*');
$this->db->from('user_entry');
$this->db->join('user_details', 'user_details.id = user_entry.UserID');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->num_rows();
in your controller:
$config['base_url'] = 'http://yoururl.com/controller/function';
$config['total_rows'] = $this->model_name->function(); // Model and function to the code above
$config['per_page'] = 10; // # of results you want to display per page
$config['num_links'] = 10; // # of pagination links you want to display
$this->pagination->initialize($config);
in your view where you want the pagination displayed:
<?=$this->pagination->create_links();?>

How to connect the Db and get the data from table in Joomla php

I want to connect the Joomla DB and i have created table in PHPmyAdmin.
And want to get the row and values from the field.
Best way when having a proper JTable:
$row =& JTable::getInstance('my_table', '');
$row->load($id);
When working without JTable:
$db = &JFactory::getDbo();
$sql = 'SELECT * FROM #__table WHERE id ='.$id;
$db->setQuery($sql);
$object = $db->loadObject();
OFC you can make that more elegant inside MVC. BUT for starters maybe enough.
$db = JFactory::getDBO();
$query = "select id,title from #__content";
$db->setQuery($query);
$result = $db->loadObjectList();//here we got array of stdClass===prepared objects in PHP 5.3
foreach($result as $res){
echo $res->id."".$res->title;
}

Resources