Welcome Guest, Not a member yet? Register   Sign In
Add extra columns to database generated table
#1

[eluser]Maxat[/eluser]
Hi,

I am new to Codeigniter and I seek your help in adding extra columns to the table generated from database.
The main thing is I need to add edit and delete columns to each generated row. Can you please help me?

Below is my code for controller
Code:
public function index() {

$config['base_url']= site_url('admin/fieldlist/index');
$config['total_rows']= $this->db->get('field')->num_rows();
$config['per_page']=5;
$config['num_links']=3;
$config['full_tag_open']= '<div id="pagination">';
$config['full_tag_close']='</div>';
$config['uri_segment'] = 4;

$this->pagination->initialize($config);

$data['records'] = $this->db->get('field',$config['per_page'],$this->uri->segment(1));

$this->load->view('admin/fieldlist_view',$data);
}




And View

Code:
<h3>Fields</h3>
  

  <div id="holder">
&lt;?php $this->table->set_heading('Field ID', 'Field Name');
   echo $this->table->generate($records);
   echo $this->pagination->create_links();
  ?&gt;
  
  </div>


Don't have a Module
#2

[eluser]Tominator[/eluser]
I'm not sure, but I think you can do something like this:

Code:
...

$field = $this->db->get('field',$config['per_page'],$this->uri->segment(1)); // just saves that

foreach ( $field->result_array() as $row )
{
    $data['records'][] = $row + array('extra_column' => 1); // adds extra column
}

...

If this works, you can add as many columns as needed.

Tom.
#3

[eluser]Maxat[/eluser]
Thanks, but it doesn't work.
#4

[eluser]CroNiX[/eluser]
Code:
//in controller
$query = $this->db->get('field',$config['per_page'],$this->uri->segment(1))->result_array();  //original result array
foreach ($query as $key => $row )
{
    //add new columns with a value
    $query[$key]['extra_column'] = 'new_value1';
    $query[$key]['extra_column2'] = 'new_value2';
    //...
    // Note: the original data for this row is in $row if you need data from it
}
$data['records'] = $query;  //store the reformatted array to send to view

//in view
echo $this->table->generate($records);
#5

[eluser]marielBarrios[/eluser]
Interesting post, will serve me well for my project of <a href="http://juegosvestirgratis.com">juegos de vestir</a>, thank you very much.
#6

[eluser]Maxat[/eluser]
Thanks CroNiX,

It does work. Can you please also help with sending id of the field to the controller from the column.
For example: One of the extra columns will be with delete value. on click it sends field id to controller . Based on id, delete method will delete from database.

Code:
$query[$key]['extra_column'] = anchor(site_url('test1t'), 'Delete');

#7

[eluser]CroNiX[/eluser]
Assuming the id is in the current row and its called 'id'...
Code:
$query[$key]['extra_column'] = anchor(site_url('test1t/' . $row['id']), 'Delete');
#8

[eluser]Maxat[/eluser]
Thanks a lot.




Theme © iAndrew 2016 - Forum software by © MyBB