Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ray023 opened this issue May 20, 2012 · 6 comments
Closed

Comments

@ray023
Copy link

ray023 commented May 20, 2012

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").

@ckdarby
Copy link
Contributor

ckdarby commented Jun 5, 2012

Confirmed on 2.1 & development branch; Pull request above.

narfbg added a commit that referenced this issue Jun 6, 2012
@narfbg
Copy link
Contributor

narfbg commented Jun 7, 2012

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

@narfbg narfbg closed this as completed Jun 7, 2012
@ray023
Copy link
Author

ray023 commented Jun 7, 2012

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?

@narfbg
Copy link
Contributor

narfbg commented Jun 7, 2012

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

@ray023
Copy link
Author

ray023 commented Jun 7, 2012

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.

@narfbg
Copy link
Contributor

narfbg commented Jun 7, 2012

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants