You are logged out. Click here to login. « Much Food |
Main
| Burnout »
September 16, 2004
MOVEABLE AWESOME
WARNING: This will be a nerdy entry. Yes, even more so that usual. There, I said it.
So...I'm okay at web-y stuff. I don't think I'm the bestest ever, but sometimes I do stuff that I think is kinda cool. Today I did one of those things, and I'd like to share it with you.
You may or may not have noticed that I created a marathon log recently. It's been live for a few days, but I'm still in the process of hammering out the details. One of those details was that, for each day I had an entry on, I wanted to keep track of which day of training it was, how much money I had raised, and how many days I have left to fundraise. At first, I was typing them in manually into each entry. This quickly became tedious, so I started looking into a more automated way of doing this.
While reading about PHP time functions, (hey, I said this would be nerdy) I happened upon this comment which gave a code snippet to calculate the elapsed days between two dates. This was perfect for what I was trying to do. So I created couple new templates which have something like this:
$firstdayoftraining = "18 September 2004";
$fundraisingdeadline = "26 November 2004";
$dayselapsed = round((strtotime($today) - strtotime($firstdayoftraining))/(60*60*24));
if($today >= $firstdayoftraining) {
$dayselapsed = $dayselapsed + 1;
}
$dayselapsed2 = round((strtotime($fundraisingdeadline) - strtotime($today))/(60*60*24));
$s = " ";
if($dayselapsed2 != 1) {
$s = "s ";
}
echo "Day " . $dayselapsed . " of training";
echo $dayselapsed2 . " day" . $s . "left to fundraise";
I include them in my Movable Type templates right after the date. I also have to add this before the includes:
$today = <$MTEntryDate format="%d %B %Y">;
This way, the elapsed days are calculated automatically from the date of each entry.
The last thing I needed to do was some way of keeping track of how much money I had raised so far in each entry. It wasn't as simple as including one file because the total would (presumably) be different for different entries. I wanted to able to look back and see my progress. I thought about this for a bit and came up with what I think is a pretty clever solution. For each entry, I put the current fundraising total in the "Extended Entry" field. Next, I created a new daily archive template that only had the following:
<MTEntries lastn="1"><$MTEntryMore$></MTEntries>
I set the archive file name to be: fundraising/<$MTArchiveDate format="%Y_%m_%d"$>.php. So for each day that has an entry, a file is created in the fundraising/ folder.
So for each day, I include that day's fundraising total in the template:
include '[path]/archives/fundraising/<$MTEntryDate format="%Y_%m_%d"$>.php';
The last thing I did was to leverage those new fundraising total files for donation box on the left side. I added the following code:
Total:
<MTEntries sort_order="descend" lastn="1">
<?php include '[path]/archives/fundraising/<$MTEntryDate format="%Y_%m_%d"$>.php'; ?>
MTEntries>
Assuming the fundraising total for the last entry is the current total, the donation box on the left side will automatically update as I create new entries.
So...all that was probably only interesting to me, but I was quite proud of myself. And now my site is quite easy to maintain. Woo!
[12:13 AM]
+++++
|