PHP: Convert string to variable?!

PHP: Convert string to variable?!

PHP: Convert string to variable?!

Problem:

$button0 = "candy";
$button1 = "pop";
$button2 = "nuts";
$button3 = "sugar";

$buttons = 4;
$counter = 0;
while ($counter < $buttons) {
$printbutton = "$"."button".$counter;
echo($printbutton."<br />"); // echos $button0 but not the value of $button0
$counter = $counter+1;
}

What happens is that $printbutton always prints $buttonx (where x = $count) but I want it to print candy or nuts or whatever $buttonx’s value is.

Solution:
(more…)

CakePHP: How to add global variables

CakePHP: How to add global variables

CakePHP: How to add global variables

How to define a global variable in CakePHP which i can use anywhere in the application.

Solution:
You can write a new config value or you can define a variable or constant in bootstrap.php.

I have done it this way

Declaring Global Variable: Configure::write(‘var_name’,’var_value’);
Reading Global Variable: Configure::read(‘var_name’);

Magento: The Best Solution to Ugrade Magento from 1.5.x.x – 1.7.x.x

Magento: The Best Solution to Ugrade Magento from 1.5.x.x – 1.7.x.x

Magento: The Best Solution to Ugrade Magento from 1.5.x.x – 1.7.x.x

I had the same issue and RESOLVED IT by the following
1. Move live site to test/production server

2. UPDATE the following in core_config_data:
web/unsecure/base_url
web/secure/base_url

3. Modify magento/app/etc/local.xml to point to your database

4. Delete * var/cache and var/session

5. Delete the following from the following tables which is what causes the conflict (contain non essential statistical data):
tables: report_compared_product_index & report_viewed_product_index

