<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="content-type"
 content="application/xhtml+xml; charset=utf-8" />
  <title>Multi-column balanced lists :: robfelty.com</title>
  <style type='text/css'>
  div.col {float:left;
           padding:1em;}
  h2 {text-align:center}
  .col1 {background-color:#DDF}
  .col2 {background-color:#FFD}
  .col3 {background-color:#DFF}
  .col4 {background-color:#FDF}
  .col5 {background-color:#EEE}
  </style>
</head>
<body>

<?php
/* 
* print out items in a three column, balanced list
* Copyright 2010 
* Robert Felty
* Feel free to use as you wish
*/
$numCols 5;
$numItems 56;
$itemsPerCol ceil($numItems $numCols);
$adjustedItemsPerCol=$itemsPerCol;
$blank =$itemsPerCol - ($numItems $itemsPerCol); 
if (
$blank!=$itemsPerCol && $blank >0) {
  
$adjustedItemsPerCol--;
}
$itemNum=1;
$itemColNum=1;
$colNum=0;
$columns = array();
$colClassNum $colNum+1;
$columns[$colNum] =  "<div class='col col$colClassNum'><h2>$colClassNum</h2><ol>\n";
while (
$itemNum <=  $numItems) {
  
$columns[$colNum] .= "<li>$itemNum</li>";
  if (
$itemColNum>=$itemsPerCol && $itemNum<$numItems) {
    
$rem =  $numItems $itemsPerCol;
      
//echo "$numItems, $itemsPerCol, mod = $rem\n";
    
$columns[$colNum] .= "</ol></div>\n";
    
$colNum++;
    if (
$colNum == ($numCols $blank)) {
      
$itemsPerCol=$adjustedItemsPerCol;
    }
    
$colClassNum $colNum+1;
    
$columns[$colNum] = "<div class='col col$colClassNum'><h2>$colClassNum</h2><ol>";
    
$itemColNum=0;
  }
  
$itemNum++;
  
$itemColNum++;
}
$columns[$colNum] .= "</ol></div>\n";
foreach (
$columns as $column) {
  echo 
$column;
}
?>
</body>
</html>