views:

689

answers:

2

I have data coming from my model into this Controller

function index() {
 $this->load->model('work_m');
 $data = array();  
 $config['base_url'] = base_url().'index.php/work/index/';
 $config['total_rows'] = $this->db->count_all('work');
 $config['per_page'] = '10';
 $config['full_tag_open'] = '<p>';
 $config['full_tag_close'] = '</p>';
 $this->pagination->initialize($config);
 $data['result'] = $this->work_m->get_records($config['per_page'],$this->uri->segment(3));   
 $data['title'] = 'Page Display';
 $data['content'] = 'todo/work_display';
 $this->load->view('template3', $data);
}

I need to create a table(using the HTML Table Class) with the following links in the cells(this is done by hand the old way and is in my view)

<td width="8%"><?php echo anchor("work/fill_form/$row->id", $row->id);?></td>
<td width="10%"><?php echo $row->date; ?></td>
<td width="20%"><?php echo $row->title; ?></td>
<td width="47%"><?php echo $row->item = $this->typography->auto_typography($row->item);

How can I convert the data back in the controller to be able to use the table generate method? Using the "usual" php method creates a horrible table.

A: 

To utilise CodeIgniter's table class, here's an example of how you may use it:

//-- Table Initiation
$tmpl = array (
  'table_open'          => '<table border="0" cellpadding="0" cellspacing="0">',
  'heading_row_start'   => '<tr class="heading">',
  'heading_row_end'     => '</tr>',
  'heading_cell_start'  => '<th>',
  'heading_cell_end'    => '</th>',
  'row_start'           => '<tr>',
  'row_end'             => '</tr>',
  'cell_start'          => '<td>',
  'cell_end'            => '</td>',
  'row_alt_start'       => '<tr class="alt">',
  'row_alt_end'         => '</tr>',
  'cell_alt_start'      => '<td>',
  'cell_alt_end'        => '</td>',
  'table_close'         => '</table>'
);
$this->table->set_template($tmpl);   
$this->table->set_caption("TABLE TITLE");

//-- Header Row
$this->table->set_heading('ID', 'Date', 'Title', 'Item');

//-- Content Rows
foreach($rows as $index => $row)
{
  $this->table->add_row(
    anchor("work/fill_form/$row->id", $row->id),
    $row->date,
    $row->title,
    $this->typography->auto_typography($row->item)
  );
}

//-- Display Table
$table = $this->table->generate();
echo $table;
rockacola
Thank you very much. I use the table class when tabling raw data but did not know how to put the links in thus changing the raw database data.Again, thank you very much
Brad
I did however change "foreach($rows as $index => $row)"to "foreach($result as $row)" as $resut was what i was using in the controller. Everything works excellent
Brad
You're welcome.Also, I have table codes (above) in Views rather than in Controllers because they are part of presentation.
rockacola
I put everything except the foreach section and the table generate in the controller. Works like a charm. I wondered if the add row wasn't the way to go but didnt really understand it
Brad
CodeIgniter User Guide (http://codeigniter.com/user_guide/) is fairly easy to use and noob-proof. Its Table of Content covers pretty much all usage in this framework, takes you 2~3 hours to run through every single topic and you'll know CodeIgniter inside out!
rockacola
A: 

Thank you very very much dear. I was struggling to do that for last few days. This helped me a lot. You can go to heaven. Thanx alot

Best Regards, MaX

MaX