Welcome Guest, Not a member yet? Register   Sign In
Clean way to work with views
#2

[eluser]Bart v B[/eluser]
I don't understand your problem.

Normaly it would look like this:

Youre Controller:
Code:
<?php

class Test extends Controller
{
   function __construct()
   {
     parent::Controller();
     $this->load->model('some_model');
   }

   function index()
   {
      $data['everything'] = $this->some_model->GetItems();
      $data['header'] = 'Hello CodeIgniter!';
      $this->load->view('test_view', $data);
   }
}

Youre model should look like this:

Code:
<?php

class Some_model extends Model
{
   function GetItems()
   {
     $query = $this->db->get('mytable');

     if($query->num_rows() > 0)
     {
      foreach ($query->result() as $row)
      {
        $data[] = $row;
      }
       return $data;    
    }
    
   }
}

Your view:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $header; ?&gt;&lt;/title&gt;


&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?php echo $header;?&gt;</h1>

&lt;?php  if(!empty($everything )) :
       foreach($everything as $item): ?&gt;

   <p>&lt;?php echo $item->some_row; ?&gt;</p>

&lt;?php
      endforeach;
      endif;
?&gt;
&lt;/body&gt;
&lt;/html&gt;

As you see you don't need to have a foreachloop when its an array from the controller.
$data is just an array() to have some infomation of where it's comming from.
Cleander as this is not possible. So i think your problem is something else.
Maybe you can give more information aboute what you can not do in your view.


Messages In This Thread
Clean way to work with views - by El Forum - 10-16-2010, 08:51 PM
Clean way to work with views - by El Forum - 10-17-2010, 01:41 AM
Clean way to work with views - by El Forum - 10-17-2010, 07:35 AM
Clean way to work with views - by El Forum - 10-17-2010, 07:39 AM
Clean way to work with views - by El Forum - 10-17-2010, 10:26 AM
Clean way to work with views - by El Forum - 10-18-2010, 10:46 AM
Clean way to work with views - by El Forum - 10-18-2010, 11:21 AM
Clean way to work with views - by El Forum - 10-18-2010, 05:42 PM
Clean way to work with views - by El Forum - 10-22-2010, 09:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB