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 »

Basic Tag Cloud

This post assumes basic understanding of PHP, HTML, and CSS.

Creating my blog by hand gave me the opportunity to work with many features that come standard with online blogging software. I developed tag clouds, post filtering, and the search functions based on the visual functionality that other blogs appeared to have. With this post, I'm going to explain how to make a tag cloud using PHP and CSS that can be implemented on any type of article- or post-based environment.

Most blogs that use tags allow multiple tags per post and tags being placed on multiple posts. The tag not only helps explain what the post is about, but also gives basic navigation based on tag for users interested in a specific blog category. A tag cloud provides the navigation with an additional feature: tags that are used more often in the blog are increased in font size, giving them more weight compared to less-used tags.

The first thing that you'll need is a PHP array of all your blog tags. The creation of this array is wholly dependent on your blog setup - I have all my tags in a separate table, so I just did a SQL select command and pulled out an array. If your blog is basic, you may have a manual array or have to loop through a larger table setup. The important thing is to have a...

read more »

Dynamic Template Methods

Ever since I started working on websites during the summer of 2006 I made sure to keep dynamic and static information separate on my projects. Certain things in the website's content should remain the same, or similar, on separate pages: header, footer, navigation, etc. (I'll refer to these similar pieces as 'static' and the content that is unique to each page as 'dynamic'). However, when you work on a website with multiple pages, it becomes difficult to update the static information the same on all pages. This is where a templating system comes in handy.

A templating system allows the developer to separate the content to ease updating. When I first started with websites, I used Dreamweaver templates for this, which is a classic example of a templating system. You create a document in Dreamweaver with the header, footer, and any other information you want to remain the same across all the pages on your site and save it as a template. You can create new pages based on the template and Dreamweaver will 'lock' the template structure and only allow you to edit the dynamic, or unique, area of the page. If you want to change the static information, you can open the template separately, edit it, and save it. All of the pages created with the template will automatically update...

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 »

Modular Website Setup

After making several iterations of my personal site, I've finally found a php backend solution that has made me happy. While I have no doubt that I'll find a new system or methodology within the year that will warrent a complete redesign, my current setup is surprisingly simple and fun to play with.

As php is my primary coding language, I make it a personal goal to never use third party plug-ins. I often utilize jQuery or posted Javascript plug-ins in my work, mostly because working with Javascript and forcing it to work in every available browser can be excrutiating. I have never used Ignitor or Wordpress for a project and do not see the need for it in my near future. So, when I create new sites and applications, I might find myself copying some of my old code for a particular function, but often try to make it cleaner or more efficient during the development process.

When I tackled my personal site this time around, I tried to sketch out the data flow before coding. Not only did I want a central file to handle all web page requests (mod rewrite style) and create the wire frame of my div layout, but I also took all of the unique content, including meta data and navbar, and put it in a separate file outside of my public_html folder. All of this is generated and...

read more »

Cross Site Status Updater

This post assumes semi-advanced understanding of PHP scripting as well as basic understanding of Twitter and Facebook.

When I started working on launching my personal website, an interesting problem came up. I wanted to have a feature that showed a current status on my site, similar to a Facebook status. Also, since I also have a Twitter and Facebook account, it only made sense to have a single place to update my Twitter, Facebook, and personal site status.

After doing a bit of research, I decided to use Twitter as the central updater. Twitter has many nice applications built into it, including the ability to update from a desktop or phone. Once my Twitter status was updated, the change would get carried down to my Facebook account and personal site through the Twitter feed.

The first step was with Facebook, and ended up being quite easy. There is already an application built for this that works quite well. Once you agree to use the application, all you need to do is sign into your Twitter account through Facebook and change a few settings, and every tweet you make is imported to your Facebook status.

The next step was updating my own site. I chose to use a PHP script for this, utilizing...

read more »