Welcome Guest, Not a member yet? Register   Sign In
Dynamic Table Structure
#1

[eluser]mdcode[/eluser]
Hope someone can give me a clear concise answer for this as I've been going around in circles. I have data being passed to my view via my controller that I want to format in a table. This is all very well and good if I wanted to just display a new row for each data set (id, name, icon etc.)

However, what I want is to specify the number of columns I want the data in and for the table to add columns and rows accordingly, so I would end up with something like this (if I specified I wanted two columns):

Code:
+------+------+
| icon | icon |
| text | text |
+------+------+
| icon | icon |
| text | text |
+------+------+
| icon |
| text |
+------+

I have searched on Google but the information given is for things that are not using the MVC structure and so I'm a bit lost with it. I had something close with a foreach loop (to arrange the query array values) and a for loop inside that to get the columns but the values were either repeated (for the number of columns I specified) or not formatted correctly.

Here's my query:
Code:
/* used on the main page to retrieve the list of departments/sections */
    function get_departments()
    {
        $this->db->select('*');
        $this->db->from('departments');
        $this->db->order_by('name', 'ASC');
        
        $query = $this->db->get();
        return $query->result();
    }

And my controller passing the data:
Code:
$data['query'] = $this->index->get_departments();

And I don't really want to confuse anything by pasting my view..... I do appreciate any and all assistance though. Thanks.
#2

[eluser]pickupman[/eluser]
I was trying to do this in the past. What you are looking to do is a pivot table. Pivot tables are not easily done in php and mysql, and especially in CI/active record syntax. I have done in the past, I decided how many columns I want, iterate through the results, and add cells to an indexed array like col1, col2, col3. Then work through cells accordingly. You might find it usefully to create a nested table.

[code]
//In Controller
$results = $result->result(); //Save result object from model
$i = 0;
$data['col1_cell'] = array();
$data['col2_cell'] = array();
$data['col3_cell'] = array();
foreach($results as $row){
if($i%2 == 0 && $i == 6)
$data['col2_cell'][] = array('icon' => $row->icon, 'text' => $row->text);
elseif($i%3 == 0)
$data['col3_cell'][] = array('icon' => $row->icon, 'text' => $row->text);
else
$data['col1_cell'][] = array('icon' => $row->icon, 'text' => $row->text);
}

//Now loop through each cell to create a table. You may to create a view to do this.
$data['col1'] = $this->load->view('pivot_table', $data, TRUE); //Build col1
$data['col2'] = $this->load->view('pivot_table', $data, TRUE); //Build col2
$data['col3'] = $this->load->view('pivot_table', $data, TRUE); //Build col3

$this->load->view('final_view', $data); //Load page

//In final_view
<table>
<tr>
<td id="col1">&lt;?=$col1;?&gt;</td>
<td id="col2">&lt;?=$col2;?&gt;</td>
<td id="col3">&lt;?=$col3;?&gt;</td>
<tr>
</table>
</code]
It was a huge pain in the ***. This example is perfect, but that was off the top of my head. Obviously using 2 & 3 as a modulus would cause a problem so you would have to extend the syntax to accommodate for common multiples of 2&3.
#3

[eluser]mdcode[/eluser]
Thanks very much for the quick reply and suggestion. It sounds and looks like it would be more difficult and painful than it's worth to get this to work just so I can neatly display the text label for the icon underneath the icon. I'll take a look at this but using the templating and creating another view just to map out the table (and there would be several of these things), I really am wondering whether to trouble. Sad

Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB