Archive for the 'Programming' Category

« Previous Entries

New design for the Excuse Rolodex

Excuse Rolodex

Gruman Design and Development

I’ve redesigned my portfolio.

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

Update: Charismatic Programmer posted a much better way to do this in the comments section. This is their method:

function serial_comma($array)
{
     $size = sizeof($array);
     switch($size)
     {
          case 0:
          case 1:
               return reset($array);
          case 2:
               return join(' and ', $array);
          default:
               return join(', ', array_slice($array, 0, $size – 1)).', and '.$array[$size - 1];
     }
}

Example:

$array = array('Anthony Bourdain', 'Mario Batali', 'Thomas Keller');
echo serial_comma($array);

Produces:

Anthony Bourdain, Mario Batali, and Thomas Keller

The old 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).

« Previous Entries