Welcome Guest, Not a member yet? Register   Sign In
How to call function in viewer
#11

[eluser]MaxEisley[/eluser]
toopay: Yea my database is badly designed due to this i have to do it in viewer.
#12

[eluser]cryogenix[/eluser]
pls don't! otherwise don't use CI instead... if you really designed your models that badly, then take the time to fix it. you'll have easier times in the long run.

sample model:
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  class Demo_model extends CI_Model
  {
    function __construct()
    {
      parent::__construct();
      $this->load->database();
      $this->table = 'tbl_something';
    }

    function get_something($id)
    {
      $query = $this->db->get_where($this->table, array('id' => $id));
      return $query;
    }
  }
/* End of file demo_model.php */
/* Location: ./application/models/demo_model.php */

sample controller:
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  class Something extends CI_Controller
  {
    function __construct()
    {
      parent::__construct();
      $this->load->model('demo_model', 'demo');
    }

    function index()
    {
      $result = $this->demo->get_something(1);
      $orders = $result->query();

      foreach($orders as $order)
      {
        //do something with the orders and store them to some $processed_result variable
      }

      $data['something'] = $processed_results;
      $this->load->('some_view', $data);
    }
  }
/* End of file something.php */
/* Location: ./application/controllers/something.php */

sample view:
Code:
<div>
  <p>&lt;?php echo $something->column1; ?&gt;</p>
  <p>&lt;?php echo $something->column2; ?&gt;</p>
</div>
#13

[eluser]web-johnny[/eluser]
I agree with noctrum . Don't use this foreach in your view. You can easily use a model instead . Even if your database is a disaster this will organize the chaos and you will not have unexpected errors (as this). Since I worked with models, I rarely have problems with database and views. The
Code:
$ci = &get;_instance
helps me to use models in the views ;-)




Theme © iAndrew 2016 - Forum software by © MyBB