PHP Developers: Pre-made state pulldown lists for HTML forms

I like to reuse code as much as possible and if there’s underyling data in something I’ll use again, I’ll put that data in some extractable form in an EAV (Entity, Attribute, Value) database table so I don’t need to maintain the data anywhere except one place.

So in the example below where the variable $jsonText is being assigned, I actually have a method called getFormChoices(‘states’) that pulls this $jsonText automatically, from anywhere.  In Symfony, the getFormChoices() is a method I use in a base repository class that all other repositories extend and therefore inherit automatically.  This is efficient since certain higher-level global functions are all accessible and maintained in a single “Base Repository” class.

Please feel free to use parts or all of this script below in your own PHP projects.  Some old school developers might remember 1999 hard-coding every state as an HTML SELECT option — yet, there are still web developers out there that spend time remaking these over and over.  Your company pays for this inefficiency in a big way — projects don’t get completed as fast as they could or should, for one.  Do you suspect your web department is complacent and inefficient?  Ask them what they are doing to be efficient and reduce code maintenance later.  If your web developers don’t have a clear answer, chances are they haven’t adapted in 5 years and your systems and code will reflect this apathy.

Aaron Belchamber has written 243 articles

Leave a Reply

Your email address will not be published. Required fields are marked *