more fun with Blogger tags and PHP

I wanted to enhance my tag list PHP program to do a couple more things. Basically, I wanted it to be aware of when we are on a label page, so that we could make the list item for the current page plain text (instead of a link), and so that I could put a link back to the main page on any label page.
Normally, I have a link back to the main page in the header of any page other than the main one. I do this through the Blogger template, using the “ItemPage” and “ArchivePage” Blogger template tags to figure out when we’re not on the main page. Unfortunately, there’s no “LabelPage” template tag, as far as I can tell.
Making one of the list items text rather than a link broke the sorting, since I was just sorting on the text of the list item, including the “a href…” stuff. To get around this, I switched to using an associative array, where the key value is the tag.
Here’s the code:

<?php
$dirname = ".";                        # current directory
$uri = $_SERVER["REQUEST_URI"];        # the page we're on
$bOnLabelPage = false;                # are we on a label page?
$dir = opendir($dirname);
$file_list = array();
$i = 0;

while (false != ($file = readdir($dir)))
    {
    if (ereg(".html$", $file))
        {
        $tag = substr($file, 0, strlen($file)-5);
        $key = strtoupper($tag);
        if (ereg("$file$", $uri))
            {
            $file_list[$key] = "<li>$tag";
            $bOnLabelPage = true;
            }
        else
            $file_list[$key] = "<li><a href='/labels/$file'>$tag</a>";
        $i++;
        }
    }
closedir($dir);
#natcasesort($file_list);
ksort($file_list);
?>
<h1>My Tags</h1>
<?php
if ($bOnLabelPage) echo ("<a href='/'>[Back to main page]</a>");
?>
<ul>
<?php foreach ($file_list as $file) echo($file); ?>
</ul>

A little more complex than what I started with, but still not too bad.
As a side note, I am using TextWrangler to edit PHP files on my Mac, and it’s working pretty well. It’s got syntax-highlighting for PHP and HTML. I’m also now using Fugu to copy files up to my server. It integrates well with TextWrangler, so that I can just keep a file open in TextWrangler and have it copied back to the server every time I save. Nice.
On Windows, I’m mostly using Multi-Edit and WinSCP. That combo works pretty well too, though I’m using an older version of Multi-Edit that doesn’t have PHP syntax highlighting.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.