rcaden is currently certified at Journeyer level.

Name: Rogers Cadenhead
Member since: 2002-04-04 04:26:04
Last Login: 2009-08-10 15:31:44

FOAF RDF Share This

Homepage: http://workbench.cadenhead.org/

Projects

Recent blog entries by rcaden

Syndication: RSS 2.0

Programming a Confidence Pool for the World Cup

World Cup 2014 soccer ball, Adidas Brazuca

I did some coding yesterday to run a 2014 World Cup confidence pool on SportsFilter. The way a confidence pool works is you assign points to teams based on how confident you are they will win. In this contest, you get point values from 32 down to 1 and assign them to the 32 World Cup teams. The more points you assign a team, the more you are awarded when that team wins or ties a match.

Anyone can sign up for a SportsFilter account and play. A prize will be awarded to the winner: the Adidas replica version of the official World Cup ball. The deadline for entries is shortly before the first match Thursday.

I wrote the web application in PHP, stored the database in MySQL and used the Smarty PHP template engine for the presentation. This was a project I had to complete in a single day, so I didn't think I'd go to the trouble of using Smarty. I figured I would just kludge together some ugly PHP pages that had code and HTML markup mixed together.

But Smarty makes it easier to present the HTML output of a PHP web application. I find that I write better PHP code when I use it and there's less debugging required to get things right.

The way Smarty works is you write a PHP script that does the work such as collecting user input and storing data, make some variables from that script available to Smarty and tell it to load a template file that contains HTML and some simple code to present information.

Here's the last part of the script that displays the contest entry form and validates what the user has submitted:

// make data available to templates
$smarty->assign('spofi', $spofi);
$smarty->assign('page_title', "Enter the SportsFilter World Cup 2014 Confidence Pool");
$smarty->assign('spofi', $spofi);
$smarty->assign('form_output', $form_output);
$smarty->assign('error_message', $error_message);
$smarty->assign('random_team', $random_team);
$smarty->assign('submit_widget', $submit_widget);

// display output
$smarty->display('header.tpl');
$smarty->display('confidence-pool.tpl');
$smarty->display('footer.tpl');

Those three .tpl files are templates. The header and footer contain the SportsFilter layout and the main body of the page is in confidence-pool.tpl.

Here's a portion of that template:

{$error_message}
<p><form action="/confidence-pool.php" method="post">
{$form_output}
<input type="submit" value="Preview My Entry" name="command">
{$submit_widget}
</form>

Smarty's template language can do more than display variables. It has for loops to display every element in an array and formatting functions that can do things such as control how the elements of a date are displayed.

I also used it on Wargames.Com. Here's some template code from the state directory pages:

{foreach from=$stores item=store}
<p class="directorystore">
<span class="directorystorename"><a href="{$store.link}">{$store.name}</a><br /></span>
{if $store.phone neq ''}<span class="directorystorephone">{$store.phone}<br /></span>{/if}
<span class="directorystorelocation">{$store.city}, {$store.statename} {$store.zip}<br /></span>
</p>
{/foreach}

The $stores variable is an array with link, name, phone, city, statename and zip elements.

The ultimate goal of today's project is to make it possible for a website's users to create and run their own confidence pools for any sporting event that lends itself to this type of contest. One sport it would be ideal for is the NFL, with players ranking the teams playing that weekend and competing all season.

Syndicated 2014-06-11 15:49:47 from Workbench

Ghost of Computer Author Past

You could tell a lot about an author or conference speaker by tracking the changes made to that person's short bio over the years. Here's how I described myself in 1996 for Java Unleashed, Second Edition, a frankenbook written by 24 authors in the book publishing rush after Java was launched:

Rogers Cadenhead is a web developer, computer programmer and writer who created the multi-user games Czarlords and Super Video Poker. Thousands of readers see his work in the Fort Worth Star-Telegram question-and-answer column "Ask Ed Brice." Rogers has developed Java applets for Tele-Communications Inc. and other clients, and is the coauthor of Teach Yourself SunSoft's Java Workshop in 21 Days. Rogers wrote Chapters 1, 5, 17 and 18 and contributed to Chapter 2.

It didn't take long for me to drop references to Czarlords and Super Video Poker, two online games I created for bulletin board systems. My friend Mark Terrano and I started a company to create multi-user BBS games right before the Web came along and killed the businesses of every potential customer. Our total earnings were $180.

Terrano did a little bit better on his next game venture. He was lead designer of Age of Empires II and is a founder of Hidden Path Entertainment.

Syndicated 2014-03-24 21:00:13 from Workbench

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

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.

Syndicated 2010-02-01 20:37:30 from Workbench

Book Giveaway: Teach Yourself Java in 24 Hours

My newest book, Sams Teach Yourself Java in 24 Hours, Fifth Edition, recently hit bookstores. The book is a for-absolute-beginners guide to programming Java, and this section from chapter one's Q&A section shows how much license I get from the publisher to have fun with the series:

Q. Do you only answer questions about Java?

A. Not at all. Ask me anything.

Q. Okay, why is Prince mad at the Foo Fighters?

A. Prince is unhappy that the Foo Fighters performed a cover of his song "Darling Nikki" and released it as a B-side single in Australia. He told Entertainment Weekly they should write their own tunes and wouldn't let the band release it in the United States. This became a pretty meaningless distinction as the song became a radio hit around the globe and was played regularly during their concerts.

When Prince performed at Super Bowl XLI a few years later, he covered the Foo Fighters' "Best of You," an artistic decision that surprised the Foo Fighters as much as everybody else.

"It was pretty amazing to have a guy like Prince covering one of our songs," Foo Fighters drummer Taylor Hawkins told MTV, "and actually doing it better than we did."

Although playing someone else's music is an odd way to exercise a grudge, this was a better option for the 5-foot-2 Prince than challenging the band to a fight.

Every chapter ends with one reader question that has bupkiss to do with Java. I used to be the Fort Worth Star-Telegram's Ed Brice, an answer man who fielded random questions, so old habits die hard.

Sams Teach Yourself Java in 24 Hours, Fifth EditionMy book has been fully updated for Java 6 and has new chapters on JAX-WS and game programming. I have 20 copies I'd like to give to people who want to learn Java, and there's still time for me to mail them before Christmas.

If you know someone who wants to learn Java, or you can make a convincing case for why Santa owes you this book after the year 2009 you just endured, please leave a comment here on Workbench or in a Twitter post to rcade. Make sure I have some means of contacting you, so I can get the address of the person getting the book.

I'm planning on mailing these out on Wednesday morning in the pre-Christmas scrum at the post office. I will mail the books directly to the people receiving them and can put your name and address as the sender and wrap them if necessary. No one needs to know I was involved.

Please note that I'm expecting the people who get this free book to teach themselves Java in a single contiguous 24-hour period. For too long, Sams has coddled readers who devote one hour a day to a subject and learn it at their leisure.

Syndicated 2009-12-14 18:10:10 from Workbench

Saving Bandwidth on RSS Feed Details

With the current interest in rssCloud and PubSubHubbub (PuSH), I've been thinking about all the bandwidth that's consumed by the RSS elements that describe the feed. When a client requests an RSS feed 10 times in one day, it gets the basic details of the feed over and over again. When clients request the Workbench feed, they get 1,800 characters containing optional RSS elements that I haven't changed in years, except for the PuSH element I added last month. Workbench has 1,900 feed subscribers, so if they average 10 checks a day, they're consuming 32 megabytes every day on information they know already.

James Holderness directed me to RFC3229+feed, a method to request partial RSS feeds that omit elements that a client has already seen. That's useful and has been adopted by some feed publishers and clients, but as far as I can determine, the approach still sends all of the channel elements that describe the feed itself. I wanted to float an idea here to see if it would be useful:

<rssboard:feedDetails>
  http://ekzemplo.com/feedinfo.rss
</rssboard:feedDetails>

This channel-level RSS element identifies a URL that contains the full details about the feed. The details would be expressed as an RSS feed without any item elements.

An optional ttl attribute could contain the number of days the publisher would like clients to cache the information before checking it again:

<rssboard:feedDetails ttl="30">
  http://ekzemplo.com/feedinfo.rss
</rssboard:feedDetails>

A feed publisher who wished to make use of this could move all channel elements except for title, link, description and atom:link to the detail URL. Title, link and description are required in RSS, and atom:link identifies the feed's URL so it can't be moved.

Syndicated 2009-10-13 13:01:25 from Workbench

86 older entries...

 

rcaden certified others as follows:

  • rcaden certified rcaden as Journeyer
  • rcaden certified myelin as Journeyer
  • rcaden certified markpasc as Apprentice

Others have certified rcaden as follows:

  • rcaden certified rcaden as Journeyer
  • myelin certified rcaden as Apprentice
  • markpasc certified rcaden as Apprentice
  • mcroydon certified rcaden as Journeyer
  • pencechp certified rcaden as Apprentice
  • lerdsuwa certified rcaden as Apprentice
  • rasjani certified rcaden as Journeyer
  • mterry certified rcaden as Apprentice
  • tnt certified rcaden as Journeyer
  • ta0kira certified rcaden as Journeyer

[ Certification disabled because you're not logged in. ]

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

X
Share this page