Archive for the 'Programming' Category

« Previous Entries

Serial commas and PHP

I love serial commas (putting a comma before “and” in a list. I.e. one, two, and three). People call them old-fashioned and clunky, but they really help with clarity (see this article for an example).

The thing I don’t like about serial commas is trying to automate their formatting. I can’t find examples anywhere, so here’s my PHP code to do it. The way I use it is a while() loop after querying a database, but it will work with any loop (i.e. a foreach’d array). $num_authors is the total number of records. $count_authors is set to 1 before the loop.

The code

if ($num_authors == 1)
{
	echo $author;
}
else
{
	if ($num_authors > $count_authors)
	{
		if (($count_authors + 1) == $num_authors)
		{
			if ($num_authors == 2)
			{
				echo "$author and ";
			}
			else
			{
				echo "$author, and ";
			}
		}
		else
		{
			echo "$author, ";
		}
	}
	elseif ($num_authors == $count_authors)
	{
		echo $author;
	}
      	else
	{
		// error message
	}
	$count_authors++;
}

Friendly Dates with MySQL and PHP

$when = date('F j, Y', strtotime($datetime));

The above code, where the ‘$datetime’ variable is a MySQL datetime stamp, will output a humanized date use PHP’s date(); function.

Display age based on birthday PHP script

I was looking for a way to keep the About section up to date and came across this one from Dzone that works really well:

function birthday ($birthday)
{
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}

You can activate it with:

echo birthday("YYYY-MM-DD");

Random Post Widget

I added a “Random Post” widget to the sidebar today. I spent a couple minutes at the WordPress Widgets site, but I couldn’t find anything I liked so I made my own in PHP that is stupidly simple.

I defined the randomPost() function in wp-includes/functions.php because I knew it was being included already. You can put it anywhere, or just dump the code directly onto your page.

Random Post Code

function randomPost() {
<div class="side-sec pages">
<h3>Random Post</h3>
<?php
// Get ID, Title, Post, and Date from WordPress DB
$query = "SELECT ID, post_title, post_content, post_date FROM wp_posts WHERE post_type='post' ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
// Use $row for grabbed content
$row = mysql_fetch_array($result);
// Take only the first 150 characters and get rid of HTML from post
$content = substr(strip_tags($row[2]), 0, 150);
// Display using relative dates
echo '<p><a href="?p='.$row[0].'">'.$row[1].'</a> '.lastfm_relative($row[3]).'</p>
<p>'.$content.' [...]</p>';
?>
</div>
}

Since I already use the Last.fm for WordPress plugin, I used their relative time function: lastfm_relative(). If you don’t have/want that plugin, here is the code for the function.

Relative Time Function

function lastfm_relative($time) {
$time_orig = strtotime($time);
$diff = $just = time()-$time_orig;
$months = floor($diff/2592000);
$diff -= $months*2419200;
$weeks = floor($diff/604800);
$diff -= $weeks*604800;
$days = floor($diff/86400);
$diff -= $days*86400;
$hours = floor($diff/3600);
$diff -= $hours*3600;
$minutes = floor($diff/60);
$diff -= $minutes*60;
$seconds = $diff;
// Variables defined
if ($just<=0) {
return 'Just Now!';
} else {
if ($months>0) {
// over a month old, just show date (yyyy/mm/dd format)
return 'on '.date('Y/m/d', $time_orig);
} else {
if ($weeks>0) {
// weeks and days
$relative_date .= ($relative_date?', ':'').$weeks.' '.__('week').($weeks>1?'s':'');
$relative_date .= $days>0?($relative_date?', ':'').$days.' '.__('day').($days>1?'s':''):'';
} elseif ($days>0) {
// days and hours
$relative_date .= ($relative_date?', ':'').$days.' '.__('day').($days>1?'s':'');
$relative_date .= $hours>0?($relative_date?', ':'').$hours.' '.__('hour').($hours>1?'s':''):'';
} elseif ($hours>0) {
// hours and minutes
$relative_date .= ($relative_date?', ':'').$hours.' '.__('hour').($hours>1?'s':'');
$relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' '.__('minute').($minutes>1?'s':''):'';
} elseif ($minutes>0) {
// minutes only
$relative_date .= ($relative_date?', ':'').$minutes.' '.__('minute').($minutes>1?'s':'');
} else {
// seconds only
$relative_date .= ($relative_date?', ':'').$seconds.' '.__('second').($seconds>1?'s':'');
}
}
}
// show relative date and add proper verbiage
return $relative_date.' ago';
}

And that’s it. The relative time function is by far the most complex part of this whole operation. There’s no validation or verification — it assumes that there are posts ready to be grabbed, that your database is accessible, etc. You can see it working on the right side of this page.

Dictionary Design

Drafting Process

