Hello,

I am trying to create a table in CI:

controllers/ctable.php

<?php

class Table extends CI_Controller {

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

    $data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')
             );

    $this->load->view('tablefile');
}

?>

views/vtable.php

<?php


echo $this->table->generate($data); 


?>

routes.php

$route['vtable'] = 'ctable';

Result:

Welcome to CodeIgniter!

The page you are looking at is being generated dynamically by CodeIgniter.

If you would like to edit this page you'll find it located at:
application/views/welcome_message.php

The corresponding controller for this page is found at:
application/controllers/welcome.php

If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.

I expect table . Why only a welcome message?

Recommended Answers

All 14 Replies

Few things;

First your controller file name and class name need to be the same. So either change the class to Ctabel (recommended) or change the controller file name to table.php

Second issue is your controller class needs to be split into functions, the functions forms part of your url (domain.com/index.php/controller/function), if you just want it the default than you use index.

Third issue is you need to pass your array to the load view function as a second parameter.

<?php

class Ctable extends CI_Controller {

    public function index()
    {

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

        $data = array(
                 array('Name', 'Color', 'Size'),
                 array('Fred', 'Blue', 'Small'),
                 array('Mary', 'Red', 'Large'),
                 array('John', 'Green', 'Medium')
                 );

        $this->load->view('tablefile',$data);
    }
}

?>

As for getting the welcome message, you're probably only loading the main welcome url (www.yourdomain.com/index.php) you have to add your ctable controller to the URL like so (assuming you haven't done a mod rewrite to remove the index.php): www.yourdomain.com/index.php/ctable/

If you haven't done so already, I'd suggest you read through and do the examples in the CI tutorial in the user manual: http://ellislab.com/codeigniter/user-guide/tutorial/

Just noticed one other thing, your load view calls tablefile, but your view is actually called vtable.php these need to be the same. The extra line in your routes file is not needed, that is only for re-routing whihc you're not doing here.

ok this is what I have right now:

controllers/ctable.php

<?php

class ctable extends CI_Controller {

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

    $data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')
             );

    $this->load->view('vtable', $data);
}

?>

views/vtable.php

<?php


echo $this->table->generate($data); 



?>

routes.php

$route['default_controller'] = "welcome";
$route['404_override'] = '';

$route['vtable'] = 'ctable';

I wonder what should I type in the url to make it works. This is what I try: http://localhost/TableCI/ and http://localhost/TableCI/ctable

The first open the welcome message whereas the second Object not found!

You missed the class:

<?php

class ctable extends CI_Controller {

    public function index()
    {

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

        $data = array(
                      array('Name', 'Color', 'Size'),
                      array('Fred', 'Blue', 'Small'),
                      array('Mary', 'Red', 'Large'),
                      array('John', 'Green', 'Medium')
                );

        $this->load->view('vtable', $data);
    }
}

?>

I already add the class and the same still appears: the welcome message.

Sorry that should have said you missed the function ( public function index() ) and in response to your other question you should be going to http://localhost/TableCI/ctable

I try this: http://localhost/TableCI/index.php/ctable

This is a new table
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/vtable.php

Line Number: 8
Undefined table data

views/vtable.php

<html>

<p> This is a new table </p>

<?php

    echo $this->table->generate($data);

?>


</html>

what else is lacking?

You should use basic examples first and try finding relations to your requirement(s).
Here is what we have in basic example:

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

$data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')   
             );

echo $this->table->generate($data);

Notice that all of that is in controller. If you want to relate it to view file, you should do in controller something like:

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

$data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')   
             );

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

$this->load->view('vtable', $view_data);

And in your vtable.php file should be something like:

<html>
<p> This is a new table </p>
<?php
    print_r ($data); // this is element data from $view_data array or $view_data['data']
?>
</html>

Disclaimer: not tested.

Sorry I should have caught that before. Your data array gets passed to the view and is assigned varible names based on the array's key. You need to add add a key and change your view:

Controller:

<?php

class ctable extends CI_Controller {

    public function index()
    {

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

        $data['table_data'] = array(
                                     array('Name', 'Color', 'Size'),
                                     array('Fred', 'Blue', 'Small'),
                                     array('Mary', 'Red', 'Large'),
                                     array('John', 'Green', 'Medium')
                                   );

        $this->load->view('vtable', $data);
    }
}

?>

View:

<html>

<p> This is a new table </p>

<?php

    echo $this->table->generate($table_data);

?>


</html>

ok thanks. Now, whatif I want to color the table header with gray color, how to do so? and have a straight line in between each row.

I also trying to pass the css link to the view but it shows error, how to code it correctly?

controllers/ctable.php

<?php

    class ctable extends CI_Controller {

    public function index()
        {

        $this->load->helper('url');     

        $this->data['assets'] = array('tablestyle' => base_url().'assets/css/table.css');

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

        $tmpl = array ( 'table_open'  => '<table border="0" cellpadding="4" cellspacing="1" class="mytable">' );

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

        $data['table_data'] = array(

            array('#', 'Product Category', 'Post', 'Position'),
            array('1', 'Connection', 'Post', '1'),
            array('2', 'Collaboration', 'Post', '2'),
            array('3', 'Solution', 'Post', '3')

        );

    $this->load->view('vtable', $data);
    $this->load->view('vtable', $this->$data['assets']);
    }

 }

 ?>

This line shows error:

$this->load->view('vtable', $this->$data['assets']);

since I am trying to pass two values at the same time: the table and the table css link.

You need to load it into head of web page.
Delete line 28 it's incorrect.
All you need to do is to make rule in your css file for table.mytable as you have already mytable class in your table specified.

table.myclass tr {}
table.myclass tr td {}
table.myclass tr td:first {}
table.myclass tr td:last {}

And so on.

As Tpojka stated you don't need line 28, you only send the info to your view once. When you pass the data array to the view it creates variables for every array key.

So for example when you set the following in your controller:

$data['css'] = "css/table.css";
$data['table_data'] = "My table array";
$data['welcome_msg'] = "My new table.";

This all gets passed to the view with the one call
$this->load->view('vtable', $data);

Now in your view the keys become your variables like so:

<html>
<head>
<link rel="stylesheet" type="text/css" href="<?= $css; ?>">
</head>

<body>

<?php

  echo $welcome_msg . "<br /><br />";

  $this->table->generate($table_data);

?>

</body>

</html>

controllers/ctable.php

<?php

    class ctable extends CI_Controller {

    public function index()
        {

        $this->load->helper('url');     

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

        $tmpl = array ( 'table_open'  => '<table border="0" cellpadding="4" cellspacing="1" class="mytable">' );

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

        $data['tablestyle'] = base_url().'assets/css/table.css';

        $data['table_data'] = array(

            array('#', 'Product Category', 'Post', 'Position'),
            array('1', 'Connection', 'Post', '1'),
            array('<hr>','<hr>','<hr>','<hr>'),
            array('2', 'Collaboration', 'Post', '2'),
            array('3', 'Solution', 'Post', '3')

        );

    $this->load->view('vtable', $data);
    }

 }

 ?>

How to create a straight line in between row?

This does not work:

array('<hr>','<hr>','<hr>','<hr>'),

Any other alternative ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.