Abstract Form Handling

I've already gone over some basic form handling and form best practices with my last few posts, but building with an object-orientated MVC starts to throw forms into a different light. It's very easy to abstract out forms with their repetitive logic patterns, something that I've recently done on one of my side projects. While I don't want to explain the code line-by-line, this post will go over some of the basic thought processes and steps I took to make my abstracted form handler.

Form Wrapper

The first logic I worked out was the definition of a form. To create each new form, I create a new class that is abstracting off a base pattern. Each form class defines form elements within a standard 'get' method. This way I can call on individual form elements easily or call the entire class to pull the whole form. Example...

read more »

How Many Tiers for a Clean View Layer?

An interesting possibility cropped up for my MVC a few weeks ago. I've been working with microformats on and off the last few months (you can read about one of my first adventures with them when I created a microformatted resume). I started to wonder where else my site could use this new feature. An easy place to add microformat tags are links - you can add a 'rel' attribute that describes the relationship of the link to your site. Also, if I added a layer of abstraction to my links site wide, then I could easy add behavior rules (target="_blank" and whatnot) that would affect an individual link across all of my pages and sites.

This idea would involve creating a table of all my links, internal and external, that could be mapped to meta information about the links. There would be a helper class on my site that would pull a link based on an alias, give it attributes based off of the meta data (including microformatting), and spit out a final link. No where on ...

read more »

Abstracting the Resume

Recoding all of my subdomains to follow a model-view-controller framework has been a really interesting process. I've had the opportunity to experiment with different design patterns and techniques, especially in the realm of data handling and content delivery. Abstracting out my resume is a great example of how I'm handling my data in a way that provides necessary functionality at the moment while staying flexible enough for future rewrites.

Until recently my resume was written inline with html tags, merely included into different web pages as needed. This was a bad solution - every time I wanted to change a piece of it I had to wade through html tags. I couldn't place any dynamic content in my resume or reuse it in non-html formats. A cleaner solution was needed.

My first step was to divide up my resume into pieces. I have five main categories: objective, job history, skills, achievements, and education. The objective is a single line of text while the other four categories are lists. Some of these lists are lines of text while ...

read more »

Thoughts on Data Abstraction

Something that I've been working on a lot with both my work and personal web programming is data abstraction. The opportunity to work with several different handmade php frameworks has given me different ideas on what an effective data abstraction system should have. Without formal education, though, this post will be only based on my hands-on experience with the functionality I find myself using without true design patterns.

Data abstraction is a fancy word for treating the data as a separate entity from your web application logic. Web sites usually store their data in databases, using customized queries to pull information for their code to output and manipulate information. It doesn't matter what type of database or language you're working with; data abstraction can be employed whenever there is interaction with data (this post will be approaching this from a rough PHP - MySQL angle). Complex sites often have numerous queries scattered throughout the web site, and one needs to have intimate knowledge of the database structure during the ...

read more »

Beginner Web Developer Mistakes

With my introduction into web development coming from a static html/css direction, I've had to overcome several bad coding habits over the years. Most of these started with poor assumptions about the way websites worked. It took a crash course in php programming and a few hard bumps before realizing how wrong my first websites really were. Here are a few of those assumptions and how I built on them to increase my programming skills.

Each URL relates to a unique file on a web server

If you're only used to working with static html files than this assumption is largely true. Every different URL that a user visits on a web page is in reference to a different file on the server. Without any type of scripting, the file is merely a chunck of html and can only be changed by a webmaster physically manipulating the document. My first dynamic website used php include commands to pull a fixed header and footer for all of my pages, allowing me to make global site changes from a single file. When I finally started to learn more about php variables ...

read more »

Revisiting the Database Class

Several months ago I wrote a blog post describing how to create a database class that would wrap the native php mysqli connection. You can read that post here. Since that post was written I've learned a lot about proper php classes and realized that I made several mistakes in that blog post that I'd like to revisit.

Singleton

The database class that I wrote constructs a new read/write connection whenever it is instantiated. However, there is nothing unique about this connection - it remains the same no matter how many different instances you have of this class. There should be only one instance of this class in this case. If you have several different connections (say, a different user for each of your databases or schemas) then you'd have to look at passing connection information into the class through a factory class, but this case requires a singleton.

A singleton class is fairly simple to make. By making the __construct method private and ...

read more »

Intro to SQL Part D

For the last post in this short series on SQL I wanted to explain some more in-depth usage. A technique that I wish I would have started using in my early websites is a centralized script to handle all of my database calls. PHP does have some great functions built in to connect, read, and write using SQL statements (check out mysqli), but it's easy to start copying the same eight to ten lines of code every time you want a piece of data from your tables. A better approach is to handle the queries with a centralized class.

Before anything can be read or written to a table with a SQL statement, you need to create a connection. Your website will be signing as a user with specific privileges. A good technique is to make two users, one that can read and one that can read and write. By using the former user as much as possible and taking extra care with the latter one, you will minimize the possibility of hackers uploading malicious information or dropping whole tables.

Once a connection is made you can start having fun with the database ...

read more »

MVC CSS Stylesheets

While working on redoing my website using a custom model-view-controller framework I decided that it was a good time to do some extra cleanup. After all, cleaning up the backend only changes the content portion of the site. There's much more to websites - styles, behaviors, cache control - then the content. In the interest of cleaning up my website I decided to take my MVC framework to the next level and use it to create my stylesheets and Javascript in real-time.

The primary benefit of MVC frameworks is to simplify code logic and remove duplicate code. With all user requests being transferred through the same routes and controllers, it's easy to whip out extra html pages or make site-wide changes by editing a few lines. Common SQL functions, AJAX calls, and classes can be used across different pages and sites easily without duplication.

There's a lot of duplicated code in my assets. I use Erik Meyer's reset stylesheet, a global set of style rules, jQuery, similar lightboxes, etc across my different subdomains. I could include each ...

read more »

Website Overhaul

It may be no surprise to any of my long-term visitors, but I've decided to perform a deep overhaul of my website. Based partly on my recent introduction to model-view-control frameworks and partly on research into web caching and optimization, I've spent a good chunk of time analyzing my current and future website functions in the hopes of streamlining the backend code and optimizing the frontend. There are several goals that I have in mind for this overhaul, most of which will stretch my current programming skills.

Model-View-Controller Setup

The idea behind this sort of programming is breaking everything up into separate, reusable objects. All requests to the website are directed to a primary router, which interprets the request and pulls a controller to handle the page. A controller is a set of instructions based on the page type (example: home page, blog post pages, etc) that sets variables for the final page. The controller may talk to a model, which is the 'logic' for the site (usually a database object), and pull additional ...

read more »

Distance Compare Class Part B

This post assumes advanced understanding of PHP and some object orientated knowledge.

In my last post, I outlined a basic class that compares addresses by distance. The class returns a sorted, filtered, or single best match from an array of addresses based on a single comparison address. However, this didn't fulfill my client's needs completely. I needed to extend this class to fill his needs, so I made a new class that utilized my basic class.

The first step was making a construct method that extended to gDistanceCompare's construct method. To do this, all I needed to do is put parent:: in front of the class method from gDistanceCompare.

public function __construct()
{
 parent::__construct($this->_readCSV('PL_Locator_Website_Confirmations.csv'));
}

The client's information was in a csv file, so I made a method for turning the CSV information into an array that gDistanceCompare needs.

read more »