PHP: A to Z: Table of Contents
As more information gets put up on your website a table of contents becomes more useful. So having an A to Z list at the top of a page, with HTML anchors down the page makes for quick navigation. A list like this:
| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
Of course typing in the whole alphabet is a bit of a waste of time. So you can use the PHP range() function within a foreach loop. The code looks like this:
<?php
print "<table width=\"78%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "<tr>\n";
foreach (range('A','Z') as $char)
{
print "<td width=\"3%\"><a href=\"#$char\" title=\"Goto To $char\">$char</a></td>\n";
}
print "</tr>\n";
print "</table>\n";
?>
The range() function creates an array containing a range of elements. It it mostly used with numbers, but as PHP knows its alphabet it can be used with letters too. The format is:
range(start,end,step)
The step is optional and defaults to 1. You can reverse the alphabet too, though I cannot see why you would want to! Just use the code: range('Z','A')
