Welcome Guest, Not a member yet? Register   Sign In
make table with sql data and php
#1

Hello,

I am tasked to display data from sql db in a table on the web with following information,

          2014     2015     2016     2017
ABC     415      232        45        445
DEF     315     815         515
GHI     243     256         221       455

I have to get this from a table with select query and display it as above using Php.

Any help would be appreciated.
Thanks.
Reply
#2

@ebaad,

What tables(and fields) need to be used to create the table listed above? We need more information...
Reply
#3

HI,

I assume your table layout is something like this:
- name
- 2014
- 2015
- 2016
- 2017

So the code could look like this:



PHP Code:
<?php
class Testmodel CI_Model {


 public function 
__construct()
 
   {
 
       parent::__construct();
 
    }

public function 
get(){
$q $this->db->get('your_table')->result();

 if (
count($q) > 0)
 {
 
   return $q;
 }
 else
 {
 
   return false;
 }
}


Controller:

PHP Code:
<?php
class Test extends CI_Controller {

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

public function 
index(){

 
$this->load->model('testmodel');

 
$data['results'] = $this->testmodel->get();

 
$this->load->view('test'$data);

 }


Then you can create a view called test.php in your view folder with the below:

PHP Code:
<!DOCTYPE html>
<
html lang="en">
 
   <head></head>
 
   <body>
 <
table>
 
   <thead>
 <
tr>
 
   <th>Name</th>
 
   <th>2014</th>
 
   <th>2015</th>
 
   <th>2016</th>
 
   <th>2017</th>
 </
tr>
 
   </thead>
 
   <tbody>
 <?
php if (isset($results) && FALSE !== $results): ?>
    <?php foreach ($results as $row): ?>
 <tr>
    <td><?= $row->name ?>/td>
    <td><?= $row->{'2014'?>/td>
    <td><?= $row->{'2015'?>/td>
    <td><?= $row->{'2016'?>/td>
    <td><?= $row->{'2017'?>/td>
 </tr>
    <?php endforeach; ?>
 <?php endif; ?>
    </tbody>
 </table>
    </body>
</html> 

ps: i would not name my columns with numbers
Reply
#4

Thanks much , this really helped.

Now data is showing as expected.

Smile
Reply
#5

Or just use this.

CodeIgniter Users Guide - HTML Table Class
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB