[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?