Friday, March 23, 2012

How to call a .phtml block file into another .phtml file in magento

you can call a .phtml block file into another .phtml  like

<?php echo $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/list/related.phtml')->toHtml();
 

?>

Wednesday, March 21, 2012

How to get product attribute in .phtml file


you can Access the product attribute in the .phtml file like...

<?php echo $_product->getResource()->getAttribute('Attribute_code')->getFrontend()->getValue($_product); ?>

Tuesday, February 14, 2012

How To Excute Custom Query In Magento


Magento has provide us very good features for handling or interacting with Database tables. Magento give all the data by default but some time we need to get or insert some custom data.So for this we need to write custom query.
1
2
3
4
5
6
7
<?php
// fetch read database connection that is used in Mage_Core module
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
 
// fetch write database connection that is used in Mage_Core module
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
?>

How to excute custom query in magento

1
2
3
4
5
6
7
8
9
10
<?php
// fetch read database connection that is used in Mage_Core module
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
 
$value=$read->query("SELECT ....");
$row = $value->fetch();
 
echo "<pre>";print_r($row);echo "</pre>"; // As Array
 
?>

Insert custom data in magento tabel

1
2
3
4
5
6
7
<?php
// fetch write database connection that is used in Mage_Core module
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
 
// now $write is an instance of Zend_Db_Adapter_Abstract
$write->query("insert into tablename values ('1','demo','data')");
?>
OR you can wite same as below
1
2
3
4
5
6
<?php
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
// insert
$sql = "INSERT INTO `test_table` (`id`,`name`,`data`) VALUES ('1','demo','data')";
$connection->query($sql);
?>