PHP:

How To Extract SimpleXMLElement Object Value

1

I’m working with Google’s geocode API today and used xpath to extract the data I needed from the XML returned.

$xml = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false');
 
//use xpath to extract lat and long
$latitude = $xml->xpath('/GeocodeResponse/result/geometry/location/lat');		
 
print_r($latitude[0]);

The code above produces the following output:

SimpleXMLElement Object
(
    [0] => 42.3584308
)

The problem is, how I can I just get 42.3584308 so it can be used. No combination of $latitude[0][0] works. After some digging and trying things out, I found the solution was to type cast the object to a string like this

print_r((string)$latitude[0]);

The above code produces 42.3584308. Now you can use the value as needed.

Posted in: Development, PHP
Tags: ,

Continue Reading

Create a symbolic link on Windows

1

Today I created symbolic link to a config file on our server. More specifically, a link to config.php, which exists outside of the document root of our website. Creating the link on Linux was simple, but with windows it was a bit more tricky. I found a good resource here that describes the types of symbolic links for windows http://www.maxi-pedia.com/mklink .

If I have the following directory structure C:\Program Files (x86)\Zend\Apache2\htdocs\application_root\public\ . I want the config.php file to exist within application_root, and I have index.php residing within the public directory. At the top of index.php, I’d write

require('/config.php');

The config.php file is not really at the root of the public directory where PHP would expect it to be, but rather one level deeper within application_root.

To setup the symbolic link, do the following

  1. Open a windows command prompt and change your directory to where you’d like the symbolic link to exist, in my case it is C:\Program Files (x86)\Zend\Apache2\htdocs\application_root\public\
  2. Run the following command mklink config.php “C:\Program Files (x86)\Zend\Apache2\htdocs\themlsonline\config.php”
  3. Open your apache conf file where the virtual host is specified and add “Options Indexes FollowSymLinks” within the node. More on this here http://www.maxi-pedia.com/FollowSymLinks.
Posted in: Apache, Development, PHP

Continue Reading

The value of meaningful identifiers in variables

0

I found the following code today that reminded me of my first attempt at programming in college. I was having a terrible time with it and was convinced I did not want to do programming as a career. Unfortunately, the code I’d written was based off an example in a book, which looked very much like the below code I found today while at work.

function calculate_easter($y) {
	$a = $y % 19;
	$b = intval($y / 100);
	$c = $y % 100;
	$d = intval($b / 4);
	$e = $b % 4;
	$f = intval(($b + 8) / 25);
	$g = intval(($b - $f + 1) / 3);
	$h = (19 * $a + $b - $d - $g + 15) % 30;
	$i = intval($c / 4);
	$k = $c % 4;
	$l = (32 + 2 * $e + 2 * $i - $h - $k) % 7;
	$m = intval(($a + 11 * $h + 22 * $l) / 451);
	$p = ($h + $l - 7 * $m + 114) % 31;
	$EasterMonth = intval(($h + $l - 7 * $m + 114) / 31); // [3 = March, 4 = April]
	$EasterDay = $p + 1; // (day in Easter Month)
	return format_date($y, $EasterMonth, $EasterDay);
}

I can’t make heads or tails of this code and leaves me thinking the person who wrote it was either very smart or very dumb. I’d lean towards the latter.

When I asked my professor for help and showed him my program full of single letter variable names, he said “have you ever heard of meaningful identifiers?”. He should have given me a smack on the head as he said it. Today, I don’t mind writing out long variable names or functions names as long as they “meaningfully” describe the variable or function. Trust me on this – it will make code maintenance much easier in the long run and will make the lives of other developers looking at your code much less painful.

A side note – the author of the above code could have just used PHP’s built in easter_date function http://us2.php.net/easter_date .

Posted in: Development, PHP
Tags:

Continue Reading

PHP Developer Jobs in Minneapolis St. Paul

1

A challenge I’ve found being a PHP developer in the .NET and Java ruled Minneapolis and Saint Paul area is finding good PHP jobs.  I am building a list of companies in the Twin Cities that use PHP or have a need for PHP developers so when the time comes to find work, this list should be very helpful. If you’re willing to work for ten bucks an hour, you can find PHP work in craigslist any day. However, if you need to make a living, this list should help you out.  Please leave a comment with additional companies in the Twin Cities area you’re aware of that employ PHP developers.

Employ Full Time PHP Developers

  1. VISI
    visi.com
  2. Sierra Bravo
    sierrabravo.com
  3. Carging Bridge
    caringbridge.com
  4. Capstone Digital
    capstonepub.com
  5. Ecreativeworks
    ecreativeworks.com
  6. Colle + Mcvoy
    collemcvoy.com
  7. National Bankcard Services, Inc
    nbs-inc.com
  8. Internet Exposure
    iexposure.com
  9. Cazarin
    cazarin.com
  10. Star Tribune
    startribune.com
  11. Digital River
    digitalriver.com
  12. Artropolis
    artropolis.com
  13. The MLS Online
    themlsonline.com
  14. White House Custom Color
    whcc.com
  15. Catch Fire
    catchfire.com
  16. U4EA
    u4eadesign.com
  17. Weather Nation
    weathernation.com
  18. Live Edit
    www.getliveedit.com
  19. Creed Interactive
    www.creedinteractive.com
  20. Vaddio
    www.vaddio.com

Contract or Temporary PHP Developers

  1. Best Buy
    bestbuy.com
    They only hire contractors. Accenture is their main recruiting firm.
  2. Dolan Media dolanmedia.com
    Various recruiting agencies
  3. Regis regissalons.com
    Various recruiting agencies
Posted in: Development, PHP

Continue Reading

Easy caching for PHP

0

I just implemented this cache class
http://www.phpguru.org/static/Caching on one of my sites.

The product detail page
ALR Hyperdrive 3.0 has eight or ten queries that run every time to pull the product info, nutrition info, reviews, manufacturer info, category info, etc…  This cache class provides the ability to store either output cache (html) or data cache (query results) in a file.  Each time the page is loaded, we’ll first check if there is a cached file of the output before running all the queries.  If there is a cache file, we’ll grab it and display.  If not, we’ll run the queries as usual and then save the output to a file.  We’re able to set an expiration on the cache file so it automatically refreshes itself in case the content has changed.

Example for caching output

    if (!OutputCache::Start("myGroup", "myID", 600)) {
 
        // Generate some output (as you do)...
 
        OutputCache::End();
    }

Example for caching data structures and query results

    if (!$data = DataCache::Get("myGroup", "myOtherID")) {
 
        $result = $db->query("SELECT BIG_ASS_QUERY()");
 
        DataCache::Put("myGroup", "myOtherID", 600, $result);
    }
 
    // Do something useful with $result

The page on my site ALR Hyperdrive 3.0 took on average 9 seconds from the time I clicked the refresh button until it was completely loaded.  I added the few lines of code for output caching on the page so it would skip all product related queries and it then averaged 7.5 seconds per page load and database processing and bandwidth in the meantime.  I was hoping the pages would load in two or three seconds, but this is a great improvement for the little effort it took.

Posted in: PHP
Tags: ,

Continue Reading