Closed ray023 closed 12 years ago
Confirmed on 2.1 & development branch; Pull request above.
This is not a bug, see the documentation on how to use the Table library: http://codeigniter.com/user_guide/libraries/table.html
The documentation on the Table class does not cover how to manipulate data from a database resultset before it is rendered. (e.g. take a field with an id and turn it into a checkbox containing that id).
This question has been asked several times on the forums and the ones I found came up with the same solution:
The example I listed to reproduce is a simplified version of the following code:
from the model:
function get_pharmacist_list()
{
$select_fields = "pharmacist_id
,last_name
,first_name
,CASE active WHEN 0 THEN 'No' WHEN 1 THEN 'Yes' ELSE '' END AS Active";
//Get the query results for the clinical pharmacists
$this->db->select($select_fields, false);
$this->db->from('pharmacist');
$this->db->where('facility_id',$this->facility_id);
$this->db->order_by('last_name, first_name');
$query = $this->db->get();
return $query->result_array();
}
from the controller:
function index()
{
//...
$this->load->library('table');
$this->table->set_heading('','Last Name','First Name','Active');
$this->load->model('Pharmacist_model');
$pharmacist_array = $this->Pharmacist_model->get_pharmacist_list();
//loop through the results and hyperlink the id
foreach($pharmacist_array as $row)
{
$row['pharmacist_id']= '<input class="GridCheckbox" type="checkbox" name="_chkIntervention'.$row['pharmacist_id'].'" >';
$this->table->add_row($row);
}
$local_table_template = set_table_template();
$this->table->set_template($local_table_template);
$local_table = $this->table->generate();
//... }
The above worked in 1.72 but has the logged issue above in the new version.
If this is not a bug, then what is the correct action to take when database results need changing before rendering?
The Table library can't possibly know what your idea or database design is, it doesn't even know that you're using a database. In the example that you've shown, you just need to change this line:
$this->table->add_row($row);
... to this:
$this->table->add_row($row['pharmacist_id'], $row['last_name'], $row['first_name'], $row['Active']);
it doesn't even know that you're using a database.
I disagree as there is a function meant to handle database results: see function _set_from_object($query) in table.php.
Also, the Table documentation gives an example on how to generate a table from a query. It has to understand on some level that the information is coming from a database.
Regardless of the above argument, it does not apply to me. I did not state (nor imply) the Table Library should understand my database design; only that I needed to manipulate the results before render.
In 1.72 the solution was a lot cleaner than what you prescribe now.
There might be a method that tries to transform a database result set into a table row, but add_row()
has nothing to do with it. And while there is some example for generating a table from a DB result set (supposedly utilizing the method that you noted) - you're not using it (not that I know if it works, just saying).
Examples for add_row()
usage are all over the Table library manual and not one of them shows that you could use a structure like yours. Every example shows either multiple arguments passed, or an array with numeric keys.
Copied from here
If you take the latest version of CI and replace the welcome controller with the following code:
The table html will render this way:
Notice the first td contains all the elements of the array and should not belong.
The problem is in ../system/libraries/Table.php in function _prep_args around line 183.
What's happening is that the values for the array are getting appended to the array passed. I assume it should replace the array passed.
I was able to fix by making the following code change:
My part is _retargs (stands for "return arguments").