Welcome, Guest | Sign In

Submit - Cancel

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 and url manipulation, I kicked myself for the extra time and work I spent creating larger sites. There are a number of techniques that you can use to have a single script output multiple pages on a site, a concept I discussed with...

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 maintaining a single instance saved within the class, you guarantee that there will be only one instance of the class. It can be very helpful to restrict some classes in a singleton manner. For this case we'll avoid creating multiple identical mysqli connections...

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 class, customizing the methods as much as necessary to integrate it easily with your site. I prefer returning my information as arrays of objects or single objects, so I have two separate methods in my class.

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 asset as a separate file, but that setup would create an increasingly long list of http requests, which is bad for website optimization. My adventure map, for example, would need at least 5 Javascript assets: Google maps settings, jQuery,...

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 content for the page. Finally, views are included and passed variables to display a unique page per URL.

The benefits for this system are numerous - once the system is setup, that is. One database model can handle all SQL commands...

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.

private function _readCSV($url)
{
  $handle = fopen($url,'r');
  while(($data = fgetcsv($handle,1000,","))!=false)
    if($data[1]!='Street Address') $array[] = $data[1].', '.$data[2].', '.$data[3].' '.$data[4];
  fclose($handle);
  return $array;
}

To read the CSV, you need to loop through the file with...

read more »

Distance Compare Class Part A

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

One area of PHP that I haven't had much experience with yet is classes. I have a rough idea of what they do, but after working with a few inefficient classes, figured that they are more trouble then they're worth. It wasn't until one of my contract jobs specifically requested an application built up from a class that I actually built one from scratch and started understanding the true benefit of object-orientated programming.

In this scenario, the client owned about eight stores with unique addresses. They wanted a page on their website that allowed a user to input a zip code in a form to find the closest stores. There are several php classes online that can do this, but they depend on custom databases with zip code information built in and only calculate straight-line distances. As zip codes change over time, these classes became outdated quickly, requiring regular maintenance.

For this project, I decided to connect to Google Maps to find out the driving distance between the user's zip code and the available stores. The first step, though, is creating the class with a construct method. I wanted this class to work regardless of the method of address retrieval...

read more »