March 7, 2009

Failure to grasp the bigger picture

Last night me and Mom were at home watching TV when the phone rang. It being half past midnight I said that it was probably my Brother as no one else would call so late.

I was right: he was calling to say he’d crashed his car and could we go and pick him up.

So about 45 minutes later we get there and my Brother is standing by his car. As we pull up and the headlights shine on him it’s instantly apparent that he’s broken a side light, crushed the wheel arch, broken the steering and punctured a tyre. My Brother is looking clearly shaken and a small tree is lying near by that has clearly been uprooted by his car.

First thing Mom says when we get out of the car is, “You’ve lost a hub cap at the front”.

Filed under: Uncategorized, musings
Tags:
komakino @ 1:35 pm

March 2, 2009

Using PHP to get blogger paths as relative paths

I was manipulating my blog template today when I came across a new problem (new for me, anyway). The paths that blogger uses for the archive links and various other bits and pieces are given as absolute paths. Normally this would be OK if you’re using a totally self contained blog, but in my case I load my blog page into the middle of my main PHP page so that everything is nice and consistent (no matter which link you click on my menu you get the same menu bar and layout, all provided by my main index.php page and stylesheet). However, if you clicked a link to an archived blog page it opened up as a standalone page: no menu, no stylesheets, no nice layout.

What I wanted was to be able to get blogger to use relative paths to the archive pages so that I could then pass them to my index.php to include.

Fortunately PHP makes this easy. In my blog’s template there’s a section that looks as follows:

<BloggerArchives>
<li><a href="<$BlogArchiveURL$>"><$BlogArchiveName$></a></li>
</BloggerArchives>

What I did was to use a little PHP to first take the text that Blogger replaces the BlogArchiveURL with, tokenise it around the / character and then use just the last token (the file name) in the anchor tag as the link (I’ve shortened the domain name to just ‘rj’ to make lines fit better):

<BloggerArchives>
<?php
$myarray = split('/',"<$BlogArchiveURL$>");
$myarray[sizeof($myarray)-1];
echo '<li>';
$ind = sizeof($myarray)-1;
echo '<a href="http://www.rj.co.uk/index.php?page='.$myarray[$ind].'">';
?>
<$BlogArchiveName$></a></li>
</BloggerArchives>

The ’sizeof($myarray)-1′ is to use the last item of the array (-1 because counting starts at 0, i.e. if there are 4 items then sizeof = 4 but the last item is at index 3).

I did something similar to get the links for previous posts, except this time I needed to pass the year and month subdirectories that blogger stores the files under as well, so I used a slight variation on split() which passes each token into a variable directly:

<BloggerPreviousItems>
<?php
list($http, $blank, $retro, $y, $m, $p) =
split('/',"<$BlogItemPermalinkURL$>");

echo '<li><a href="http://www.rj.co.uk/index.php?page='.$y.'/'.$m.'/'.$p.'">';
?>
<$BlogPreviousItemTitle$></a></li>
</BloggerPreviousItems>

You’ll also notice that it’s only the year, month and page name I’m interested in. There’s probably a tidier way but I’m not a great PHP coder. I just use what I need to get by :)

Filed under: Uncategorized, musings
Tags:
komakino @ 11:16 pm

Older Posts »
x