• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Adding Extra Columns To The HTML Table Library

#1
On my category controller I am using the codeigniter library table class

Every thing is working fine but I would like to know is for each row it need to have it's own

category edit button / link. And be able to like anchor('category/update/' . $category['category_id'])

I do not know how to modify the table for that

Question How to modify the $this->table->generate to be able to add a link for each category.

[Image: HTML_TABLE.png]

Model Function


PHP Code:
public function get_categories() {
    $this->db->select('*');
    $this->db->from($this->db->dbprefix 'category');
    $query $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return false;
    }


Controller


PHP Code:
<?php

class Category extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }
    
    public 
function index() {
        $this->dynamic->set_title('Category');

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'active' => '',
            'href' => anchor('admin/dashboard''Home')
        );

        $data['breadcrumbs'][] = array(
            'active' => 'class="active"',
            'href' => 'Category'
        );

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

        $template = array(
        'table_open'            => '<table class="table table-striped table-bordered">',

        'thead_open'            => '<thead>',
        'thead_close'           => '</thead>',

        'heading_row_start'     => '<tr>',
        'heading_row_end'       => '</tr>',
        'heading_cell_start'    => '<th>',
        'heading_cell_end'      => '</th>',

        'tbody_open'            => '<tbody>',
        'tbody_close'           => '</tbody>',

        'row_start'             => '<tr>',
        'row_end'               => '</tr>',
        'cell_start'            => '<td>',
        'cell_end'              => '</td>',

        'row_alt_start'         => '<tr>',
        'row_alt_end'           => '</tr>',
        'cell_alt_start'        => '<td>',
        'cell_alt_end'          => '</td>',

        'table_close'           => '</table>'
        );

        $this->table->set_heading(array('Category ID''Parent ID''Category Name''Category URL''Category Status''Category Date Added''Category Edit'));

        $this->table->set_template($template);

        $data['categories'] = $this->table->generate($this->get_categories());

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');
        $data['timeout'] = Modules::run('admin/common/timeout/index');
        $data['navbar'] = Modules::run('admin/common/navbar/index');

        $this->load->view('template/catalog/category_view'$data);

    }

    public function get_categories() {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix 'category');
        $query $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }



Update I think I have the model correct now


 

PHP Code:
public function get_categories() {
        
$this->db->select('*');
        
$this->db->from($this->db->dbprefix 'category');
        
$query $this->db->get();

        if (
$query->num_rows() > 0) {

            foreach (
$query->result_array() as $result) {
                
$data[] = array(
                    
'categor_id' => $result['category_id'],
                    
'parent_id' => $result['parent_category_id'],
                    
'name' => $result['name'],
                    
'url' => $result['url'],
                    
'status' => $result['status'],
                    
'date_added' => $result['date_added'],
                    
'category_edit' => anchor('admin/category/update/' $result['category_id'], 'Edit')
                );
            }
    
        } else {
            return 
false;
        }

        return 
$data;    
    } 


Attached Files
.php   Category.php (Size: 2.24 KB / Downloads: 48)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
Use the table template like below:

PHP Code:
$this->load->library('table');