6. Run the following commands
chmod 755 mage
rm -rf var/cache/* downloader/pearlib/cache/* downloader/pearlib/download/*
./mage mage-setup .
./mage sync –force
./mage config-set preferred_state stable
./mage list-upgrades
./mage install http://connect20.magentocommerce.com/community Mage_All_Latest –force
rm -rf var/cache/* downloader/pearlib/cache/* downloader/pearlib/download/*
chmod 755 mage

7. run this command:
php shell/indexer.php reindexall

This step causes an error but this is normal and only happens on the product price this will be fixed further on. There error you will see is:
Product Prices index process unknown error:
exception ‘PDOException’ with message ‘SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘website_date’ in ‘field list’’ in /home/erjpub/documents/public_html/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/erjpub/documents/public_html/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/erjpub/documents/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/erjpub/documents/public_html/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /home/erjpub/documents/public_html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#4 /home/erjpub/documents/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query(’INSERT INTO `ca…’, Array)
#5 /home/erjpub/documents/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query(’INSERT INTO `ca…’, Array)
#6 /home/erjpub/documents/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(1974): Varien_Db_Adapter_Pdo_Mysql->query(’INSERT INTO `ca…’, Array)
#7 /home/erjpub/documents/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(1947): Varien_Db_Adapter_Pdo_Mysql->insertArray(’catalog_product…’, Array, Array)
#8 /home/erjpub/documents/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price.php(599): Varien_Db_Adapter_Pdo_Mysql->insertMultiple(’catalog_product…’, Array)
#9 /home/erjpub/documents/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Price.php(378): Mage_Catalog_Model_Resource_Product_Indexer_Price->_prepareWebsiteDateTable()
#10 /home/erjpub/documents/public_html/app/code/core/Mage/Index/Model/Indexer/Abstract.php(143): Mage_Catalog_Model_Resource_Product_Indexer_Price->reindexAll()
#11 /home/erjpub/documents/public_html/app/code/core/Mage/Index/Model/Process.php(209): Mage_Index_Model_Indexer_Abstract->reindexAll()
#12 /home/erjpub/documents/public_html/app/code/core/Mage/Index/Model/Process.php(255): Mage_Index_Model_Process->reindexAll()
#13 /home/erjpub/documents/public_html/shell/indexer.php(158): Mage_Index_Model_Process->reindexEverything()
#14 /home/erjpub/documents/public_html/shell/indexer.php(198): Mage_Shell_Compiler->run()
#15 {main}

8. Continue with the update by running the following commands:
./mage upgrade-all –force
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 777 {} \;
chmod 750 mage
chmod o+w var var/.htaccess app/etc
chmod -R o+w media
chmod -R 777 var/package var/locks var/report var/export downloader

9. Enter the url for your site for the update. It will take a while (long while….BE PATIENT) depending on the size of your site.

Spend 3 days trying to get this working and this is the only thing that worked for me.

Problems

Unable to reindex Product Flat Data – Stuck on Processing. ‘SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

The catalog_product_flat_# table is a flat table containing data from EAV product catalog tables.
If you have problem with reindexing the catalog, truncate (drop the content) that table and reindex your data.

It seems that Magento doesn’t clean that table when you delete some information.

In my case, that table was full of old deleted products.

Get the current currency code and symbol in magento

Get the current currency code and symbol in magento

Get the current currency code and symbol in magento

I have tried many times but I didn’t got success . But today I got the solution to show the currency symbol of product price:
Currency Code :

<?php echo $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); ?>

Currency Symbol :

<?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(); ?>

Magento: Getting product type

I got product type following way in phtml file

$product=Mage::getModel('catalog/product')->load($product_id);
$productType=$product->getTypeID();
//Simple Product    
if($productType == 'simple')
{   
  echo "Simple Product";
}                           
//Configurable Product
if($productType == 'configurable')
{   
  echo "Configurable Product";
}

Source:
http://stackoverflow.com/a/12419301

Magento: How do I get attribute set name & id?

Whenever you have a product object, you can access its attribute set like this:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName  = $attributeSetModel->getAttributeSetName();
$attributeSetID  = $attributeSetModel->getAttributeSetID();

This will give you the name of the attribute set, which you can then compare using strcmp:

if(0 == strcmp($attributeSetName, 'My Attribute Set') {
    print $product->getAttributeText('attribute');
}

Hope that helps!
Source:
http://stackoverflow.com/a/2095238

jQuery: How to add ‘onchange’ attribute in a ‘select’ tag?

<select id="sel_id">
</select>

Works for me if I use the native setAttribute().

Also, remember that you need quotes around the selector.

$("#sel_id")[0].setAttribute("onchange", "foo()");

the final html will be

<select id="sel_id" onchange="foo();" >
</select>

Also remember to define foo in the global namespace, and not inside jQuery’s .ready() function.

Source:
http://stackoverflow.com/a/3638016

Magento Dataflow Export/Import form the command line

One issue with Magento is the overhead involved in importing/exporting data which can cause timeouts when run from the web. Here is a script which will run from the command line….

<?php
//THIS SCRIPT JUST INITIALS THE PROFILE TO BE RUN VIA MAGENTO ADMIN "RUN PROFILE IN POPUP". Its the same thing as click just via this file that you can run via cron
$profileId = 8; // SYSTEM - IMPORT/EXPORT - ADVANCED PROFILES <-- you need to go into your magento admin and grab the exact profile ID
 
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
$profile->load($profileId);
if (!$profile->getId()) {
    Mage::getSingleton('adminhtml/session')->addError('ERROR: Incorrect profile id');
}
 
Mage::register('current_convert_profile', $profile);
$profile->run();
$recordCount = 0;
$batchModel = Mage::getSingleton('dataflow/batch');
echo "EXPORT COMPLETE. BATCHID: " . $batchModel->getId();
?>

Source:
http://www.premasolutions.com/content/magento-dataflow-exportimport-form-command-line

Un-Ban Yourself from WHMCS Administration

Here’s how to un-ban yourself:

  1. Log into your cPanel or website database administration panel
  2. Enter phpMyAdmin
  3. Look for a database ending in _whmcs
  4. Browse that database
  5. Browse the table named tblbannedips
  6. Refer to your WHMCS ban notification where it says your banned IP address (such as “Your IP 12.345.12.345″)
  7. Find the entry in the tblbannedips where the ip column matches your IP address
  8. Click the X in that row to delete the ban
  9. Re-access your WHMCS ban-free!

Source:
http://www.seodenver.com/whmcs-banned-ip-address/

Excel: Excel CEILING Function Tutorial

The CEILING function is used to round a number upwards to the nearest multiple of a specified value.

For example, the function can be used to round up the costs of items to the nearest dime ( $ 0.10 ) to avoid having to deal with smaller change such as pennies ($ 0.01 ) and nickels ($ 0.05 ).

The syntax for the CEILING function is:

= CEILING ( Number, Significance )

Number – the value to be rounded.

Significance – the function rounds the Number specified above up to the nearest multiple of this value.
Example: Using Excel’s CEILING Function

For help with this example, see the image above.

Enter the following data into cells D1 to D3: 34.57, 23.82, 31.16

Click on cell E1 in the spreadsheet – this is where the function will be located.

Click on the Formulas tab of the ribbon menu.

Choose Math & Trig from the ribbon to open the function drop down list.

Click on CEILING in the list to bring up the function’s dialog box.

In the dialog box, click on the Number line.

Click on cell D1 in the spreadsheet.

In the dialog box, click on the Significance line.

Type in 0.10 – the number in D1 will be rounded up to the nearest multiple of 0.10.

Click OK.

The answer 34.60 should appear in cell E1 since 34.60 is the nearest multiple of 0.10
( 0.10 * 6 ) higher than 34.57.

Drag the fill handle in the bottom right corner of cell E1 down to cells D2 and D3 to copy the function to those cells.

The cells D2 and D3 should have the answers 23.90 and 31.20 respectively.

When you click on cell E1 the complete function = CEILING ( D1 , 0.1 ) appears in the formula bar above the worksheet.

Source:
http://spreadsheets.about.com/od/excelfunctions/qt/90128_ceiling.htm

Pin It on Pinterest