Saturday, February 20, 2016

Get Full Product URL Including Category in Magento


Magento is e-commerce open source for based on PHP for creating shopping based website all over the world. If you are Magento developer or need some customization in Magento that must need Magento product page URL for display product anywhere in the website.
For getting product URL in Magento use `getProductUrl()`  function. when you visit product from category listing that it's display full URL with category and when use getProductUrl() function that it's display only product URL and also change breadcrumbs.so that time you must need full product URL.so just create new function in product model.
open file `app/code/local/Mage/Catalog/Model/Product.php` and add below function in `Mage_Catalog_Model_Product` class.


public function getFullProductUrl( $product ){
 if(is_object($product) && $product->getSku()){
 $proArr = $product->getCategoryIds();
 // first try SQL approach
 try{
  $query      = "SELECT `request_path` FROM `".Mage::getConfig()->getTablePrefix()."core_url_rewrite` WHERE `product_id`='" . $product->getEntityId() . "'".((end($proArr)) ? 'AND `category_id`='.end($proArr) : '')." AND `store_id`='" . Mage::app()->getStore()->getId() . "';";
  $read       = Mage::getSingleton('core/resource')->getConnection('core_read');
  $result     = $read->fetchRow($query);
  return Mage::getUrl('').$result['request_path'];
 }
 catch(Exception $e){
  $allCategoryIds     = $product->getCategoryIds();
  $lastCategoryId     = end($allCategoryIds);
  $lastCategory       = Mage::getModel('catalog/category')->load($lastCategoryId);
  $lastCategoryUrl    = $lastCategory->getUrl();
  $fullProductUrl     = str_replace(Mage::getStoreConfig('catalog/seo/category_url_suffix'), '/', $lastCategoryUrl) . basename( $product->getUrlKey() ) . Mage::getStoreConfig('catalog/seo/product_url_suffix');
  return $fullProductUrl;
 }
  }
  return false;
}

Now call below function in theme files by call product model use below code for get full product URL.

 
$getproduct =Mage::getModel('catalog/product')->load($productid); //use $_products->getId();
$getproduct ->getFullProductUrl($getproduct ); 


Here you can also create own extension by extend `Mage_Catalog_Model_Product` class and add below function in that class if you want to not change magento core files.

Solve 'Error displaying the error page: Application Installation Error' Error in Joomla


Joomla is free open source content management system for creating blogger website or web contenting started from 2005.when you working on Joomla 3.x version than sometimes you may be change server or upload from local server to live server that time you fetch configuration problem and display error like 'Error displaying the error page: Application Installation Error'.
This type error because of database connection to server.so check your database connection first.
In Joomla database setting write on `configuration.php` file. so check that file on Joomla root folder.
In that file below match setting for database.

public $dbtype = 'mysqli'; //"mysqli" or "mysql"
public $host = 'localhost'; //server name
public $user = 'root'; //name of database user who access database
public $password = ''; //password of below database user
public $db = 'joomla'; //name of database
public $dbprefix = 'joomla_' //database prefix name 

Check below setting according to your server all value enter correct or not.(If you not sure about than you can check using `mysql_connect` function on any demo file).
All user data you enter correctly than change database type `mysqli` to `mysql` because some server not support mysql database.


Saturday, February 6, 2016

Rename all Files in Folder According to Date in PHP


In any operating System like window, ios or Linux does not support same name files in the same directory so when you try to copy other files with same name it's overwrite or rename new file according to operating system functionality.
Now when you collect many pictures or documents in any specific folder for the long term then you may fetch this problem many times and for that you want to find some solution like separate each file name according to date modify or created. so it's easy to do this using PHP language.
In PHP for get all files in directory use `readdir()` function.get all files list in some specify directory as below.

if ($handle = opendir('.')) { //Here list directory and files of present folder
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
          echo $entry; 
 }
    }
    closedir($handle);
}

Here below you can insert folder path in `opendir()`.for example if you want to get all list from 'News/Info/' directory than enter `opendir('News/Info/')`.Here below display list of all files.now you want date of file for that use `filemtime()` function as below.

if ($handle = opendir('.')) { //Here list directory and files of present folder
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
          echo filemtime($entry); //Here if file in any folder than must put folder name like filemtime('News/Info/'.$entry) 
 }
    }
    closedir($handle);
}

Now you want to rename all listed file according to file time.Note that here below time display as timestamp so you must convert to date time.and for rename any file you must know file extension for that `pathinfo()` function use.so complete code for rename all files according to file date as below.

if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
         $filedate = date('d-M-Y H-i-s',filemtime($entry));
         $extension = pathinfo($entry, PATHINFO_EXTENSION);
  rename($entry,$filedate.'.'.$extension); //If here file is not in code directory than must enter folder name like rename('News/Info/'.$entry,'News/Info/'.$filedate.'.'.$extension); 
 }
    }
    closedir($handle);
}

Here from below date function you can convert file name like `06-Feb-2016 08-10-12` for using PHP date function you can change this format also.