3

CI table->generate($data1, $data2, $data3) will output my data in a form of simple table like:

<table>
    <tr>
        <td>data1</td>
        <td>data2</td>
        <td>data3</td>
    </tr>
</table>

What if I need a complex cell layout with multiple $vars within each cell:

$data1 = array('one', 'two', 'three'); 

and I want something like this:

<table>
    <tr>
        <td>
            <div class="caption">$data1[0]</div>
            <span class="span1">$data1[1] and here goes <strong>$data1[2]</strong></span>
        </td>
        <td>...</td>
        <td>...</td>
    </tr>
</table>

How should I code that piece?

For now I just generate the content of td in a model and then call generate(). But this means that my HTML for the cell is in the model but I would like to keep it in views.

2 Answers 2

2

What I would suggest is have a view that you pass the data that Generates the td structure. Capture the output of the view and pass this to the table generator. This keeps your structure in the view albeit a different one.

Sign up to request clarification or add additional context in comments.

1 Comment

Great solution! Didn't know that you can load->view() into a variable!
0

Hailwood's answer isn't the best way to do it. the html table class has a data element on the add_row method. so the code would be:

$row = array();
$row[] = array('data' => "<div class='caption'>{$data1[0]}</div><span class='span1'>{$data1[1]} and here goes <strong>{$data1[2]}</strong></span>");
$row[] = $col2;
$row[] = $col3;

$this->table->add_row($row)
echo $this->table->generate();

as an aside, having a class named caption in a table is semantically confusing because table has a caption tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.