More Map Tweaks


Posted on 24 February 2006 by jose

At first, it seemed my vainglorious attempt to list my international (web site) hits would be a disaster: the access map page was not part of the Wordpress structure, and editing it was not as easy as it could have been. More importantly, even though I could greet those visitors on my Wordpress pages, which would get pushed to the main Hackwater page, the XML file listing all my hits geographically was not the easiest to read, as I wasn't doing any filtering (all my US hits were crowding out the relatively few international ones). My solution: create an XML file just for the countries outside of the US that were visiting me. This would temporarily let me see all the countries, and after adding a DISTINCT clause to my SQL statement, there were no repeats. That's when I noticed hits from Thailand and Malaysia: greetings to both!

Once I did that, I started thinking about how the country names were being displayed: UPPERCASE, with a country code in parenthesis. That's fine for the markers on the map itself, but for my country list, perhaps there was a way that was easier on the eyes. Seeing as my XML file is being generated by a PHP script, I decided to edit the output of the database query. For security reasons, I changed the file type on the database connection methods and classes file that I got from Ricardo Galli, and then began to edit my countries XML file. I took the country name from my database and removed the last five characters (two-letter country code, two parentheses, and a space), then converted the name to title case (first letter of every word is uppercase, every other letter is lowercase). I then realized that for the map markers, I could transform the country names just as easily and keep the country code the same by splitting the country name into a code variable and a country variable, transforming the country variable, and then concatenating the two variables into a single country name again.

Finally, I re-wrote the while loop that prints out the country names (it essentially moves through the SQL query results, which were placed in an associative array, until it reaches the end of the array). I changed the order in which the loop advances to the next value in the array. The final action in the while loop before it checks the loop condition ("are we at the end of the array?") is to advance the array pointer by one index. If I advance the array pointer immediately after retrieving the current value, instead of at the end of the loop, I can check to see if I'm at the end of the array before breaking the loop. This lets me create a comma-separated text list of the country names with " and " as the final separator, to have a pretty English sentence/list. All that work just to get a self-updating list of countries. Feel free to contact me if you're interested in seeing the code.

In other tweaking news, I changed my search function so that it can search static pages as well as posts, and changed its action method to point to something that made sense (allowing the search to actually work).

Latest poll

Which do you favor?

Choices

Twitter