Welcome Guest, Not a member yet? Register   Sign In
Extend table meta data to include values in db
#1

[eluser]gh0st[/eluser]
I'm trying to figure out a way of combining Active Record and the table meta data functionality.

Instead of doing:

Code:
foreach($query->result() as $row):
print $row->name;
print $row->title;
print $row->body;
// and so on for possibily lots of fields
endforeach;

I want to have the ability to do the listing automatically, especially when I am on listing pages in my admin section.

According to the docs, you can (not all DB's handle this) get meta data:

Quote:$fields = $this->db->field_data('table_name');

foreach ($fields as $field)
{
echo $field->name;
echo $field->type;
echo $field->max_length;
echo $field->primary_key;
}


But as you will notice there is no $field->value, which would contain the value stored at said field.

What I'd like is to combine the Active Record's table meta data with the actual value.

This will prove useful for when I am in my admin section and I want to build a quick table using the fields I have asked for in my query.

I've tried the following:

1. Loop through the meta data and my query array to find common array key's and then try to append a field->value to my array.

This doesn't work, also if the database is really big this loop would be very slow.

Any help on this would be great.
#2

[eluser]Michael Wales[/eluser]
It's 2 completely different processes so you're really not going to have a decent way of doing it all in one step like you want. Here's what I would do:

Code:
echo '<table><tr>';
// Let's make headers for our table
$field_names = array();
$fields = $this->db->field_data('table');
foreach ($fields as $field) {
  echo '<th>' . $field->name . '</th>
  $field_names[] = $field->name;
}
echo '</tr><tr>';

// Now let's query the database
$this->db->select(implode(',', $field_names));
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
  echo '<tr>';
  foreach ($row as $field) {
    echo '<td>' . $field . '</td>';
  }
  echo '</tr>';
}
echo '</table>';
#3

[eluser]gh0st[/eluser]
Thanks for your help -- I am only using it for the admin side when I am listing stuff and its just a quick way to get a HTML table up and running.

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB