Welcome Guest, Not a member yet? Register   Sign In
Extending table functions
#1

[eluser]starsinmypockets[/eluser]
I built the following function to dynamically build a table from db values.

Code:
function make_table($table = '', $fields = array(), $link = '')
    {    
        $url = '';
        $this->table->set_heading(array_values($fields));
        //build db query
        foreach ($fields as $key => $value)
        {    
            //get result array from db
            $this->db->select($key);
            $this->db->from($table);
            $result[] = $this->db->get()->result_array();          
        }    
        //build rows[] array from result
        foreach ($result as $inner_array)
        {
            foreach ($inner_array as $rownumber => $pair)
            {
                $rows[$rownumber][] = current($pair);
            }
        }    
        
        if ($link)
        {
            for ($i = 0; $i < count($rows); $i++)
            {
                //build url from $link variable
                if ($link['parameters']) {
                    // if $link['paramaters'] contains references
                                        // to the field indexes, substitute value
                    // from rows[] variable
                    $url = '';
                    foreach ($link['parameters'] as $param) {
                        if (in_array($param, $fields))
                        {    
                           $t = array_search($param,         array_values($fields));    
                            $param = $rows[$i][$t] . '/';
                            $url .= $param;
                            var_dump($t);
                        } else {
                        $url .= $param . '/';
                        }
                    }
                }
                $rows[$i][] = '<a href = "' . $url . '">' . $link['title'] . '</a>';
            }
        }                
        echo $this->table->generate($rows);
    }

I like what this does, and how it does it, but I have a couple questions:

1. Where should this live so that I can access it from all of my controllers?
2. Should I separate out the db calls for semantic reasons?


Messages In This Thread
Extending table functions - by El Forum - 06-11-2011, 10:07 AM
Extending table functions - by El Forum - 06-11-2011, 07:46 PM
Extending table functions - by El Forum - 06-12-2011, 02:41 AM
Extending table functions - by El Forum - 06-12-2011, 06:59 AM
Extending table functions - by El Forum - 06-12-2011, 11:21 AM
Extending table functions - by El Forum - 06-12-2011, 01:40 PM



Theme © iAndrew 2016 - Forum software by © MyBB