Welcome Guest, Not a member yet? Register   Sign In
Help converting a function to CI
#2

[eluser]JaRoLLz[/eluser]
CI use MVC. So, you should separate your app into model, view, controller.

As for that function, you should split it into M,V,C.

[model] In job_model, create a function to get the `cats` data then return an array that contains its data
[view] in a view file (e.g. subcat.php), create a layout to present the data
[controller] in your controller (e.g. Cats) create a function that will get data using the model then passing it to view file to show.

crude example:

MODEL: job_model.php
Code:
class Job_model extends Model {
  function Job_model() {
    parent::Model();
  }

  function getCats() {
    $query = $this->db->query('
      SELECT *
      FROM `cats`
    ');
    if ($query->num_rows() > 0) {
      return $query->result_array();
    } else {
      return array();
    }
  }
}

VIEW: subcat.php
Code:
<html>
<body>
<h1>Cats</h1>
&lt;? foreach ($catdata as $r): ?&gt;
  ID: &lt;?php echo $r['id'] ?&gt;
  Name: &lt;?php echo $r['name'] ?&gt;
  Desc: &lt;?php echo $r['desc'] ?&gt;
  <br>
&lt;? endforeach; ?&gt;
&lt;/body&gt;
&lt;/html&gt;

CONTROLLER: cats.php
Code:
class Cats extends Controller {
  function Cats() {
    parent::Controller();
  }

  function populate() {
    $this->load->model('job_model');
    $data['catdata'] = $this->job_model->getCats();
    $this-&gt;load->view('subcat',$data);
  }
}

p.s.: Adjust the code as needed. CMIIW!


Messages In This Thread
Help converting a function to CI - by El Forum - 02-01-2009, 03:55 PM
Help converting a function to CI - by El Forum - 02-01-2009, 06:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB