Monday, February 9, 2004
Alphabetical Archives by letter
Sites with topical articles or listings may wish to provide a variety of archive options to their readers. One good way to present a lot of data is by alphabetical order.
- PHP required.
The script has notations for what each section does. For a working example, TheRedKitchen's alphabetical archives were the initial place this code was used. It's been cleaned up and optimized a bit.
<?
//set the lastn value on both greater than your number of entries.
$titles = array(<MTEntries lastn="800">"<$MTEntryTitle encode_php="qq">", </MTEntries>);
$links = array(<MTEntries lastn="800">"<$MTEntryLink encode_php="qq">", </MTEntries>);
foreach ($titles as $firsts) { $first[]=strtolower(substr($firsts, 0, 1)); }
//defaults listing to show the letter a's entries, otherwise uses the letter from the URL.
if (!isset($_REQUEST['letter'])) { $letter="a"; }
else { $letter=$_REQUEST['letter'];}
//sorts the titles and links but lets them keep the same value.
array_multisort($titles, $links, $first);
$count = count($titles);
//create the links to each unique letter on the top of the pages.
$firstarray = array_values(array_unique($first));
foreach ($firstarray as $let) {
echo " <a href=\"?letter=", $let, "\">", $let, "</a> ";
}
echo "<hr \>\n\r";
//display the links to entries beginning with the selected letter by checking all entries for a match.
for ($all = 0; $all < $count; $all++) {
if ($letter == $first[$all]) {
echo "<a href=\"", $links[$all], "\">", $titles[$all], "</a><br \>\n\r";
}
}
?>
//set the lastn value on both greater than your number of entries.
$titles = array(<MTEntries lastn="800">"<$MTEntryTitle encode_php="qq">", </MTEntries>);
$links = array(<MTEntries lastn="800">"<$MTEntryLink encode_php="qq">", </MTEntries>);
foreach ($titles as $firsts) { $first[]=strtolower(substr($firsts, 0, 1)); }
//defaults listing to show the letter a's entries, otherwise uses the letter from the URL.
if (!isset($_REQUEST['letter'])) { $letter="a"; }
else { $letter=$_REQUEST['letter'];}
//sorts the titles and links but lets them keep the same value.
array_multisort($titles, $links, $first);
$count = count($titles);
//create the links to each unique letter on the top of the pages.
$firstarray = array_values(array_unique($first));
foreach ($firstarray as $let) {
echo " <a href=\"?letter=", $let, "\">", $let, "</a> ";
}
echo "<hr \>\n\r";
//display the links to entries beginning with the selected letter by checking all entries for a match.
for ($all = 0; $all < $count; $all++) {
if ($letter == $first[$all]) {
echo "<a href=\"", $links[$all], "\">", $titles[$all], "</a><br \>\n\r";
}
}
?>

