Archive for : October, 2010

Php – read all files and folders from a directory

Read all files and folders from a directory using scandir:


$dir = "/temp";
$files = scandir($dir);
print_r($files);

Read all files and folders from a directory using opendir:


$dir = "/temp/"
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "Filename: " . $file . ": filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}

MySQL – create user and grant all privileges

Create user with mysql command:


CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Add all privilleges to databases for user:


GRANT ALL ON *.* TO 'username'@'localhost';

MySQL – import / export database from / into file

Export database into a file with shell command:


mysqldump -u USERNAME -p PASSWORD database > filename.ext;

Import database from a file with shell command:


mysql -u USERNAME -p PASSWORD database < filename.ext;

Magento – cms tags

Display a magento block in CMS Page:


{{block id='block_id'}}
{{block type='module/package_classname' template='path/to/template.phtml'}}

Escapes all HTML tags in the value of the var attribute, except for those specified in the value of the allowed_tags attribute. (The latter expects a comma separated list of allowed HTML tags).


{{htmlescape var='' allowed_tags=''}}

Inserts HTML layout output. The handle attribute expects the name of a layout handle, e.g. default.


{{layout handle=''}}

Inserts the URL of the media directory.


{{media url=''}}

Inserts the URL of the current theme’s skin directory.


{{skin url=''}}

Inserts the store’s base URL, i.e., the URL of the store’s home page.


{{store url=''}}

Does the same as {{store url=”}} but without appending a slash to the resulting URL.


{{store direct_url=''}}

Source: Markup Tags – CMS Directives

Magento – product attribute

Get attribute collection:


$attribute = $_product->getResource()->getAttribute('attribute_name');

Get attribute type:


$attribute->getAttributeType();

Get attribute Label:


$attribute->getFrontendLabel();

Get attribute default value:


$attribute->getDefaultValue();

Check if the attribute is visible:


$attribute->getIsVisible();

Check if the attribute is required:


$attribute->getIsRequired();

Magento – get product images

This code can be used to get all images for product:


$_product = Mage::getModel('catalog/product');
$_product->load($product_id);
// Get image gallery
$_gallery = $_product->getMediaGalleryImages();
// Go through each image
foreach ($_gallery as $_image)
{
// Display image
echo '<img src=\'' . $_image->getUrl() . '\' alt=\'' . $_image->getLabel() . '\' />';
}

Magento – products block

Go to “CMS – Manage Pages” and select the page do you want to display the products from the list of pages.

Use next code to display products labeled as “new”:


{{block type="catalog/product_new" name="catalog.product.new" alias="new_products" template="catalog/product/new.phtml"}}

Use next code to display all products from catalog:


{{block type="catalog/product_list" name="catalog.product.list" alias="all_products" template="catalog/product/list.phtml"}}

Use next code to display all products from a specific category:


{{block type="catalog/product_list" name="catalog.product.list" alias="category_products" category_id="4" template="catalog/product/list.phtml"}}

PHP – runnig a script in command line

To run a script in command line:


php test.php

or


php -f test.php

To run a script in command line with parameters:


php test.php param1 param2 param3

or


php -f test.php param1 param2 param3

The list of used parameters can be found in:


$_SERVER['argv']

Php – script execution time

To view php script execution time insert next code into your script.

Insert next code at the top of the page:


<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?>

Insert next code at the bottom of the page:


<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "Time execution: ".$totaltime." seconds";
?>