Model-View-Controller MVC

Model-View-Controller MVC

=> Model
Model is the classes providing data, service related to data and business logic. This class works directly with data and provides data for other elements. In a module, these classes are contained in Model folder.

=> View
View is the classes which define data presented method (not update data). These classes are contained in the folder Block of module.

=> Controller
Controller is the classes which control application stream. They receive input which is requirement of users through HTTP request, transfer those requests to the classes which process the requests directly. From the link, Router will get to Controller which controls that link. In Magento module, these classes are contained in controller folder.

Call phtml file in Magento

1) Magento: call phtml file in CMS page.

When you want to display dynamic content on home page or any other CMS page. You just need to create block of specific module which create an object of that module and call predefined methods of that module to fetch date and call that modules phtml file.

Below is code by using you can call phtml file in CMS page.

{{block type="core/template" template="templateFolder/your_template.phtml"}}

2) Magento: call phtml file in static block.

You can use same syntax as CMS page

{{block type="core/template" template="templateFolder/your_template.phtml"}}

3) Magento: call one phtml file in another phtml file.

To call any phtml fine in another phtml file you just need to add below code in phtml file.

echo $this->getLayout()->createBlock('core/template')->setTemplate('templateFolder/your_template.phtml')->toHtml();

4) Magento: call phtml file using XML file.

To call any phtml file using XML you need to add below code in reference block where you want to show that block.
i.e if you want to add phtml file in footer at that time you need to add below code in <reference name=”footer”> in page.xml

<reference name="footer">
<block type="core/template" name="unique_name" template="templateFolder/your_template.phtml"/>
</reference>

Call static block in Magento

Static block is use to allow store Admin to update content or any block without changing code. Admin can update content from admin-panel easily. We can call static block in different location in Magento. I.e. in CMS page,
any of phtml file, any block etc.

Below are syntax to call static block in different location.

1) Magento: call a static block in a phtml file.

To call a static block in a phtml file you just need to replace static block id in below code. Put this code where you want to call.

echo $this->getLayout()->createBlock('cms/block')->setBlockId('static_block_id')->toHtml();

2) Magento: call a static block in CMS page.

use below code to call static block in CMS page.

{{block type="cms/block" block_id="static_block_id"}}

3) Magento: call static block in xml.

syntex to call static block using xml file

<block type="cms/block" name="home-page-block">
<action method="setBlockId"><block_id>home-page-block</block_id></action>
</block>

Steps to activate logs in magento using admin panel

using below steps you can enable logs in magento

1) Login in magento back-end
2) Go to System -> Configuration
3) from left section click on Advanced -> Developer
4) you can able to see Log Settings in main section click on it
5) Enabled set this option ‘YES’ and click on ‘save’ button on right top corner.

Now you can able to see log files in var/log folder

generate png image using php

To create image using php you can use below code

$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');

Mysql functions

MySQL Group By Clause – The MySQL GROUP BY statement is used along with the SQL aggregate functions like SUM to provide means of grouping the result dataset by certain database table column(s).
MySQL IN Clause – This is a clause, which can be used along with any MySQL query to specify a condition.
MySQL BETWEEN Clause – This is a clause, which can be used along with any MySQL query to specify a condition.
MySQL UNION Keyword – Use a UNION operation to combine multiple result sets into one.
MySQL COUNT Function – The MySQL COUNT aggregate function is used to count the number of rows in a database table.
MySQL MAX Function – The MySQL MAX aggregate function allows us to select the highest (maximum) value for a certain column.
MySQL MIN Function – The MySQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column.
MySQL AVG Function – The MySQL AVG aggregate function selects the average value for certain table column.
MySQL SUM Function – The MySQL SUM aggregate function allows selecting the total for a numeric column.
MySQL SQRT Functions – This is used to generate a square root of a given number.
MySQL RAND Function – This is used to generate a random number using MySQL command.
MySQL CONCAT Function – This is used to concatenate any string inside any MySQL command.
MySQL DATE and Time Functions – Complete list of MySQL Date and Time related functions.
MySQL Numeric Functions – Complete list of MySQL functions required to manipulate numbers in MySQL.
MySQL String Functions – Complete list of MySQL functions required to manipulate strings in MySQL.

Script to get all product magento

Below is the script which display all magento product

create new file in root of the magento source and add below code in that file

require_once('app/Mage.php');
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSort('created_at', 'DESC')
->addAttributeToSelect('*')
->load();
foreach ($_productCollection as $_product){
echo $_product->getId().'</br>';
echo $_product->getName().'</br>';
echo $_product->getProductUrl().'</br>';
echo $_product->getPrice().'</br>';
}

Add JQuery in Magento

To add Jquery in magento we need to add below code in layout file.

/app/design/frontend/package/your theme/layout/page.xml
<reference name="head">
<block type="page/html_head" name="head" as="head">
<action method="addItem">
<type>skin_js</type>
<name>Jquery path with name</name>
</action>
</block>
</reference>

Script to get all category in magento.

External script to Display All category which are created in magento.
Below is the code to display all category.

create new file in root of the magento source and add below code in that file

require_once 'app/Mage.php';
$app = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$categoriesArray = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->load()
->toArray();
$categories = array();
foreach ($categoriesArray as $categoryId => $category) {
if (isset($category['name']) && isset($category['level'])) {
$categories[] = array(
'level' => $category['level'],
'label' => $category['name']
);
}
}
echo "<pre>";
print_r($categories);

Above code will display all categories as below screenshot:
categorylist