It’s rare for me to do any kind of digital work without a paper/pencil or whiteboard next to me for jot notes, drafts, and calculations. I’ve been redeveloping the entry system for a dictionary that’s going to be used by hundreds of students around the world for the next seven years, and eventually as the public digital front end. It’s very important to me that I get this right.

Draft 1 was just a quicky to make sure I could get everything I wanted, so there was no drawing. Now that is IS working, it’s time to think about usability and aesthetics — the two most important aspects, to me, of any website — and that means it’s time to grab a marker.

Draft 2

Wire frame whiteboard drawing of a website

Draft 2 was an attempt to streamline the dictionary’s entry system, but failed when it came down to semantics. The back-end was ridiculously elaborate and difficult. A system does not work when the person who created it can’t figure out how to use it properly.

Draft 3

Wire frame whiteboard drawing of a website

Draft 3 looks a little cramped from the wire frame, but it’s very intuitive and slick — this is it for now. Also, it has big ass Submit and Cancel buttons. Failing something awful, the final product will probably be very similar.

Goodbye To Do List

I’ve gotten rid of the To Do List on the right side of this website. I realize that I’m not the type of person who responds well to pressure — internal or external — as motivation. With the exception of the first week, I was only adding things that I didn’t really care about (i.e. whether or not I’ll finish a book within the alloted time) or I could make vague enough not to matter (i.e. “The Web 2.0 Project”; what does that even mean?!). And in truth, most of the things I do I either CAN’T or WON’T tell people about until they’re actually done.

If anyone remembers and wants it, send me an email. It’s simple PHP and MySQL.

Social Network Apps

The big loser? Well, anyone who writes apps for social networks, pretty much by definition.

Valleywag.

Audiogalaxy

Remember Audiogalaxy? That was pretty much the glory days of p2p, eh? I don’t know why I was thinking of them today, but I came across this very interesting article written by one of the original programmers back in 2002 (when the RIAA took them down).

Panic – Coda

Smashing

A few weeks ago, I won a license for the “One-Window Web Development” program, Coda, from Smashing Magazine‘s – one of my favourite graphic/web design blogs – Anniversary Contest. Thanks to Hotmail, I didn’t end up getting the license until yesterday, but I’ve been using the full-featured demo since I found out that I won and it is undoubtably the best web development program I’ve used.

Smultron and Cyberduck

When I upgraded to Adobe’s CS3, I went with the Design Standard package instead of paying for Dreamwear (which I dislike and only ever used as an FTP client and text editor) and Flash (which I rarely use) in the Premium package.

I started using Smultron for text editing (HTML, PHP, CSS, JS, XML), which is an excellent open source editor with syntax highlighting and other goodies. I was using the open source Cyberduck for FTP; it integrates nicely with Smultron and has an awesome rubber duck logo/icon. Those two programs worked really well together, and while there were pay packages that worked better, they never worked better enough to justify the cost.

Enter Coda

Somehow I missed Coda – and it’s pretty perfect. It integrates the excellent Trasmit FTP client into an all-in-one web developing solution. It won the Apple Design Award in 2007 for Best Mac OS X User Experience with good reason. Everything just works exactly the way I want it to. It’s fast, it looks great, and it’s been saving me lots of time. I would have bought it if I hadn’t been a winner.

It puts everything you need for web development into one program. You can easily define and select sites from a pane that actually shows a thumbnail from your live-on-the-internet website – it’s one of those things that I find myself just looking at because it’s so aesthetically pleasing. From there, it displays all the required remote/local FTP functions/directories on the left while do your coding on the right. The syntax highlighting is great, it graphically shows you the start and stop of functions (i.e. when your cursor goes over a curly brace, it does this very neat looking fading circle around the relative start/stop brace), it offers suggestions for tags/functions, and it has a very unobtrusive automatic tag closer.

Extras

There is a LOT built into Coda that I don’t use. You can preview directly from the program, there’s a WYSIWYG CSS editor, Terminal, and Books. Actual books. They included digital versions of Desk References for HTML, CSS, and Javascript while also including a PHP reference book. I’m sure I’ll be using them during those “which tag does that?” moments.

Panic

I’m also pretty impressed with the company, Panic. Since I hadn’t heard from anyone about my license, I contacted the guys at Smashing who immediately figured out that it was a Hotmail problem. They got in touch with Panic, who confirmed that it was sent to my Hotmail address, and immediately got in touch with me with directions for the license. I gave them my details, and they gave me my code. All this happened within the course of a single work day. Incredibly friendly and quick customer service.

Coda sells for a $99, and is currently on sale for $79.

Coda

Smashing 1st Anniversary Giveaway | Events

Matthew (September 7th, 2007, 1:22 am)

Wonderfully relevant articles with a slightly unfortunate name.

#1: Coda

That comment won me a copy of Coda from Smashing Magazine! I’m absolutely tickled.

« Previous Entries