Category - PHP/MySQL/AJAX

Get the local IP within PHP on a Raspberry Pi (Raspbian)
8 Reasons why Sublime editor is awesome
Copy Magento Category

Get the local IP within PHP on a Raspberry Pi (Raspbian)

I’m currently working on interfacing a Raspberry Pi with an Arduino. The Pi needs to tell the Arduino it’s IP address on the LAN in order for it to be displayed on an LCD screen.
My code running on the Pi is PHP. It’s usually pretty straightforward to get your local IP address from within PHP.
I just make use of the handy $_SERVER array like so:


failed. SERVER_ADDR wasn’t set. Using my initiative, I threw together the following function, which works a treat:

function getIp()
	exec('/sbin/ifconfig', $resultArray);
	$result = implode(",", $resultArray);
	$ip = preg_match('/eth0.*?inet\saddr:([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})\s/', $result, $matches);
	return isset($matches[1])?$matches[1]:false;

It basically calls the linux command ‘ifconfig’. The IP address is then extracted from the results.
This should work for any occasion where $_SERVER[‘SERVER_ADDR’] isn’t available, as a fallback.
Feel free to use this in your own projects!

8 Reasons why Sublime editor is awesome

After using many PHP/HTML/CSS/JS code editors and IDE’s over the past decade or so (Eclipse, Komodo, Notepad++, Dreamweaver, Netbeans etc etc),
I’m super chuffed that I have finally discovered one which doesn’t drive me round the bend for one reason or another. Sublime text editor is awesome. And I don’t use that word lightly. Here’s 10 reasons why you need to get on it.

1. It’s free
It’s completely free to try for as long as you like. You get a nag popup about once per day, but you can keep trying after that as long as you like. To buy, it’s just $59 per person. You can install it on as many machines as you like.

2. It’s fast
There’s no hanging around with Sublime. It’s fast no navigate between projects and files, fast to search and fast to install addons.

3. It’s light on RAM
I’ve been flicking through projects all morning, I’ve probably edited 50 files so far today and Sublime is using just 70MB of RAM. Netbeans/Eclipse often used to chew up over 500MB.

4. It’s mega extensible
There’s a decent amount of plugins available. I followed a video guide online to install the top addons when I did my initial setup and I found something to do everything I need.

5. It’s easily configurable
Sublime uses two configuration files: Default and User. The Default files holds the current configuration settings in JSON format. To override something, you just copy that setting into the User file, which overrides. The same technique is used for hotkeys, via the ‘key bindings’ configuration files.

6. It’s efficient to navigate
Everything in Sublime can be done from the keyboard, via hotkeys. You can jump between projects, search files, jump to line by number, move screens around, anything. There’s obviously a bit of a learning curve, but within a week you’ll be flipping around like a friggin’ ubergeek using just the tips of your pale fingers.

7. It has plenty of dark themes
I prefer dark themes for coding, they’re much easier on the eye than white once you get used to them. Sublime has loads to choose from.

8. It’s code completion just works
After my frustrations with Netbeans’s’s weird code completion shoving triple speech marks around everything, Sublime offers smooth, intuitive code completion.

So if you find your current code editor/IDE to be resource-hungry, bloated or lacking decent features, you simply have to give Sublime a shot. I’ve been using it for about a month now and I’m mega happy with it.

Copy Magento Category

Since first laying eyes on Magento, the open source eCommerce software, I been impressed by it’s power, flexibility and rigid code structure. But despite Magento’s almost endless list of strengths, it also has some significant weaknesses, that often leave me frustrated and confused. One such weakness is the apparent lack of any functionality in regards to copying products and categories.

I had created a category, with about 10 child categories. Now I just wanted to duplicate that category 5 times, rather than creating another 4 from scratch, each with 10 children (4×10=40 categories!). Rather than spend the next couple of hours adding categories, I knocked together this quick PHP script. Simply pass in the ID of the category you’d like to duplicate, and it will create a copy of the category, along with all of it’s child categories, as deep as they go!

Ideally, this would be a proper Magento module, but I’m just starting out with Magento. Give me 6 months, eh?!

To use the script, simply copy it into a file such as copycat.php in your Magento root directory and call it like so:[CATEGORY ID TO COPY]

if(!is_numeric($_GET['id']))die('Please specify a category ID');
$catId = $_GET['id'];
$xml = simplexml_load_file('app/etc/local.xml');
$host = $xml->global->resources->default_setup->connection->host;
$username = $xml->global->resources->default_setup->connection->username;
$password = $xml->global->resources->default_setup->connection->password;
$dbname = $xml->global->resources->default_setup->connection->dbname; 
$res = mysql_pconnect($host, $username, $password);   
$catsDone = 0;
echo $catsDone . ' Categories duplicated.';
function duplicate_entity($id, $parent_id = null){
	global $catsDone;
	// Grab category to copy
	$sql = "SELECT * FROM catalog_category_entity WHERE entity_id = " . $id;
	$query_entity = mysql_query($sql);
	$entity = mysql_fetch_object($query_entity);
	if(!$parent_id)$parent_id = $entity->parent_id;
	mysql_query("INSERT INTO catalog_category_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count)
						VALUES ({$entity->entity_type_id}, {$entity->attribute_set_id}, {$parent_id}, NOW(), NOW(), '', {$entity->position}, {$entity->level}, {$entity->children_count})");
	$newEntityId = mysql_insert_id();
	$query = mysql_query("SELECT path FROM catalog_category_entity WHERE entity_id = " . $parent_id);
	$parent = mysql_fetch_object($query);
	$path = $parent->path . '/' . $newEntityId;
	mysql_query("UPDATE catalog_category_entity SET path='". $path."' WHERE entity_id=". $newEntityId);
	foreach(array('datetime', 'decimal', 'int', 'text', 'varchar') as $dataType){
		$sql = "SELECT * FROM catalog_category_entity_".$dataType."
				WHERE entity_id=" . $entity->entity_id;
		$query = mysql_query($sql);
		while ($value = mysql_fetch_object($query)){
			mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value)
							VALUES ({$value->entity_type_id}, {$value->attribute_id}, {$value->store_id}, {$newEntityId}, '{$value->value}')");
	$sql = "SELECT entity_id FROM catalog_category_entity WHERE parent_id = " . $id;
	$query = mysql_query($sql);
	while ($entity = mysql_fetch_object($query)){
		duplicate_entity($entity->entity_id, $newEntityId);

Copyright © 2018. Created by Hayden Kibble.