Use Table Library in CodeIgniter Framework


Create new database named learn_codeigniter_with_real_apps. This database have 1 table: Product table.

--
-- Table structure for table `product`
--

CREATE TABLE `product` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(250) NOT NULL,
  `price` double NOT NULL,
  `quantity` int(11) NOT NULL,
  `description` text NOT NULL,
  `status` tinyint(1) NOT NULL,
  `photo` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `status`, `photo) VALUES('name 1', 2, 5, 'Description 1', 0, 'thumb1.gif');
INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `status`, `photo) VALUES('name 2', 7, 3, 'Description 2', 1, 'thumb2.gif');
INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `status`, `photo) VALUES('name 3', 2, 7, 'Description 3', 1, 'thumb3.gif');

Product Table

Data of Product Table




Create new folder named assets in root project. In this folder, create new folders as below:

Create new folder named images in assets folder. Copy images need to use in project to images folder.

In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:

$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';

In CodeIgniter Project, Open autoload.php file in config folder. Add database and table libraries to libraries config as below:

$autoload['libraries'] = array('database', 'table');

Open database.php file in config folder. Add values as below connect to database:

$db['default'] = array(
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '123456',
	'database' => 'learn_codeigniter_with_real_apps'
);

Create new PHP file named product_model.php in models folder as below:

<?php
if (! defined('BASEPATH'))
    exit('No direct script access allowed');

class Product_model extends CI_Model
{

    function findAll()
    {
        return $this->db->get('product')->result();
    }

}




In CodeIgniter Project, Open autoload.php file in config folder and set value for model config as below:

$autoload['model'] = array('product_model' => 'productModel');

Create new PHP file named product.php in controllers folder as below:

<?php defined('BASEPATH') or exit('No direct script access allowed');

class Product extends CI_Controller
{

    public function index()
    {
        $data['products'] = $this->productModel->findAll();
        $this->load->view('product/index', $data);
    }

}

Open routes.php file in config folder. Set value for default_controller as below:

$route['default_controller'] = 'product';

Create new folder named product in views folder. In this folder, create new PHP file named index.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
	<head>
		<title>Product List</title>
	</head>
	<body>
		<h3>Product List</h3>
		<?php
    		$tmpl = array (
    		  'table_open' => '<table border="1" cellpadding="2" cellspacing="2">'
            );
    		$this->table->set_template($tmpl);
    		$this->table->set_heading('ID', 'Name', 'Photo', 'Status', 'Description', 'Price', 'Quantity', 'Sub Total');
    		foreach ($products as $product) {
    		    $this->table->add_row(
    		        $product->id,
    		        $product->name,
    		        '<img src="'.base_url().'assets/images/'.$product->photo.'" height="50">',
    		        $product->status ? 'active' : 'inactive',
    		        $product->description,
    		        $product->price,
    		        $product->quantity,
    		        $product->price * $product->quantity
                );
    		}
    		echo $this->table->generate();
    	?>
	</body>
</html>




Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/product/index

Output

I recommend you refer to the books below to learn more about the knowledge in this article: