Why use a version control system like Subversion?

Version control, revision control or source control are all umbrella terms for systems that allow you to manage your code and assets.

The two popular systems in the web dev community are CVS (Concurrent Versioning System) and SVN (Subversion).

In the last 6 years I’ve had a great deal of exposure to Subversion. It has become the backbone to the larger projects I manage/develop.

A prime example of this is MailBigFile.com. The site has paying subscribers in over 100 countries around the world, and is deployed across multiple datacenters. It receives over a quarter of a million visits per month, with notable clients such as BBC, Carlsberg, Pepsi, Nikon and AEG Live. It’s a pretty big deal.

With mulitple developers working on the project at any one time, managing the code is vital to a smooth development process.

Subversion has the concept of a trunk and branches… like a tree! The live version of the site is defined as the trunk. You can create branches which are essentially a copy of the trunk.

It is best practise to carry out your development on a dev branch, although you can call them anything.

More to come…

Posted in Project Management | Leave a comment

List MySQL users with access to a certain database

It can be hard to keep track of all the users you have setup in MySQL.

In some cases, a user will have access to multiple databases, with different levels of permission.

As part of a security review, you may wish to list each user and their respective IP address, who’ve been granted access to a particular database.

In the following example, the database is called ecommerce.

SELECT `user`, `host`
FROM `mysql`.`db`
WHERE `db` = 'ecommerce'

If your database name contains underscores, they will need to be escaped with a backslash.
Continue reading

Posted in MySQL | Comments Off

Calculate how many hours between 2 dates using MySQL

Using the correct field types in your database allows you to take advantage of the built in date and time functions in MySQL.

The MySQL manual is always a little sparse on real world usage, so here is an example!

HOUR( TIMEDIFF( `date_created`, NOW() ) ) >= 8

TIMEDIFF takes two arguments, both date and/or time expressions. It returns the difference between them in a time format (00:00:00).

HOUR takes one argument, simply a time expression and returns the hour component of it.

In the example above `date_created` is a field with type “datetime”.

NOW() returns the current date/time.

Here, we are comparing the two to see if there is 8 or more hours between them.

You would use this in the WHERE clause of your query.

(As a side note, you will notice backticks around `date_created`. It’s good practice to use these on database, table and field names to prevent conflicts with built in functions/syntax. e.g. A table called “order” could cause confusion.)
Continue reading

Posted in MySQL | Comments Off

Highlight a keyword using jQuery

Normally you would perform this type of operation server-side with PHP. I’ve put together a javascript solution for when it’s not possible to do so.

// Iterate over each paragraph element.
$("p").each(function(){

	// Extract child elements/textnodes.
	var code = $(this).html();

	// Do find/replace. "g" = global. "i" = case-insensitive.
	code = code.replace(/(keyword)/gi, "<span>$1<\/span>");

	// Re-insert modified code to DOM.
	$(this).html(code);

	// Add appropriate class to span.
	$(this).find("span").addClass("highlight");
});

In this case, I have set the context as all paragraph tags. You can change the selector on Line 2 to suit your needs.

Line 8 defines the phrase to search for, “keyword”. The $1 is a backreference to the matched pattern. i.e. Put the keyword, in its original case, inside the span tag.

Finally, add some CSS magic. This example sets the text to white and the background to red.

p span.highlight {
	background: #c00;
	color: #fff;
}

(a collection tin will be passed around shortly…)
Continue reading

Posted in jQuery | Comments Off

When 1st in Google is bad for business

Increasingly, smaller businesses are being seduced by cold calling SEO companies promising to make their website “number 1 in Google”.

The benefits: This position could be great for increasing traffic, leads/sales and general brand exposure. However, are their methods to gain you this lucrative spot actually harmful?

The method: To achieve their goal, the SEO company will often suggest a series of changes to page titles, meta descriptions, h1 tags and general copy to promote a particular keyword phrase. All standard stuff.

The problem: Their chosen phrase will not be related to your core business and is selected purely on the basis it has little competition from other sites.

This makes their job much easier. Goal met… cash paid… goodbye. Meanwhile, you’re left with a site optimised for a phrase your target audience will never search for.

At least you’re first in Google! That’s what you wanted right?
Continue reading

Posted in SEO | Comments Off

Find and Replace using Grep in BBEdit or Coda

You have been sent a long list in an email or Word doc. Being a good web citizen, you want to markup it up semantically as an ordered/unordered list.

How can you transform this…

Lewis Hamilton
Jenson Button
Sebastian Vettel
Mark Webber
Fernando Alonso
Robert Kubica

In to this?

<li>Lewis Hamilton</li>
<li>Jenson Button</li>
<li>Sebastian Vettel</li>
<li>Mark Webber</li>
<li>Fernando Alonso</li>
<li>Robert Kubica</li>

Solution

Copy your original list into BBEdit. Open the Find dialog (Command + F). Enable the Grep option.

In the Find box type:

(.+)

In the Replace box type:

<li>\1</li>

Explanation

The “.” means any character, excluding new lines. The “+” means one or more occurances of the preceeding character. Wrapping it in “()” enables us to reference the matched pattern using “\1″.

Using Coda? The same can be achieved, however you need to mess around with the RegEx settings. The pattern will be the same though.
Continue reading

Posted in HTML | Comments Off

FTP on a Mac using the Terminal

Here’s a quick way to test a set of FTP login credentials, without having to fire up your regular FTP client. (I use Cyberduck Coda).

Open the Terminal app. This can be found in the following location:
Applications -> Utilities -> Terminal.app

Initiate FTP session:

ftp

Connect to server:

open your.server.com

It will prompt you for the username, followed by password.

If the credentials are correct, you will see the following message:

Login successful

Tip: You can use regular unix commands such as cd and ls to navigate and list directories/files. For the full run down of available commands type man ftp.
Continue reading

Posted in Apple Mac | Comments Off

Is today a weekday? Check with PHP

Simple PHP function to determine if today is a weekday. Could be useful if you have alternative advertising/content for weekends.

function isWeekday() {

	$date = date("w");

	if ( $date != "0" && $date != "6" ) {
		return true;
	}
	else {
		return false;
	}

}

Continue reading

Posted in PHP | Comments Off

Simulate target blank with jQuery

The target attribute is not valid using the xhtml strict doctype, so I wrote this javascript workaround.

// give your anchor tag a class of "target-blank".
$(".target-blank").click(function() {

	window.open( $(this).attr("href") );

	return false;
});

Continue reading

Posted in jQuery | Comments Off

Limit the number of checkboxes a user can select with jQuery

The following code limits the number of checkboxes a user can select simultaneously.

// when user updates a checkbox.
$("input:checkbox").change(function(){

	// total allowed to be checked.
	var max_allowed = 2;

	// count how many checked.
	var checked = $("input:checked").size();

	// perform test.
	if ( checked > max_allowed ) {

		// is more than the max so uncheck.
		$(this).attr("checked", "");

		// display error message.
		alert("Please select a maximum of " + max_allowed + " options.");

	}

});

Continue reading

Posted in jQuery | Comments Off