Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Table Generate() creating td with all values as attributes within td tag #1374

Closed
ray023 opened this Issue · 6 comments

3 participants

Ray Nowell Cory Kennedy-Darby Andrey Andreev
Ray Nowell

Copied from here

If you take the latest version of CI and replace the welcome controller with the following code:

public function index()
    {
        $this->load->library('table');
  
          $my_row["car_id"]= "1";
          $my_row["nickname"] =  "Bessie" ;
          $my_row["year"]=  "1975" ;
          $my_row["model"]=  "Bug" ;
          $my_row["engine"]=  "Water-Cooled" ;
          $my_row["owned"]= "0/0/0000" ;
          $my_row["sold"]= "0/0/0000" ;
          $my_row["for_sale"]=  "No" ;
          $this->table->add_row($my_row);
          
          $local_table = $this->table->generate();
          echo $local_table;
    }

The table html will render this way:

1 Bessie 1975 Bug Water-Cooled 0/0/0000 0/0/0000 No

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:

// args sent as indexed array
            if ( ! isset($args[0]['data']))
            {
                $ret_args = array();
                foreach ($args[0] as $key => $val)
                {
                    if (is_array($val) && isset($val['data']))
                    {
                        $ret_args[$key] = $val;
                    }
                    else
                    {
                        $ret_args[$key] = array('data' => $val);
                    }
                }
                $args = $ret_args;
            }

My part is ret_args (stands for "return arguments").

Cory Kennedy-Darby ckdarby referenced this issue from a commit
Cory Kennedy-Darby ckdarby Fixing extra td; Issue #1374 cbe905e
Cory Kennedy-Darby

Confirmed on 2.1 & development branch; Pull request above.

Andrey Andreev
Owner

This is not a bug, see the documentation on how to use the Table library: http://codeigniter.com/user_guide/libraries/table.html

Andrey Andreev narfbg closed this
Ray Nowell

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:

1. Turn the database result into a result_array
2. Loop through array
3. Make changes to fields as necessary
4. Set new result array in the generate method

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?

Andrey Andreev
Owner

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']);
Ray Nowell

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.

Andrey Andreev
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.