Welcome Guest, Not a member yet? Register   Sign In
Extension to the Table Class

I use the Table class a lot in my work, particularly if I have to display a bunch of categories (or any other list o' links) on a page and I don't want to bother with manually formatting them into columns.

One thing I didn't like is that the class alphabetizes across columns instead of down rows...so if you have a list of items (Apple Banana Candy Dog Elephant Fox Grape), you would have to read left-right instead of up-down to follow any alphabetized list...which doesn't really work for most people.

So I rolled up my sleeves to create an extension to the Table class, a function called make_columns_vertical(), which creates vertically organized lists. There may have been an easier way to do this (like maybe grouping within mySQL in some weird way) but this seemed easier.

With any luck, it will be useful to you. If you spot any obvious errors or anything stupifyingly stupid, please bring it up. Just trying to make a customer happy, no ego involvement.

class MY_Table extends CI_Table {
     * Set columns vertically.  Takes a one-dimensional array as input and creates
     * a multi-dimensional array with a depth equal to the number of
     * columns.  This allows a single array with many elements to  be
     * displayed in a table that has a fixed column count.
     * Listings are made vertically instead of horizontally.
     * So for example, instead of A, B, C going across with D, E, F on the second row,
     * First column would contain A B C and second column would contain D E F.
     * @access    public
     * @param    array
     * @param    int
     * @return    void
    function make_columns_vertical($array = array(), $col_limit = 0) {        
        if ( ! is_array($array) OR count($array) == 0){
            return FALSE;
        // Turn off the auto-heading feature since it's doubtful we
        // will want headings from a one-dimensional array
        $this->auto_heading = FALSE;
        if ($col_limit == 0){
            return $array;
        //$array_items = count($array);
        $per_column = intval(count($array)/$col_limit);
        $new = array();
        $string = '';
        while (count($array)){
            for ($i=1; $i <= $col_limit; $i++){
                for ($j=0; $j <= $per_column; $j++){
                    $item = array_shift($array);
                    $string .= $item . "<br/>";
                $new[$i] = $string;
                $string = '';

        return $new;    


Messages In This Thread
Extension to the Table Class - by El Forum - 04-30-2008, 10:16 AM
Extension to the Table Class - by El Forum - 04-30-2008, 10:31 AM
Extension to the Table Class - by El Forum - 02-01-2010, 11:51 AM

Theme © iAndrew 2016 - Forum software by © MyBB