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

If you’ve moved across sites from a development to live site or your encountering this error after importing/exporting then here’s the solution that works every time.

1. Search for “catalog_product_flat” . Under my installation its called catalog_product_flat_1

2. Then in mysql run:

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE catalog_product_flat_1

3. Last step open up SSH and run (or do this from the Magento admin)

php path/to/shell/indexer.php --reindex catalog_product_flat or navigate to the /shell directory and run php indexer.php --reindex catalog_product_flat

You should get “Product Flat Data index was rebuilt successfully”

Clean up linux boot partition

If your boot partition is getting full and your running the machine as a server and have no GUI you can run this command to remove all those unecessary kernel images if you update frequently they can take up quite a bit of space.

dpkg --get-selections|grep 'linux-image*'|awk '{print $1}'|egrep -v "linux-image-$(uname -r)|linux-image-generic" |while read n;do apt-get -y remove $n;done

You may also need to update GRUB  /usr/sbin/update-grub after this has completed.

Checking for a whole number in php

There may be a simpler way but I didn’t come across one except this handy function when dealing with decimal numbers.

////////////////////////////////////////////////////////////////////////////////////////////// 
//is_wholeNumber(string $value)
//Returns TRUE if a WHOLE NUMBER
//Returns FALSE if anything else (Float, String, Hex, etc)
//////////////////////////////////////////////////////////////////////////////////////////////   
function is_wholeNumber($value)
{
	if(preg_match ("/[^0-9]/", $value))
	{	return FALSE;	}
	return TRUE;
}

There is also ctype_digit but this seems to return false when using addition like so:

$numeric_string = '1'+'1';
$integer = 1+1;

if(ctype_digit($numeric_string)) { echo "true";} else { echo "false"; }  // false
if(ctype_digit($integer)){ echo "true";} else { echo "false"; }          // false 

if(is_numeric($numeric_string)){ echo "true";} else { echo "false"; }    // true
if(is_numeric($integer)){ echo "true";} else { echo "false"; }           // true

Both equal 2 yet ctype_digit returns false? , is_numeric works here but as i’m dealing with decimal places this returns true when it is not a whole number.

Credit for the function due to http://davidwalsh.name/php-validatie-numeric-digits

Javascript function to return device type

Here’s a simple JS function to return the type of device based on screen size and some other helpful variables for the main manufacturers.

// mobile check
function checkForMobile() {

	var x = screen.width;
	var y = screen.height;
	var agent = navigator.userAgent.toLowerCase();
	var mobileOS = typeof orientation != 'undefined' ? true : false;
	var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
	var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
	var iOS = (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPad") != -1) ? true : false;
	var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;
	var istablet = (/ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()));
	var tablet = (istablet==true && x >= 768) ? true : false;

	// is it mobile

	if(x >= 320) {
		whichDevice = "Mobile";
	}

	if(tablet==true) {

		whichDevice = "Tablet";

	} 	

	if(x >= 800 && tablet==false) {

		whichDevice = "Desktop";

	} 

	return whichDevice;

}
       //sets the device type
	checkForMobile();

Found something better? , please let me know.