$tmpl = array (
    
'table_open'            => '<table border="0" cellpadding="4" cellspacing="0">',

    
'thead_open'            => '<thead>',
    
'thead_close'            => '</thead>',

    
'heading_row_start'        => '<tr>',
    
'heading_row_end'        => '</tr>',
    
'heading_cell_start'        => '<th>',
    
'heading_cell_end'        => '</th>',

    
'tbody_open'            => '<tbody>',
    
'tbody_close'            => '</tbody>',

    
'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);


Example table manage method change to suit yourself.

/**
 * manage ()
 * --------------------------------------------------------------------
 *
 * Description:
 *
 * @param    string
 */
public function manage()
{
    
// Grab an array of users from the database
    
$data $this->users->users();

    
// Set table headings
    
$this->table->set_heading('Username''Email''Actions');

    foreach(
$data as $value => $key)
    {
        
// Build the table actions links
        
$actions anchor("admin/users/edit/".$key['id']."/""Edit") . 
                 
  anchor("admin/users/delete/".$key['id']."/""Delete");

        
// Add row to table
        
$this->table->add_row($key['username'], $key['email'], $actions);
    }

    
// Load the view
    
$this->load->view('users/manage');


Hope this helps you.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(09-17-2016, 03:09 AM)InsiteFX Wrote: Use the table template like below:

PHP Code:
$this->load->library('table');

$tmpl = array (
    
'table_open'            => '<table border="0" cellpadding="4" cellspacing="0">',

    
'thead_open'            => '<thead>',
    
'thead_close'            => '</thead>',

    
'heading_row_start'        => '<tr>',
    
'heading_row_end'        => '</tr>',
    
'heading_cell_start'        => '<th>',
    
'heading_cell_end'        => '</th>',

    
'tbody_open'            => '<tbody>',
    
'tbody_close'            => '</tbody>',

    
'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);


Example table manage method change to suit yourself.

/**
 * manage ()
 * --------------------------------------------------------------------
 *
 * Description:
 *
 * @param    string
 */
public function manage()
{
    
// Grab an array of users from the database
    
$data $this->users->users();

    
// Set table headings
    
$this->table->set_heading('Username''Email''Actions');

    foreach(
$data as $value => $key)
    {
        
// Build the table actions links
        
$actions anchor("admin/users/edit/".$key['id']."/""Edit") . 
                 
  anchor("admin/users/delete/".$key['id']."/""Delete");

        
// Add row to table
        
$this->table->add_row($key['username'], $key['email'], $actions);
    }

    
// Load the view
    
$this->load->view('users/manage');


Hope this helps you.

Thank you for that info

I have it working now


[Image: 2vNTqBnyna7g.png]




PHP Code:
<?php

class Category_model extends CI_Model {

    public function 
get_categories() {
        
$data = array();

        
$this->db->select('*');
 
       $this->db->from($this->db->dbprefix 'category');
 
       $this->db->where('parent_category_id''0');
        
$query $this->db->get();

        if (
$query->num_rows() > 0) {

            foreach (
$query->result_array() as $result) {

                
$data[] = array(
                    
'category_id' => $result['category_id'],
                    
'parent_category_id' => $result['parent_category_id'],
                    
'name' => $result['name'],
                    
'url' => $result['url'],
                    
'status' => $result['status'],
                    
'date_added' => $result['date_added'],
                    
'category_delete' => anchor('admin/category/delete/' $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
                    
'category_edit' => anchor('admin/category/update/' $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
                );

                
$this->db->select('*');
         
       $this->db->from($this->db->dbprefix 'category');
         
       $this->db->where('parent_category_id'$result['category_id']);
         
       $query $this->db->get();

                foreach (
$query->result_array() as $result) {
                    
$category_data $this->get_parent_category_name($result['parent_category_id']);

                    
$data[] = array(
                        
'category_id' => $result['category_id'],
                        
'parent_category_id' => $result['parent_category_id'],
                        
'name' => '<b>' $category_data->name '</b>' ' &gt; '$result['name'],
                        
'url' => $result['url'],
                        
'status' => $result['status'],
                        
'date_added' => $result['date_added'],
                        
'category_delete' => anchor('admin/category/delete/' $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
                        
'category_edit' => anchor('admin/category/update/' $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
                    );
                }
            }
    
        } else {

            return 
false;

        }

        return 
$data;    
    }

    public function 
get_parent_category_name($parent_category_id) {
        
$this->db->where('category_id'$parent_category_id);
        
$query $this->db->get($this->db->dbprefix 'category');

        if (
$query->num_rows() > 0) {
            return 
$query->row();
        } else {
            return 
false;
        }
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.