array( "alpha item one", "alpha item two", ), "Category Beta" => array( "beta item one", "beta item two", "beta item three", "beta item four", ), "Category Gamma" => array( "gamma item one", ), "Category Delta" => array( "delta item one", "delta item two", "delta item three", "delta item four", "delta item five", "delta item six", "delta item seven", "delta item eight", ), "Category Epsilon" => array( "epsilon item one", ), ); /** * Plain lists. */ function lists_html($lists) { $output = ""; foreach ($lists as $list => $items) { $output .= "
\n"; $output .= "

". $list ."

\n"; $output .= " \n"; $output .= "
\n"; } return $output; } /** * Simple float. */ function columns_float_lists_html($lists, $number_of_columns) { $output = ""; foreach ($lists as $list => $items) { $output .= "
\n"; $output .= "
\n"; $output .= "

". $list ."

\n"; $output .= " \n"; $output .= "
\n"; $output .= "
\n"; } return $output; } /** * Float into cleared rows. */ function columns_float_rows_lists_html($lists, $number_of_columns) { $output = ""; $column = 0; $row_open = FALSE; foreach ($lists as $list => $items) { if ($column == 0) { $output .= "
\n"; $row_open = TRUE; } $output .= "
\n"; $output .= "
\n"; $output .= "

". $list ."

\n"; $output .= "
    \n"; foreach ($items as $item) { $output .= "
  • "; $output .= $item; $output .= "
  • \n"; } $output .= "
\n"; $output .= "
\n"; $output .= "
\n"; if ($column == $number_of_columns - 1) { $output .= "
\n"; $row_open = FALSE; $column = 0; } else { $column++; } } // Failsafe to close last row. if ($row_open) { $output .= "\n"; } return $output; } /** * Float into compacted columns. */ function columns_float_compact_lists_html($lists, $number_of_columns, $item_height = 1, $overhead_height = 0) { $output = ""; // Create an array of the heights of each list. $heights = array(); foreach ($lists as $list => $items) { $heights[$list] = $overhead_height + (count($items) * $item_height); } // Sort the heights array from largest to smallest. arsort($heights); // Set the target height of one column. $target_height = array_sum($heights) / $number_of_columns; // Create an array to store which list goes into which column. $columns = array(); // Build the columns. for ($column = 0; $column < $number_of_columns; $column++) { // Ensure there are items still remaining. if (!empty($heights)) { // Create a tracker for the remaining space in the column. $remaining_space = $target_height; // Add the largest list to the column first. do { reset($heights); $list = key($heights); // Adjust $remaining_space, remove data from $heights, and add data to $columns. $remaining_space -= $heights[$list]; unset($heights[$list]); $columns[$column][$list] = $lists[$list]; } while ($remaining_space > reset($heights) && !empty($heights)); // If there is space left in the column, find the best list to add. if ($remaining_space > 0 && !empty($heights)) { $closest = array(); foreach ($heights as $list => $height) { if (empty($closest)) { $closest["list"] = $list; $closest["height"] = $height; } else if (abs($remaining_space - $height) < $closest["height"]) { $closest["list"] = $list; $closest["height"] = $height; } } // Ensure height of what we are adding is no more than double the space remaining. if ($closest["height"] <= 2 * $remaining_space) { // Adjust $remaining_space, remove data from $heights, and add data to $columns. $remaining_space -= $closest["height"]; unset($heights[$closest["list"]]); $columns[$column][$closest["list"]] = $lists[$closest["list"]]; } } } } // Finally build the HTML. foreach($columns as $column) { $output .= "
\n"; foreach ($column as $list => $items) { $output .= "
\n"; $output .= "

". $list ."

\n"; $output .= " \n"; $output .= "
\n"; } $output .= "
\n"; } return $output; } print "
\n"; print "

Plain lists

\n"; print lists_html($example); print "
\n"; print "
\n"; print "

Simple float

\n"; print columns_float_lists_html($example, 3); print "
\n"; print "
\n"; print "

Float with rows

\n"; print columns_float_rows_lists_html($example, 3); print "
\n"; print "
\n"; print "

Float in compacted columns

\n"; print columns_float_compact_lists_html($example, 3); print "
\n"; print "
\n"; print "

Float in compacted columns with heights

\n"; print columns_float_compact_lists_html($example, 3, 21, 106); print "
\n"; ?>