Welcome Guest, Not a member yet? Register   Sign In
Confusion with MVC in CI
#1

[eluser]ChaosKnight[/eluser]
I'm very new to CI, and I don't fully understand how it processes the whole MVC idea, I'm fairly familiar with the MVC as I used Rails a few months ago.

I know that database queries goes in the model, logic goes in the controller and display elements in the view, but I'm still struggling with how to pass them between one another.

I want to make a script that connects to a database and generates a list of each country and display it in the view.

The model seems simple enough (connect, retrieve), but I don't know where to process the rows and also how CI handles the Active Record... Here is my countries_model.php model file:
Code:
<?php
class Countries_model extends Model
{
  function Countries_model()
  {
    parent::Model();
  }
  function getCountries()
  {
    $this->load->database();

    $this->db->select('country_id','country');
    $this->db->get('countries');
  }
}
/* End of Countries.php model */

I figured that the processing of the returned object has to go in the controller, because it seems like logic.
Here is the countries.php controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Countries extends Controller
  {
    function Countries()
    {
      parent::Controller();
    }
    function index()
    {
      $this->load->helper('html');
      $this->load->helper('url');
      
      $this->load->model('countries_model');
      $query = $this->countries_model->getCountries();
      
      foreach ($query->result() as $row)
      {
        $data['countries'][] = $row->country;
      }

      $data['page_title'] = 'Countries';

      $this->load->view('header',$data);
      $this->load->view('nav');
      $this->load->view('sidebar');
      $this->load->view('countries',$data);
      $this->load->view('footer');
    }
  }
/* End of Countries.php controller */

And lastly, here is my view page, countries.php:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<div id="content">

  &lt;?php echo $countries[] ?&gt;

</div>
&lt;?php
/* End of Countries.php view */

But for some reason it displays nothing... As I said, I'm very new to CI, I know a fair amount of PHP, and I'm also very new to OOP.

If someone with a pair of eagle eyes can scout through this heap of newbie code and find a mistake, I'd be very grateful.

Thanks!


Messages In This Thread
Confusion with MVC in CI - by El Forum - 06-16-2010, 07:05 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 07:26 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 07:40 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 07:45 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 07:57 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 07:59 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 08:09 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 08:48 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 09:01 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 09:18 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 09:23 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 09:28 AM
Confusion with MVC in CI - by El Forum - 06-16-2010, 12:07 PM
Confusion with MVC in CI - by El Forum - 06-16-2010, 01:15 PM
Confusion with MVC in CI - by El Forum - 06-16-2010, 01:35 PM
Confusion with MVC in CI - by El Forum - 06-16-2010, 02:28 PM



Theme © iAndrew 2016 - Forum software by © MyBB