Matt Waterworth

Biography

Matt Waterworth is an experienced web developer and second-rate Jack Osbourne impersonator. He specialises in devising and implementing PHP-based web applications. He is an advocate of practical pattern-based object-orientated design.

He is a part-time internet marketer, blogger and domainer who catalogues his entrepreneurial misadventures on TakeMoreRisks.com.

Contact Matt

Fake project idea

Pet Penguin.com

This site will give penguin owners advice on how to keep a penguin as a pet.

I'll review and promote certain fridge freezers which would be ideal for locating a penguin on a long-term basis.

I'll also seek out penguin-friendly pubs in the Belfast area and give them a recommendation on the website.

Transferring and Creating My Blog..

OK, I'm not comfortable with the idea of having to copy and paste HTML into each of the weekly folders. I want to experiment with the information design of the blog as well as the CSS. So, I'm gonna move this blog over to mattwaterworth.com. I registered it in second year, let it expire and it was subsequently captured by one of those domain parking spammer bastards. Thankfully they let it expire, probably realising it was as much use to them as a condom made out of rice paper.

My inital plan was to use Wordpress for the blog but I think it'll be more of a challenge and ultimately more useful to try to build my own blog with CakePHP. I want the blog to be as simple as possible to start with. In fact it'll only have 2 controllers, one for each week and one for each post. I'll eventually need to add in a users controller to develop an administration panel, but this isn't really important now.

The first step involves creating all of the meta redirects for each week. Before doing this I'll need to decide where all of my week entries will appear on the site. I was thinking of something like...

http://www.mattwaterworth.com/des511j1/weeks/view/3

The CakePHP install will go inside the actual directory des511ji. Weeks and view will be sent to the front controller (index.php) as parameters using mod_rewrite (which CakePHP uses to route all requests to index.php). In this case 'weeks' will be the controller, 'view' an action within that controller and 3 the week parameter which is passed to the view method.

Here's the code I used to automatically generate all of the meta redirects.

Now that the redirects are in place it's time to think about my requirements for the blog. I know I should have done this BEFORE creating the redirects, but it's easy enough to change them with the script if the URL's happen to change.

Requirements

  • When the end-user visits 'http://www.mattwaterworth.com/des511j1/weeks/view/3' they must be shown a list of the posts that have been created within that week. There's two ways to do this. The easiest method involves placing a week attribute inside the posts table, which could be entered manually by me into a web form when I'm updating my blog. The second way, would involve detecting what week it is by calling gmtmktime() and running a MYSQL query to check which posts fall within that date range. This would be a bit harder to implement,
  • Each post's title must link to a seperate page where it's again displayed. This will make my blog more accessible. I'll need to include a 'slug' attribute to store the key part of the permalink in.
  • Without an administration panel I will have to use CakePHP's scaffolding facility which automatically generates a rudimentary admin facility by reading in the structure of the database table and its relationships. Note : this is seriously insecure. Anyone can access the admin panel without a username/password. Scaffolding is turned on in CakePHP by declaring var $scaffold; inside of the necessary controller. I'll need to ensure I delete this variable once I'm finished to disable this feature.
  • Since I want to occasionally display source code in my blog, I'll need to use a code highlighting tool that'll do all of the hard work for me. According to this article there are two options, the highlight_string() native PHP function or PEAR's Text Highlighter package. Either way, I will have to store the code samples in a seperate database table, which I can then fetch through a CakePHP component.
  • Finally, the week bar at the top of the blog must be controlled dynamically.

Listing source code

This is going to be a lot more difficult than I'd initally anticipated. Whenever I try to post PHP source code into my blog it's executed automatically and therefore doesn't display right. I'll have to include another controller in the system called code, which will then help to store, retrieve and then format the code using either the highlight_string or the PEAR Text_Highlighter package.

This is the 'codes' table in my database and it's reasonably self explanatory. The 'type' attribute is there in case I use Text_Highlighter, which can apply source formatting to a range of development languages.

My biggest problem was finding a way to display the source code in the appropriate place. I initally planned to list the code at the very bottom of the post, linking to it through an anchor, but quickly realised this would be inconvienent to the reader. I instead chose to print out the listings inside a hidden div, placing another DIV where the source code should be listed. I then used Javascript to copy the contents of innerHTML of the hidden div into the correct div.

For this to work I had to devise a strict naming convention which could then be used to match the hidden div up with the source code div. Let's say that the code's slug is 'refresh'..

The hidden div would be called

code_refresh_test

The display div would be called

code_refresh

The code starts by searching for all DIV tags within the DOM. Then, each id is read in and split by the underscore delimiter. Then it tests to see if the first element of the resulting array is equal to "code", if this is true the DIV's id is stored in a results array with the slug acting as the array key. I then created another array inside the array to store the id's of the test and display divs. If both of these elements are present then the DIV's contents are substituted and the code appears in the correct place.

In the end I opted for highlight_string() over the Text_Highlighter package, which I couldn't get working as my webhost doesn't support PEAR. However after checking the source code that highlight_string produces I feel like I'm back to square one. Have a look for yourself...

Yuck! I don't have enough time to spend toying about with source formatting forever. I'll have another look at this next week so for the meantime I'll be displaying source code with screenshots.

Dynamic Week Bar

Listed below is the source code for the Weekbar helper that I created for my blog. In CakePHP, helpers are small chunks of code that help with marking up presentation logic in your layout.

The weekbar works by calculating the current week and then displaying it along with the preceding weeks as links. This means that on October 22nd at 12:00, Week 5 will automatically become available for selection. I think I'll refine this in the coming weeks to only display weeks that have posts in them.

How does the system know if I've made a post this week or not? Well it's quite simple, I placed the following SQL statement in the week controller.

SELECT * FROM `posts` WHERE UNIX_TIMESTAMP(created) BETWEEN ($this->weekOneStart + (($this->selectedWeek - 1) * 604800) + (6 * 3600)) AND ($this->weekOneStart + ($this->selectedWeek * 604800 - 1) + (6 * 3600))

If no rows are returned then there are no posts for that week.

Now if you look up to the last line of the WeekbarHelper source code you'll notice that I've made the following call.

$this->Html->guiListTree($weeks, array('id' => 'weeks')).

This command automatically generates an unordered list by taking the contents of the weeks array enclosing each element in

  • tags. This helps to keep your business and presentation logic seperate.

     

  • How To Attract A Userbase Continued

    Stumbleupon.com

    Stumbleupon.com is a social bookmarking community with a twist. Its integrated browser toolbar allows you to discover new sites that fit your interests by clicking on the "Stumble!" button.

    For a few of my new websites Stumbleupon the majority my monthly visitors. This screenshot was taken from the Awstats profile of Take More Risks.com in September 2007.

    In this instance Stumbleupon accounts for over 90% of the traffic.....

    The cool thing is that you can stumble your own websites providing each webpage has a unique URL. Rather than stumbling them manually I could write a script that automatically submits the appropriate webpage each time a new religion is created on the website, although this may violate Stumbleupon's TOS. It would be better to manually submit some of the more popular religions or unusual religions to the site.

    Now that my blog posts each have seperate URL's I'll try submitting this entry to Stumbleupon just out of interest.

    Submitting a site is relatively simple. To start with you must have the stumbleupon toolbar installed and configured with your login credentials. Next, to submit a site click on the 'I like it' button on the toolbar.

    This new window will then pop up asking for more information on your submission.

    And there we have it!

    I've found that stumbles will gain more exposure if you replicate about 5 of the topics inside the tag field as these tend to cover some popular issues. Also try to minimise the number of non-alphanumeric characters within your titles as these are automatically converted to HTML entities making your title and main link on the site look like a mess.

    So what now? Well once you've submitted the page there isn't much left for you to do, unless you want to enter the dark side. Stumbleupon is more likely to display your website if other people click on the "I like it" button. Now let's say that the page you submitted didn't contain anything worthwhile. Obviously no-one in their right mind would upvote it.

    Unless of course you paid someone to do it for you and there are plenty of companies offering this service. In fact I was just browsing through Digitalpoint when I came across this topic.

    Whilst using this service may boost your traffic in the short-term, it leaves your site liable to exclusion. For this reason it's best to provide worthwhile content that users will want to upvote naturally.

    Del.icio.us and PEAR

    I've just discovered that there is a PEAR library for interacting with the Del.icio.us API. This would make it pretty easy to automatically submit religions/beliefs to my profile.

    SEO, SEO, SEO

    This week I've been reading bits and pieces of Professional Search Engine Optimization with PHP.

    Points of discussion

    • Duplicate content will be a problem especially since each belief will be contained on a seperate page. I'll need to make it really easy for users to create quality content from the very start otherwise most of the site will languish in Google's Supplemental Index.
    • Will I use URL's like /belief/1 or belief/there-is-no-god i.e. id vs slug? The slug format will be better for search engines but will require more processing (I'll need to convert each belief title into the appropriate format and ensure there's no duplication).