MovableBlog: Links Divided Over 2 Columns

Nuance 2.0

December 4, 2002

On the Vancouver Webloggers links page, the links are alphabetized and divided into a two columns. But how?!

With PHP of course.

Place in your index template somewhere this code. I have a whole blog in MT dedicated to links, and in the case of Vancouver Webloggers, use the category "Other" for bloggers other than the authors of the site.

<?php

$links = array();

<MTEntries category="Other" sort_order="ascend" sort_by="title">
$links[] = "<$MTEntryBody encode_php="qq"$>";
</MTEntries>

$numlinks= count($links);

?>

Then, where you want the links to appear in two columns, insert this code:

<table>

<tr>
<td>
<?php
$half = $numlinks/2;
$half = (int) $half;

for ($i = 0;$i < $half; $i++)
{
print $links[$i]."<br />\n";
}
?></td>

<td><?php
for ($y = $half; $y < $numlinks;$y++)
{
print $links[$y]."<br />\n";
}
?>
</td>

</tr>
</table>

Note that for the title of each link's entry, I take the blog name and capitalize the first letter. Also, I follow the practice of not using "A" or "The" as the first word in the title. So "The Blog That Never Was" is inserted in the the title as "Blog That Never Was, The" and "a blog through time" becomes "Blog through time, A".

Posted by Richard at 12:12

Comments

Another way to do this, which would be slightly easier on php, would be:

<? $i=0;$j=0;?>
<MTEntries category="Other" sort_order="ascend" sort_by="title"><? $i++;?></MTEntries>
<? $cut_at = (int)($i/2);?>

<MTEntries category="Other" sort_order="ascend" sort_by="title">
<?
if(++$j == $cut_at)
echo '</td><td>';
?>
<$MTEntryBody$><br />
</MTEntries>