Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
snippets
.htaccess / mod_rewrite
404 header
Ajax response page
Basic HTTP Auth
Column display sans near-empty columns
JavaScript favelets/bookmarklets
misc php stuffs
php tricks
8
found
auth
Name:
Category: snippets
Body:
<?php /** * When you list .. anything, and want to display the list in columns, it's easiest to do (simplified): * $per_col = ceil($total/$columns); * foreach($list as $item) { * echo $item."\n"; * if ($i++ % $per_col == 0) { * // start a new column * } * } * * But that sometimes leaves the last column near-empty; say, 14 items over 4 columns: * 1 5 9 13 * 2 6 10 14 * 3 7 11 * 4 8 12 * * It gets worse with larger lists, obviously. * Sometimes it's nicer (if they're big web buttons, say) to show them like this: * * 1 5 9 12 * 2 6 10 13 * 3 7 11 14 * 4 8 * * Below are 2 similar solutions that do this, not sure which one I like better yet.. * **/ ?> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>WordList</title> <style type="text/css" media="screen"> table { float: left; padding-right: 20px; border: 1px solid navy; } td { border: 1px solid gray; } </style> </head> <body> <?php // input & settings $words = explode(' ', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor'. ' incididunt ut. labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation'. ' ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in'. ' voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint cupidatat non proident,'. ' sunt culpa qui officia deserunt mollit anim id est laborum.'); $total = count($words); $columns = 7; // oft-used values $xl_columns = $total % $columns; // number of columns which will have an extra row $per_col = floor($total / $columns); // number of rows per column echo "Total <strong>$total</strong>, xl_columns <strong>$xl_columns</strong>, per_col <strong>$per_col</strong><br>"; /** * The first one most resembles the classical solution: loop through all items and insert </col><col> when * appropriate. $count % $per_col [+1] doesn't work as well, since mod $per_col doesn't "work" after some * columns which were $per_col+1 - the count will be off. So you'd have to re-start from zero after the larger * columns, and then you might as well just reset to zero everytime, and == to the $per_col[+1] instead of %==0. **/ $count = 0; echo '<table>'; foreach($words as $i => $word) { $count++; echo "<tr><td>$word</td></tr>"; if ($i == count($words)-1) break; if ($count == (($xl_columns > 0) ? $per_col+1 : $per_col)) { echo "</table>\n<table>\n"; $xl_columns--; $count = 0; } } echo "</table> <div style='clear: both;'></div>\n"; $xl_columns = $total % $columns; // reset this for second example.. /** * This one's shorter but uses 2 nested for-loops, which you may or may not like. * Not sure myself yet :-) It does have 2 less if-statements, and any table headers will only need to appear once * (though that could be a string var, too). **/ $w = 0; for ($i=0; $i < $columns; $i++) { echo "<table>\n"; for ($j=0; $j < (($xl_columns > 0) ? $per_col+1 : $per_col); $j++) { echo "<tr><td>".$words[$w++]."</td></tr>"; } echo "</table>\n"; $xl_columns--; } ?> </body> </html>
Footer