Welcome Guest, Not a member yet? Register   Sign In
Help With Generating Array For Table Class
#1

[eluser]Jay Logan[/eluser]
So I'm trying to use the HTML table class and I need to produce data from my database to use within the table. I'm assuming the database query needs to be stored in my model file. How can I produce an array of the data in the correct format for the table class? With model/controller combination I use, this error gets produced: Undefined table data.

Here is the model I'm using:

Code:
function unique_counties($id_name)
        {
            $this->db->select();
            $this->db->where('id_name', $id_name);
            $get_data = $this->db->get('VSS_src_network');
            $this->db->where_in('zip_code', explode(', ', $get_data->row()->addr_assoc));
            $this->db->distinct();
            $this->db->select('county_name');
            $this->db->orderby('county_name');
            $query = $this->db->get('JL_zip_codes');
            $result = $query->result_array();
            return $result;
        }


Here is my controller:

Code:
function show($id_name)
    {
        $this->load->model('licensee_model');

        $counties = $this->licensee_model->unique_counties($id_name);

        $this->load->library('table');
        $new_list = $this->table->make_columns($counties, 5);
        $table_options = array('table_open' => '<table width="100%">');

        $this->table->set_template($table_options);
        $data['counties_table'] = $this->table->generate($new_list);

        $this->load->view('profile', $data);
    }
#2

[eluser]TheFuzzy0ne[/eluser]
It may be wise to test if there's a result there before you pass it to the generate_table(). generate_table() returns 'Undefined table data' as a string when it receives a query result with no rows.
#3

[eluser]Jay Logan[/eluser]
Did a print_r of the variable and got this:

Array ( [0] => Array ( [county_name] => Daviess ) [1] => Array ( [county_name] => Dubois ) [2] => Array ( [county_name] => Hancock ) [3] => Array ( [county_name] => Henderson ) [4] => Array ( [county_name] => McLean ) [5] => Array ( [county_name] => Ohio ) [6] => Array ( [county_name] => Spencer ) [7] => Array ( [county_name] => Union ) [8] => Array ( [county_name] => Warrick ) )
#4

[eluser]Jay Logan[/eluser]
Made a few changes and now the table shows Array in each table cell. Edited my code above to show you what I'm using.
#5

[eluser]TheFuzzy0ne[/eluser]
That doesn't look like a CI_Result object. It's just an array. The table class checks to see if it's a query, and if it is, it checks that num_rows() is more than 0. Your function should return the object itself rather than the array.

Code:
function unique_counties($id_name)
{
    $this->db->select();
    $this->db->where('id_name', $id_name);
    $get_data = $this->db->get('VSS_src_network');
    $this->db->where_in('zip_code', explode(', ', $get_data->row()->addr_assoc));
    $this->db->distinct();
    $this->db->select('county_name');
    $this->db->orderby('county_name');
    return $this->db->get('JL_zip_codes'); // return the object.
}
#6

[eluser]TheFuzzy0ne[/eluser]
I sit corrected. I've only just twigged that you were passing an array over, and I was looking at the code wrong function.

If you are passing an array, you must supply two rows. The first row if used for the column names when the table is generated. Just passing over the object as I originally suggested would be ideal.
#7

[eluser]Jay Logan[/eluser]
So if I change this line

Code:
$new_list = $this->table->make_columns($counties, '5');

to...

Code:
$new_list = $this->table->make_columns($counties, 'county_name');

The correct county names show correctly. But I can't use the feature of making X number of columns. I would like to use that. How would I add the column name into the array?
#8

[eluser]TheFuzzy0ne[/eluser]
Please try the function I suggested. That should do everything for you.
#9

[eluser]Jay Logan[/eluser]
It didn't work, but the following code almost works.

Model
Code:
function unique_counties($id_name)
        {
            $this->db->select();
            $this->db->where('id_name', $id_name);
            $get_data = $this->db->get('VSS_src_network');
            $this->db->where_in('zip_code', explode(', ', $get_data->row()->addr_assoc));
            $this->db->distinct();
            $this->db->select('county_name');
            $this->db->orderby('county_name');
            $query = $this->db->get('JL_zip_codes');
            $result = $query->result_array();
            foreach($result as $county_info) {
                $counties[] = $county_info['county_name'];
            }
            return $counties;
        }

        function unique_cities($id_name)
        {
            $this->db->select();
            $this->db->where('id_name', $id_name);
            $get_data = $this->db->get('VSS_src_network');
            $this->db->where_in('zip_code', explode(', ', $get_data->row()->addr_assoc));
            $this->db->distinct();
            $this->db->select('city_name');
            $this->db->orderby('city_name');
            $query = $this->db->get('JL_zip_codes');
            $result = $query->result_array();
            foreach($result as $city_info) {
                $cities[] = $city_info['city_name'];
            }
            return $cities;
        }

Controller
Code:
function show($id_name)
    {
        $this->load->model('licensee_model');

        $data['licensee_info'] = $this->licensee_model->licensee_info($id_name);

        $this->load->library('table');

        $table_options = array('table_open' => '<table width="100%">');
        $this->table->set_template($table_options);

        $counties_list = $this->table->make_columns($this->licensee_model->unique_counties($id_name), 5);
        $data['counties_table'] = $this->table->generate($counties_list);

        $cities_list = $this->table->make_columns($this->licensee_model->unique_cities($id_name), 5);
        $data['cities_table'] = $this->table->generate($cities_list);

        $this->load->view('profile', $data);
    }



The problem now is that the counties also show in the city list.
#10

[eluser]TheFuzzy0ne[/eluser]
You'll need to run:
Code:
$this->table->clear();
after you generate a table if you want to use the $this->table object again.




Theme © iAndrew 2016 - Forum software by © MyBB