Creating a Closest Store Locator in PHP

Over the past year, one of my side projects has been the development of shopping directory sites for categories such as wargames, sports cards, videogames and farmers markets, the last of which I launched over the weekend. The sites are running on LAMP (Linux, Apache, MySQL and PHP) using my own code and the Smarty template language, which keeps me from cluttering up my web pages with PHP.

PHP elephant iconAs I prepared the newest site, I decided to implement a feature that takes a user-submitted address and finds the closest stores. This functionality was the original impetus for the project -- I thought it would be cool if Wargames.Com had a store locator that could find the closest wargame store when I'm out of town.

To accomplish this, I needed to split latitude and longitude into their own fields in the MySQL database and use the following SQL query to find the closest stores to a user-submitted latitude and longitude:

SELECT *, (3959 * acos(cos(radians({$user_latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians({$user_longitude})) + sin( radians({$user_latitude})) * sin(radians(latitude)))) AS distance FROM stores HAVING distance < 250 ORDER BY distance LIMIT 0, 10

This query, which I found in a PHP/MySQL tutorial by a Google Maps engineer, employs the Haversine formula to compute distances between two pairs of coordinates on a sphere. The fields latitude and longitude are from the MySQL database. The PHP variables $user_latitude and $user_longitude contain the coordinates of the user address.

An address can be specified many different ways, but most people don't know the latitude and longitude of their location. Fortunately, Google Maps offers a web service that can take an address in a wide variety of formats and attempt to determine its latitude and longitude. The web service, which is a simple URL request, returns the information in either XML or JSON format. It requires a Google MAPS API key:

http://maps.google.com/maps/geo?q=ADDRESS&output=json&oe=utf8&sensor=false&key=YOUR+API+KEY

Plug the address and your API key into the request, changing "json" to "xml" if you want XML data. Here's example output for Disney World in JSON and XML.

I chose JSON over XML because it's easier to work with in PHP. PHP 5 has built-in support for JSON, but my sites are on a server running PHP 4, so I installed the Services_JSON library. After a brutal hour of trial and error that made me question programming as a lifestyle choice, I figured out that the following four lines of PHP code will pull a latitude and longitude out of Google's JSON address data:

$json = new Services_JSON();
$json_data = $json->decode($this->get_web_page($url));
$addr_latitude = $json_data->Placemark[0]->Point->coordinates[1];
$addr_longitude = $json_data->Placemark[0]->Point->coordinates[0];

The get_web_page() function returns the contents of a web page as a string.

I've added the closest-store search to all four sites, which you can try on the home page of Sportscard-Stores.Com. The next project will be to create mobile versions of the shopping sites so users can hunt stores with their phones.

News Alert Banana Losing Appeal After Brown Victory

Last night on the Drudge Retort, I honored the request of a few Republican members of the site to bring back News Alert Banana when the Massachusetts Senate race was called for Scott Brown. The Banana is the Retort's version of Matt Drudge's siren.

News Alert Banana

The banana has never celebrated a dramatic Republican upset victory with so much glee before. I don't know how he lives with himself.

There are a lot of reasons being batted around today for how the Democrats managed to lose a Senate election in Massachusetts. My favorite is the idea that voters in a liberal state that has universal health care voted for Brown, a candidate who supports the state's health care plan, in order to send a strong message in rejection of health care reform. That's like buying a Red Sox jersey to indicate how much you love the Yankees.

One reason for the ass-kicking ought to be getting more attention today, in my opinion: The Democrat-led Massachusetts legislature changed the Senate succession rules twice in the last six years to benefit their own party.

In 2004, when Sen. John Kerry of Massachusetts was the party's presidential nominee, the legislature faced the prospect that if Kerry won, the state's Republican governor Mitt Romney would be able to appoint his successor. They changed the rules at the urging of Sen. Ted Kennedy so that a special election would fill the seat instead.

Last year, when it appeared that Kennedy would die and leave the Senate Democrats one short of a filibuster-proof majority of 60 members, the rules were changed again with his involvement. The state's Democratic governor Deval Patrick would be able to appoint an interim successor to serve until the special election.

So when a little-known Republican state senator faced off Tuesday against the Democratic attorney general, who was supported by the Kennedys and the state's political establishment, this was the first time that voters had a chance to have their say in the choice of a replacement senator.

Message received.

The American public is sick and tired of insiders rigging the system for their own benefit, whether the system is politics, banking or Wall Street. Kennedy's gamesmanship with Senate seats helped elect the first Republican senator in Massachusetts in 38 years.

Human Target's Opening Credits Hit the Mark

The new Fox series Human Target has amazing opening credits:

I caught the pilot during a special preview Sunday night sandwiched between episodes of 24. Regular airings begin Wednesday. The show, based on a DC comic book from the '70s, was a light escapist romp. Mark Valley, who was great a few years ago in a similar role as Keen Eddie, plays an out-of-his-mind bodyguard for hire who manages to get shot, stabbed, blown up and trapped on a runaway bullet train in a single episode. The great Jackie Earle Haley, Rorschach from Watchmen and Kelly Leak from Bad News Bears, plays his unscrupulous henchman Guerrero.

And Now You Want to Be My Friend on Facebook?

I was poking around Google Reader when I found a recommended six-month-old blog post by Google engineer Mark Chu-Carroll on why he will not be attending his high school reunion:

... it's twenty five years since I got out of that miserable ------- hell-hole. And people from my high school class are suddenly getting in touch, sending me email, trying to friend me on Facebook, and trying to convince me to bring my family to the reunion. (It's a picnic reunion, full family invited.) Even some of the people who used to beat the crap out of me on a regular basis are getting in touch as if we're old friends. ...

Stay the ---- away from me. I don't want to hear about your lives. I don't want to know how you've changed since high school. I don't want to hear about your jobs, your spouses, your children. I've got a good life now, and I cannot imagine a reason in the world why I would pollute that world with contact with any of you.

Chu-Carroll describes in his post and subsequent comments how his fingers were broken by a bully and a swastika was once burned into his lawn while he attended a suburban New Jersey high school in the '80s. He received hundreds of responses to his post, including one that recommended an appropriate song by the Australian singer Kate Miller-Heidke:

You can hear a better audio version on her iLike page. I think my favorite genre of music is antisocial piano rock. Last fall, Miller-Heidke toured the U.S. as the opening act for the master of the form, Ben Folds.

Why Leslie Harpold's Sites Disappeared

Leslie had a special kind of magic. But today there's no trace of her sites.

As long as those sites were up, her brand of humanity was alive, pure, unedited and quenching. The availability of her writing made her slightly less absent. Sad isn't really an appropriate construct for missing Leslie. And sappy sentimentality wouldn't please her at all.

But that writing should remain on the Internet. Those sites should never come down. They belong here like Leslie belonged here. Immortal.

-- a comment by Liria Mersini

A little over three years ago, the web designer and online essayist Leslie Harpold died at age 40. Leslie was a friend of mine, part of a circle of early web creators who discovered the medium as it was blossoming in the mid-'90s. We hung out together on a private mailing list for a decade watching the web (and ourselves) grow up. Leslie left behind a vast body of online work in the form of essays, web sites, weblog entries and photos.

Since that time, almost all of it has disappeared.

Leslie HarpoldLeslie's family allowed her domains smug.com, harpold.com and others to expire and politely turned down all requests to mirror her sites. Several of her friends, including me, had offered after her death to pay the costs required to keep them online.

The recent death of Brad Graham, another early web publisher, has renewed interest in the fate of Leslie's work. I sent an email yesterday to Leslie's niece, asking if it would be possible for some of her friends to reprint her work as a book and web site. Today I heard back. They will not allow anything to be republished. Because I've been told that some of her writings might be a sensitive issue for her family, I replied to her niece that if this is indeed the case, those particular works could be excluded from reprint.

This did not go over well.

I was told that it's none of my business why her family doesn't want her work republished, which is absolutely true, and that her legacy "is not dependent on websites or books; her legacy is with every person who knew her and loved her." This is only partially true. Leslie was an early pioneer in the creation of autobiographical content and experimental web design. She left behind thousands of web pages, many of which are as memorable as Possible Scenarios for Heaven from 2003.

Leslie's family appears to have decided to let her entire body of work disappear and be forgotten completely. The only things that are left online are articles she wrote for other sites, such as The Morning News.

This raises an important question for those of us who create work on the web that we publish ourselves. When heirs decide to bury a web creator's body of work by shuttering sites and rejecting all republication requests, can anything be done to save the material?

If the heirs of Charles Dickens had decided that his novels were not his legacy, they could have spurned all publishers and let the books fall out of print, but the existing copies would not have vanished entirely. There still would be physical copies of the books to read and some would've survived long enough to fall into the public domain.

For works created on the web, however, the only thing keeping them around is an active publisher or a copyright license that permits others to reprint the material. A copyright holder who wanted a web site to disappear completely could take it offline, demand its removal from all archives and never allow republication. Leslie's work will not begin passing into the public domain until 2065.

Perhaps this is the way it should be. No one has found an email or web page where Leslie stipulated her desires for her work in the event of her death, leaving the decision to her heirs.

But everything I learned about Leslie over the years tells me that she'd want this part of her to survive.

Danish Cartoonist Left Child Outside Panic Room

Last week, the Danish cartoonist Kurt Westergaard was confronted at home by an axe-wielding intruder enraged by his depiction of Muhammad. Initial media reports indicated that the 74-year-old and his five-year-old granddaughter Stephanie fled to the safety of a "panic room" in his home and alerted police, who arrived within minutes and shot the intruder.

Yesterday, news reports revealed that Westergaard fled to the panic room without his granddaughter:

At the time, Westergaard was looking after his five-year-old granddaughter, Stephanie. He was confronted with a terrible choice: risk being killed in front of his granddaughter, or trust that the PET, Denmark's security and intelligence service, knew what they were talking about when they had told him terrorists usually don't harm family members but stick to their target.

Westergaard chose to escape into his bathroom, which had been specially fortified as a "panic room", while Stephanie was left sitting in the living room. From the bathroom he alerted the police as his assailant reportedly battered the reinforced door with the axe, shouting, "We will get our revenge!"

"Those minutes were horrible," Westergaard recalled yesterday. "But I think I have got through this fairly well -- and so, it seems, did my grandchild.

Westergaard has given several different explanations for his decision to hide without the child. He told the Copenhagen Post that he was trying to draw the intruder away from the child and said to another paper that he didn't have time to get her.

Regardless of his rationale, I can't think of a any reason I'd lock myself in a secure room while leaving a young child outside with an intruder. She could've been kidnapped, attacked or threatened as a means to get him out.

We're having a lively debate about this on the Drudge Retort, where the only thing we can agree on is this: If your dad requires constant security because of death threats, he's a poor choice for babysitter.

My Life as a Religious Parable

Rick Brown, a preacher for ChristBridge Fellowship in Tomball, Texas, used me as the subject of a sermon printed in the local newspaper this week:

When Pope John Paul died Rogers Cadenhead quickly registered www.BenedictXVI.com thinking this might be the name chosen by the new pope. When Cardinal Ratzinger was elected Pope he did choose the name Pope Benedict XVI, causing many to question what the Vatican would do to get the rights to that domain name.

Cadenhead didn't ask the Vatican for money. Instead, in a humorous manner on his blog he suggested a few things he would trade for:

1. Three days, two nights at the Vatican hotel.

2. One of those hats (referring to the bishop's hat).

3. Complete absolution, no questions asked, for the third week of March 1987.

Wonder what Rogers did the third week of March in 1987? Me too. Most of us have at least a week we'd like total forgiveness for.

Since my 15 minutes of fame as the popesquatter in 2005, I've become a religious parable. A couple times a year I'm mentioned in sermons. I've turned up in churches, a syndicated radio broadcast and the book Facing Your Giants: A David and Goliath Story for Everyday People. In English, German and Spanish.

Rogers Cadenhead on Today ShowA few years ago, my sister-in-law Trish and her family were looking for a new church to join near Purdue in Indiana, so they went to a house of worship they'd never been to before. As they listened to the sermon, the pastor mentioned my name.

Like the Tomball preacher, the pastor told the story of benedictxvi.com and my request for absolution, inviting the congregation to ponder what I did that week which required papal indulgence.

Afterwards, Trish met the pastor and she sheepishly told him exactly what I'd done:

Her younger sister.