Welcome Guest, Not a member yet? Register   Sign In
Best way of listing a table
#1

[eluser]cb951303[/eluser]
Hello everyone,

I have a controller with an index function that basically loads couple of views (header, footer). I also should output a list of db entries between header and footer views. For that I have a listResults() function as this controller's member. It basically queries the database and returns a html table structure.

so here is some code (I would like to post the actual code but its really long)
Code:
<?php
    class Mycontroller extends Controller
    {
        private $queryString = 'DEFAULT MYSQL QUERY STRING GOES HERE';
  
        function index()
        {
            $layout['header'] = $this->load->view('header', '', true);
            $layout['footer'] = $this->load->view('footer', '', true);
            $layout['list'] = $this->listResults();
            
            $this->load->view('main', $layout);
        }
        
        function listResults()
        {
            /* Query database with $this->queryString
             * and then create an html table string
             * and return it */
        }
    }
?>

My questions are: are there any better ways of doing it? creating table in the controller is really MVC way of doing things? since the table's styling and all that are contained in the controller file...

Thanks
#2

[eluser]stuffradio[/eluser]
No you need to take a look at Models.

In a model it is really easy to get results from a Database. For what you are doing this is what you do.

Controller:
Code:
<?php

class Mycontroller extends Controller
{

  function Mycontroller()
  {
    parent::Controller();
    $this->load->library('database');
    $this->load->model('Mymodel');
  }

  function index()
  {
    $layout['list'] = $this->Mymodel->listResults();
    $this->load->view('header');
    $this->load->view('main', $layout);
    $this->load->view('footer');
  }

}

Model:
Code:
<?php

class Mymodel extends Model
{

  function Mymodel()
  {
    parent::Model();
  }

  function listResults()
  {
    $query = $this->db->get('table');
    return $query->result();
  }

}


View:

Code:
<h1>Results</h1>
&lt;?php foreach($list as $result): ?&gt;
&lt;?php echo $result->field . '<br />'; ?&gt;

I hope this helps you better understand what to do Smile
#3

[eluser]cb951303[/eluser]
[quote author="stuffradio" date="1220842397"]No you need to take a look at Models.

In a model it is really easy to get results from a Database. For what you are doing this is what you do.

Controller:
Code:
&lt;?php

class Mycontroller extends Controller
{

  function Mycontroller()
  {
    parent::Controller();
    $this->load->library('database');
    $this->load->model('Mymodel');
  }

  function index()
  {
    $layout['list'] = $this->Mymodel->listResults();
    $this->load->view('header');
    $this->load->view('main', $layout);
    $this->load->view('footer');
  }

}

Model:
Code:
&lt;?php

class Mymodel extends Model
{

  function Mymodel()
  {
    parent::Model();
  }

  function listResults()
  {
    $query = $this->db->get('table');
    return $query->result();
  }

}


View:

Code:
<h1>Results</h1>
&lt;?php foreach($list as $result): ?&gt;
&lt;?php echo $result->field . '<br />'; ?&gt;

I hope this helps you better understand what to do Smile[/quote]
Actualy I'm fully aware of models and MVC design criteria. but what I 'm asking has nothing to do with models. my listResults function uses CI HTML table class to create a table. it ofcourse gets its content via a model but as I said earlier it has nothing to do with the original question. thanks anyway




Theme © iAndrew 2016 - Forum software by © MyBB