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?
#2

[eluser]InsiteFX[/eluser]
MY_Controller and extend your other Controllers from the MY_Controller

InsiteFX
#3

[eluser]JonoB[/eluser]
I'd suggest making a library (http://ellislab.com/codeigniter/user-gui...aries.html) or (if it only contains one function) then a helper would probably be OK too.
#4

[eluser]starsinmypockets[/eluser]
Is it acceptable to call other class methods from within library/helper functions - for instance,
Code:
$this->table->set_heading('foo');
?
#5

[eluser]JonoB[/eluser]
[quote author="starsinmypockets" date="1307901587"]Is it acceptable to call other class methods from within library/helper functions - for instance,
Code:
$this->table->set_heading('foo');
?[/quote]

Read the section in the above help document
Quote:Utilizing CodeIgniter Resources within Your Library
#6

[eluser]InsiteFX[/eluser]
Or you can extend the Table Class and add your new method to it!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB