CodeIgniter Forums
Confusion with MVC in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Confusion with MVC in CI (/thread-31363.html)

Pages: 1 2


Confusion with MVC in CI - El Forum - 06-16-2010

[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!


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]SPeed_FANat1c[/eluser]
Your model function getCountries doesn't return any result. You could do something like this:

Code:
function getCountries()
  {
    $this->load->database();

    $this->db->select('country_id','country');
    $query = $this->db->get('countries');
    return $query;
  }

And in your view file you should make echo in foreach loop because it is an array.


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]ChaosKnight[/eluser]
Thanks, I changed my code to this now:
Model:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Countries_model extends Model
  {
    function Countries_model()
    {
      parent::Model();
    }
    function getCountries()
    {
      $this->load->database();
    
      $this->db->select('country_id','country');
      $query = $this->db->get('countries');
      return $query;
    }
  }
/* End of Countries.php model */
Controller:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

  class Countries extends Controller
  {
    function Countries()
    {
      parent::Controller();
      $this->load->helper('html');
      $this->load->helper('url');
      $this->load->model('countries_model');
    }
    function index()
    {
      $query = $this->countries_model->getCountries();

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

      $this->load->view('header',$data);
      $this->load->view('nav');
      $this->load->view('sidebar');
      $this->load->view('countries',$query);
      $this->load->view('footer');
    }
  }
/* End of Countries.php controller */
View:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
?&gt;
<div id="content">
  &lt;?php
  foreach ($query->result() as $row)
  {
    echo $row->country;
  }
  ?&gt;

</div>
&lt;?php
/* End of Countries.php view */
I'm not sure if this is correct, but it still just returns a blank screen... I'm a bit disappointed in myself, because since I started with CI yesterday, only the few tutorials in the user guide that I tried have worked... None of my own code has worked so far :-S


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]n0xie[/eluser]
Do you get a blank screen with no output at all, or do you get some output?

If you get no output at all it could mean you have error reporting disabled. In this case whenever an error occurs PHP will output a blank page.

In your index.php change the error_reporting to:
Code:
error_reporting(E_ALL | E_STRICT);



Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]SPeed_FANat1c[/eluser]
In your controler do this:
Code:
$results = $query->result();
$data['res'] = $results;

then load your countries view this way:

Code:
$this->load->view('countries',$data);

in view file do this:

Code:
foreach ($res as $row)
  {
    echo $row->country;
  }

There may be some errors in my code, I have not tested it.


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]ChaosKnight[/eluser]
I checked, and it is enabled and set to E_ALL, but I added E_STRICT to it now, and it still displays nothing... Even the HTML source code that's returned to the browser is empty, it only returns this:
Code:
&lt;html&gt;
  &lt;head&gt;&lt;/head>
  &lt;body&gt;&lt;/body>
&lt;html&gt;
The countries page isn't the only page on the website, there is a home, about, car rental, maps, bookings and contact also.
I tried to set up a contact form (that part worked), but as soon as I tried to implement the email library, it also returns nothing after a long load when I click on the submit button. The countries page is the only one that has a model currently... I don't know if it's my host's PHP and MySQL versions... They told me that they have PHP v4.3.2, or only PHP v4.3, and their MySQL is very outdated (v3.52), but they are the only host that is currently available to me... The boss wants to be hosted at that specific host... I tried to ask them to upgrade, but they said that upgrading isn't currently an option...


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]ChaosKnight[/eluser]
Thanks SPeed_FANat1c! There is finally some light! It displays my layout correctly again, but now where I expected the list to be, it displays an error message for each country instead of the country name...
Error message:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: country

Filename: views/countries.php

Line Number: 4
Here is the view code once more:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
?&gt;
<div id="content">
  &lt;?php
  foreach ($res as $row)
  {
    echo $row->country;
  }
  ?&gt;

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



Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]SPeed_FANat1c[/eluser]
'country' must be the column name in database. Maybe there is the problem?

Edit:
oh, now I see there is such from this

$this->db->select('country_id','country');

try print_r function to see how you array looks like


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]ChaosKnight[/eluser]
Mmm.. It outputs:
Code:
stdClass Object
(
    [country_id] => zambia
)
It should also output:
Code:
[country] => Zambia
And for some reason it doesn't even mention all the other countries, there should be 11.

Edit:
Sorry, I didn't put the print_r inside the foreach. It displays all the other now, but the 'country' element in the array is still missing...


Confusion with MVC in CI - El Forum - 06-16-2010

[eluser]ChaosKnight[/eluser]
Another thing that still bothers me is the behavior after I add another function.
In my controller after the index() function, I tried to add another function called view().
But even if I just add it empty,
Code:
function view()
{

}
The screen appears blank if I reload... Why is this? It isn't as if it gets loaded? I haven't even called it?