Press "Enter" to skip to content

Category: Net: Techy: PHP

Posts about the PHP programming language

PHP: Magento – current stock value

If you run the Magento ecommerce shopping cart software and you want to find out how much your stock is worth, how many product lines you have stocked and how many individual items you have, you may find the following MySQL query handy. I’m assuming that you’ve created an attribute with the attribute code “supplier_price” as a decimal, that you’ve kept this field up to date and your stock levels are accurate 😀 If it isn’t called “supplier_price” change the appropriate part in the query. SELECT COUNT(sku) AS ‘products_stocked’, SUM(qty) AS ‘items_in_stock’, SUM(stockvalue) AS ‘stock_value’ FROM ( SELECT catalog_product_entity.sku, cataloginventory_stock_item.qty, catalog_product_entity_decimal.value, (cataloginventory_stock_item.qty * catalog_product_entity_decimal.value) AS stockvalue FROM cataloginventory_stock_item, catalog_product_entity, eav_attribute, catalog_product_entity_decimal WHERE catalog_product_entity.type_id=’simple’ AND cataloginventory_stock_item.product_id=catalog_product_entity.entity_id AND catalog_product_entity_decimal.entity_id=catalog_product_entity.entity_id AND eav_attribute.attribute_id=catalog_product_entity_decimal.attribute_id AND eav_attribute.attribute_code=’supplier_price’ AND cataloginventory_stock_item.qty > 0 ORDER BY sku ASC) AS b

PHP Profilers – A Quick summary

More an aid to memory then anything, here’s the PHP profilers I’ve recently heard about: XDebug by Derick Rethans The oldest and most well known profiler. Needs modification of the server’s PHP file to run. Compatible with KCachegrind/WinCacheGrind, MacCallGrind and Webgrind and remote debugging tools. Produces quite large debug files (typically 900Kb to 10Mb compare to XHProf’s 110Kb). XHProf by Facebook Inc (available via PECL) Large amount of information provided and allows you to “drill down” by various sections – allows comparison of “diffs” between runs to check performance enhancements. Needs modification of the server’s PHP file to run. Also compatible with the *Grind systems. Used by Facebook and Manga High’s online maths games website, but only currently runs on Linux/FreeBSD. Seems to be preferred by iBuildings (see Profiling with XHProf) which is how I originally heard about it. PHP Quick Profiler (PQP) by Particle tree Needs modifications to your PHP code, but does not need the server’s PHP modifying (so it can be used on shared hosting easily). Nice and bright. Used by ParticleTree on their Wufoo product.

PHP: self and clone

I recently had to make a PHP class (let’s call it “class_A”) which returned an array of itself (to return multiple search results). All well and good and I used “$object=new self();” to do so like this: class class_A { public $setting; public function doSomething() { $toReturn=Array(); for ($i=1;$isetting=$i; Array_push($toReturn,$object); return $object; } } Calling $class_A->doSomething() then returns 100 “class_A” objects. However, I then needed to extend the PHP class (let’s call this class_B – declared with “class class_B extends class_A”) to take into account additional settings and I fully expected $object to be a new copy of class_B (as class B is technically the class being run). Nope, it doesn’t work like that. I ended up with $object being class_A . I had to modify the code to change: $object=new self(); to: $object=clone $this; which then allowed $class_B->doSomething(); to return 100 class_B objects. Annoying, but something to remember!

PHP: Extract just the domain name from an URL

Although I posted this on Stack Overflow, but I thought for reference purposes I’ll keep a copy here as well on how to extract the domain name from a URL in PHP: If you just want to handle three character top level domains – then this code works: <?php // let’s test the code works: these should all return // example.com , example.net or example.org $domains=Array(‘here.example.com’, ‘example.com’, ‘example.org’, ‘here.example.org’, ‘example.com/ignorethis’, ‘example.net/’, ‘http://here.example.org/longtest?string=here’); foreach ($domains as $domain) { testdomain($domain); } function testdomain($url) { if (preg_match(‘/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.([A-Za-z]{3})(\/.*)?$/’,$url,$matches)) { print ‘Domain is: ‘.$matches[3].’.’.$matches[4].”.”\n”; } else { print ‘Domain not found in ‘.$url.”.”\n”; } } ?> $matches[1]/$matches[2] will contain any subdomain and/or protocol, $matches[3] contains the domain name, $matches[4] the top level domain and $matches[5] contains any other URL path information. To match most common top level domains you could try changing it to: if (preg_match(‘/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.([A-Za-z]{2,6})(\/.*)?$/’,$url,$matches)) { Or to get it coping with everything: if (preg_match(‘/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.(co\.uk|me\.uk|org\.uk|com|org|net|int|eu)(\/.*)?$/’,$url,$matches)) { etc etc. If you want a list of top level domains, you may find Mozilla’s TLD List useful and DKIM Reputation Detected Registered Domains code handy.