• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extension to the Table Class

#1
[eluser]myerman[/eluser]
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.

Code:
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;    
    
    }

}

#2
[eluser]aroman[/eluser]
wow..thanks myerman, this is useful for us.
have a nice day :p

#3
[eluser]Unknown[/eluser]
Or, if you are wanting to stick with table cells instead of the <br/>s in myerman's method:

Code:
/**
* 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.
*
* @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;
        }    
        
        $per_column = intval(count($array)/$col_limit);
        $new = array();
        $string = '';
        $step_count = 0;
        
        foreach ($array AS $value) $clean[] = $value;
        for ($i=1; $i <= $col_limit; $i++){
            for ($j=0; $j <= $per_column; $j++){
                $new[$j][$i] = $clean[$step_count];
                $step_count++;
            }
        }
        
        return $new;
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.