Source code
| Hitting your website with a pipeSaturday, September 6, 2008, 12:44 AM
|
![]() Mail article to a friend |
Anyone who visits my blog much knows I'm not the world's biggest fan of using frameworks or even someone else's code. Yeah, I've tinkered with WordPress a bit, that's mostly to drag out what really makes it effective from an SEO standpoint.
I am currently working on a project that makes use of the US Postal Service's Web API Tools. The US Postal Service requires that you jerk around with their shockingly limited testbed server before going whole hog onto their production server. In the interest of not pulling my hair out with experimenting on this very limited testbed server, I opted to Google some of the API code.
But, the code itself dumps objects nested inside arrays that don't have very useful keys. OK, find some more code, right? Wrong. A lot of the API-ready code doesn't seem to work. Hahaha. So, a crappy solution is better than no solution, right?
Proceeding with the crappy solution, I had to dig the objects out the array. This means going through the array recursively -- something PHP does piss poorly at best -- and identifying the objects and then converting them to more usable variables.
One function helps tremendously in this task: get_object_vars.
Get_object_vars is a PHP function that loads your objects into an array. It's not always the most useful functions, since obviously well coded objects should actually be simple and easy to use. A good object should work like $whatever->subwhatever and away you go, right? But, not all objects are good objects. Especially when they are nested in arrays that have little or no identifying keys and the keys change dramatically based on what they're loading.
That's where something like get_object_vars comes in. You've dumped your whole array and you run into a roadblock. It's an object. Instead of desperately trying to make the object work, which can be hard to do if it isn't your code and the code is poorly documented, you can just dump the object to an array and the go through it recursively.
Is that good form for coding? Nope. It's terrible form.
But, one thing you find out about coding for a living, especially in a meat market like web design, is that whatever gets the job done is right. Especially if you can ensure it doesn't have any security flaws.
I've seen lots of well-formed object-oriented code that just doesn't work well. Hell, look at Digg's recent problem with unscrubbed inputs!
I think this is one of the reasons I hate the religion of so-called good coding. Well-formed code can still be garbage. And poorly-formed code can still run through hackers like a tank. Now, that's a little more of a rant than the topic deserves, so I'll leave that at that.
Whatever the case and whatever your stance, get_object_vars is a handy tool.
![]() Mail article to a friend |
The similar_text function in PHP is an intriguing, if not 100% useful function. The function works like so...
![]() Mail article to a friend |
Fgetcsv is a function that ships with PHP since sometime in version 4. It allows you to parse a Comma Separated Values format spreadsheet.
But, it comes with an ugly flaw: it doesn't automatically belch out null values.
Now, if you are using CSVs to create databases in MySQL, that sucks big time. Because every iteration of an INSERT command in MySQL has to have the right number of data inserts to correspond the all the fields you request.
Yes, I am aware that the LOAD command works more efficiently from a DBA standpoint. But, for my purposes, I'm letting folks upload CSVs, and there is no viable way to allow them to use the LOAD command. So, I am stuck with CREATE and INSERT to provide the simplest and most accessible approach.
Yeah, I probably should have written the script in Perl. But, I like to keep a project as close to a basic framework as possible. Plus, I'm just not exceedingly fond of diving into Perl.
But, you'd thinks such a basic function would have been integrated with the notion in mind that CSV files are rarely consistent beyond the fact they have commas in them.
My workaround is simple. The first line of these CSV files is supposed to have the title for those fields in it. Count all the fields in the first line, and then iterate out some blank spaces into the empty fields if they yielding null values for those fields.
The project itself uses slightly under 100 fields. It is large enough that importing the spreadsheets from Excel is too cumbersome and eventually runs the server into an out of memory error.
And, with 100 fields being dynamically inserted from mismatched content over perhaps 35,000 or so entries, you're pretty much begging MySQL to get catty about something.
Now, here's the code that the main PHP website suggests using...
$row = 1;$handle = fopen("test.csv", "r");while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo " $num fields in line $row:
\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "
\n"; }}fclose($handle);
The primary flaw in this code is that it assumes each line will be consistently populated with content. Anyone who has ever handled CSV files will tell you that ain't the truth at all.![]() Mail article to a friend |
An email came in tonight that had some trouble applying the information from the post about building a system that converts latitudes and longitudes into X-Y co-ordinates for use with images generated by GD with PHP.
In the interest of promoting a little more applicability, I'm going to post the code that generates an actual PNG file from the map database.
This loads all the co-ordinates into three arrays. One for the rivers in the county. Another for the roads. A third for the county political boundaries.
Then it goes through the arrays, and draws the lines onto the image. The line colors and thicknesses are based on the type of river or stream listed from the original shapefiles.
This probably doesn't make things any clearer, but it at least gives you a look at proven production code.
And, for the critics... please refer to the post where I admit that I am very lazy about constructing anything into a usable object class. Yeah, I know the functions is significantly more recursive and could be done better with some OOP. I just don't care, because the code works.
Anyhow... I hope this gives some folks who were looking at the lat-long post something with a little more meat to chew on.
![]() Mail article to a friend |
Download getmap.zip |
echo metaphone("Garage");
Output: KRJ
The metaphone function is very likely a function you have never seen unless you have dug pretty deep into the PHP manual. Of course, there is one very obvious reason for that: it isn't the most useful command in the world.
About the only real use I've seen for it is in building a quick spell checker function. Other than that, I could imagine a cryptographic purpose for it, perhaps as a salt in a hash. I've seen some search algorithms that use it, but I'm not certain that a search that yield matches for "sun" and "son" as winners is a winner itself.
But, it is fun to dig into the obscure functions and see what is hiding there.
![]() Mail article to a friend |
One of my really, really bad weaknesses as a programmer is a tendency to not tidy up commonly-used code into object classes. Of course, one of the primary reasons for this is that object-oriented programming is not necessary for anything to function. Another reason is that I tend to be a tinkerer. I like to dip in and out of the code constantly rechecking the functionality. I rarely have a sustained bout of steady coding.
But, I thought it would be a fun bit of teaching to show you the construction of an object-oriented piece of PHP code. The code I chose is my old date conversion code. This code converts dates from either SQL (yyyy-mm-dd) or regular (mm/dd/yyyy) format to SQL, regular or full English expression (Saturday, February 2, 2008).
![]() Mail article to a friend |
Download dateconv-oop.zip |
UPDATE: You can find a full working piece of code for this project in this later post.
Right now I'm tinkering with building a map system out of the ARC Shapefile datasets available on the internet. These are the files that the Census and the USGS and such use to actually map pretty much everything in the United States.
Not surprisingly, these maps present a challenge. Foremost is shear size and processing power. For the sake of my early experiments, I'm limiting my efforts to local maps (Jefferson County, Pennsylvania) and converting those maps into three things: database entries for dynamic mapping, vector graphics for re-use and raster graphics for web use and for a long-standing project I have to rebuild Google Maps from scratch.
Assembling all this data presents another challenge. Converting the map data into images and vector files. Particularly a bitch is converting latitude and longitude, which have an origin that is to the lower right of the images I intend to generate into typical graphics which have an origin to the upper left of the images that will be generated.
![]() Mail article to a friend |
In recent weeks a number of folks have come to the blog from search engines looking for approaches to handling large files in a PHP / MySQL environment. In a previous article, I had covered how to use the PHP.ini and my.cnf files to increase upload sizes.
However, once you have accommodated those solution, sometimes the upload issue isn't a matter of file size permissions.
![]() Mail article to a friend |
© 2010 Pro Content and Design. All rights reserved.
Welcome!
Wonder where to start with your web design business?
This blog follows along with my efforts to build and grow a website design business, Pro Content and Design.
The goal of this blog is to fill in blanks that may be empty as you get your business rolling.
This blog, particularly the source code section, is not intended for beginners. If you are not comfortable with databases, Ajax, DOM objects and other advanced methods, I strongly suggest you go take a look over at W3 Schools before even reading -- let alone tinkering with -- any of the code here.
I hope this blog has some value to web designers as they attempt to get their businesses going.
Good luck, and happy reading.
Thank you,
John Crawford
Pro Content and Design

Coding
W3 Schools
IBM's Mastering Ajax Series
Graphic Design
Worth 1000
Stock.XCHNG
Urban Fonts
Website Software
Apache Web Server
SquirrelMail
PHP/Zend
Website Design Issues
Non-Standard Character Guide
Google Trends
Search Engine Optimization Analyzer
Business
Guy Kawasaki's Blog
Seth Godin's Blog
Freakonomics
Computers
NewEgg
My Main Website
Pro Content and Design
Websites I have built
PunxsyPage: local free classifieds website
My Webapps
TV Stations Transmitter Database