Welcome Guest, Not a member yet? Register   Sign In
array of data
#5

You can write all the code to do this yourself but you are learning a framework designed to make your life easier. So let's make your life easier.

Codeigniter (CI) has a nice library for creating html tables. (Documentation Here) The Table library can take the result of a database query and turn it into a nice HTML table with very few lines of code.

Here is how it might be done, and done the CI MVC way.

Controller file: application/controllers/Tabletest.php

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Tabletest extends CI_Controller
{
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load
          
->model('user_model')
 
         ->library('table');
 
       $this->table->auto_heading FALSE //Because we will set our own headings in the next line
 
       $this->table->set_heading('Name''Country''Designation');
 
   }

 
   public function index()
 
   {
 
       $db_result_object $this->user_model->get_users();

 
       $vars['table'] = $this->table->generate($db_result_object);
 
       $this->load->view('table_view'$vars);
 
   }



The constructor loads the Table library, the model, and sets a couple Table related items.

The index() method uses the model get a db result object that is used as an argument to table->generate(). That method then creates the table html. The html is then passed to the view file "table_view.php".

Model File: /application/models/User_model.php

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
User_model extends CI_Model
{
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->database();
 
   }

 
   public function get_users()
 
   {
 
       return $this->db->query('SELECT username, country, designation FROM tableName');
 
   }



Note that rather than making Query Builder (QB) calls  (e.g. select(), from(), where(), get()) the method query() is used instead.

Why not use QB? Query Builder is a great tool but is not always appropriate. QB makes calls to multiple methods to create SQL statement strings. In this case, QB would need to run a large amount of code just to construct the simple SQL statement "SELECT username, country, designation FROM tableName".  After creating that string QB literally calls the method (query()) shown above. We can type less code and execute less code by using query().

So, when you have a very simple db request you should consider bypassing QB and directly create the statement yourself.

We return the return of query() without calling any of the methods that generate query results (e.g. result(), result_array() ). All Table needs is an object that can call those result generating methods.

We pass the return from the model to the table with this line.
PHP Code:
$this->table->generate($db_result_object); 
The return of that call is sent to the view.

View file: application/views/table_view.php

PHP Code:
<!DOCTYPE html>
<
html>
 
 <head>
 <
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <
title>Table Test</title>
 
 </head>
 
 <body>
 <?
php echo $table?>
  </body>
</html> 

So, not much code and a nice html table on the screen. Any you didn't have to write a single foreach statement. Examining the source code for Table will show you how that is done and much more.

You can use CSS and the templating options of the Table class to make it look just the way you want.

Thank you for putting up with me in teacher mode.
Reply


Messages In This Thread
array of data - by anmol - 04-14-2017, 02:51 AM
RE: array of data - by neuron - 04-14-2017, 03:08 AM
RE: array of data - by anmol - 04-14-2017, 06:05 AM
RE: array of data - by natanfelles - 04-14-2017, 06:50 AM
RE: array of data - by dave friend - 04-14-2017, 08:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB