Neoseeker : Blogs : tekmosis : Multi Column Display In PHP

saiko saru

Multi Column Display In PHP

Have you ever had an array or list of items that you wanted formatted properly in a nice mutiple column layout rather than just an entire list? This method should help you out a bit with that by taking advantage of PHP's array_chunk() function.

php code

$arr = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');

$max_num_cols = 4; //the maximum amount of columns that can be output
$min_items_per_col = 5; //The minimum amount of items per column
$col_limit_calculation = $max_num_cols * $min_items_per_col; //used to determine which value to chunk by
$num_rows = count($arr); //total number of items in the array

//determins how many values to chunk our array by while still keeping with our $max_num_cols
$chunk_count = ($num_rows >= $col_limit_calculation) ? ceil(($num_rows / $max_num_cols)) : $min_items_per_col;

//The chunked array that we get to loop through now
$alphabet_chunk_array = array_chunk($arr, $chunk_count);

echo "<table border="1"><tr>";
foreach ($alphabet_chunk_array as $alphabet_array) {
    echo "<td valign="top">";
    foreach ($alphabet_array as $alphabet) {
        echo $alphabet . "<br />";
    }
    echo "</td>";
}
echo "</tr></table>";
 

Comments

  • 1 thumbs!
    bobbonew since Dec 2002 | Jan 16, 09
    Interesting function, would it have much use inside a smarty template powered page?
  • -1 thumbs!
    bobbonew since Dec 2002 | Jan 16, 09
    What happened to Kspies comment?
  • 1 thumbs!
    kspiess since Jun 2007 | Jan 16, 09
    Who, me? What did I say?
  • 0 thumbs!
    bobbonew since Dec 2002 | Jan 16, 09
    Hmmm must've been another admin then? Somebody posted some quality smarty code. Went on an hour later and poof it was gone!
  • 0 thumbs!
    huntyr since Feb 2007 | Jan 16, 09
    sorry, that we me. I removed it because it was untested. So here it is all fixed:
    code

    <table>
    <tr>
    {foreach name=foo_name item=bar from=$foo}
    {if $smarty.foreach.foo_name.index % 5 eq 0}
    <td>
    {/if}
    {$bar}<br />
    {if $smarty.foreach.foo_name.iteration % 5 eq 0}
    </td>
    {/if}
    {/foreach}
    </tr>
    </table>
    Last edited by huntyr :: Jan 16, 09
    • 0 thumbs!
      tekmosis since Jul 2006 | Jan 19, 09
      You can actually do it a lot easier in smarty. Using vars from my example this is the smarty equivalent.
      code
      {html_table loop=$arr cols=4}

      Resource
      Smarty html_table documentation
      Last edited by tekmosis :: Jan 19, 09
  • 0 thumbs!
    kspiess since Jun 2007 | Jan 21, 09
    Oh probably was me. I post smart code all over the place. Its like sort of a hobby of mine and stuff.
    • 0 thumbs!
      The Slayer since May 2003 | Jan 21, 09
      quote
      I post smart code all over the place. Its like sort of a hobby of mine and stuff.
      That made me laugh.
  • 0 thumbs!
    Hell Fire since May 2003 | Apr 12, 09
    Really really awesome Tek. Best code posted on the net my far.

    Cheers
    • 0 thumbs!
      tekmosis since Jul 2006 | Apr 14, 09
      thanks! just saw your thread on this; glad it was able to help you out (:
      Last edited by tekmosis :: Apr 14, 09
  • 0 thumbs!
    Mark | Oct 13, 09
    Here's the video tutorial on how to do it with values populated from the database: http://www.sebastiansulinski.co.uk/web_design_tutorials/tutorial/48/display_records_in_multi_column_format_with_php
Add your comment:
Name *:  Members, please LOGIN before posting
Email:
Live user
verification *:

Enter the letters you see in the image (without spaces)
Comment *:
(0.7895/d/aeon)