CodeIgniter Forums
Trying to use MVC - 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: Trying to use MVC (/thread-13029.html)



Trying to use MVC - El Forum - 11-07-2008

[eluser]d4v0[/eluser]
Hi everybody!
I've been trying to work with with the MVC using CI, but i have some problems! :red:
I'm just a beginner! ... But I want to learn more than CI! Tongue

Well, this is my model: 'mimodelo.php'

Code:
<?PHP
class Mimodelo extends Model
{
    function Mimodelo()
    {
        parent::Model();
    }
    function obtenertodo()
    {
        $datos = array();
        $consulta = $this->db->get("ALL_M_RUB");
        $datos = $consulta->row_array();
        return $datos;
    }
}
?>

As you see, my table's name is ALL_M_RUB.

This is my controller: 'micontrol.php' :cheese:

Code:
<?PHP
class Micontrol extends Controller
{
    function Micontrol()
    {
        parent::Controller();
        $this->load->database();
    }
    
    function index()
    {
        $this->load->model('Mimodelo','',TRUE);
        $data['contenido'] = $this->Mimodelo->obtenertodo();
        $this->load->view('mivista',$data);
    }
}
?>

And finally, this is my view ... but it's incomplete... 'mivista.php' :roll:

Code:
<?PHP
foreach($contenido as $elemento)
{
    //here goes my results
}
?>

That's my poblem..! :-S ... I don't know how to catch the results and show them all!

The fields of my table are 3: 'RUB_ORD' , 'RUB_DES' and 'RUB_STATUS'.

What is missing... or what am i doing bad??

Help! ... =(

Thanks!


Trying to use MVC - El Forum - 11-07-2008

[eluser]John_Betong[/eluser]
 
Quote:
... I don't know how to catch the results and show them all!
 
 
Take a look at your results by using this code:
Code:
// in your Mimodelo
   echo '<pre>';
      print_r( $datos );
   echo '</pre>';

   // in your Controller: micontrol.php
   echo '<pre>';
      print_r( $data['contenido'] );
   echo '</pre>';

   // in your view
   echo '<pre>';
      print_r( $contenido );
   echo '</pre>';
&nbsp;
Once you see what the results of print_r(...) then you will be able to format your array data.
&nbsp;
&nbsp;
&nbsp;


Trying to use MVC - El Forum - 11-07-2008

[eluser]d4v0[/eluser]
hey man!... do u have an e-mail... because... it doesn't work... i need an example where a controller get data from a model... and then show it in a view.

But this data is a query with more than one column... you got it??

I've been searching examples... but... i can't find what i want...! =(


Trying to use MVC - El Forum - 11-08-2008

[eluser]John_Betong[/eluser]
[quote author="d4v0" date="1226143716"]hey man!... do u have an e-mail... because... it doesn't work... i need an example where a controller get data from a model... and then show it in a view.

But this data is a query with more than one column... you got it??

I've been searching examples... but... i can't find what i want...! =([/quote]
&nbsp;
Here you go I have just uploaded a new page to my website showing the last 42 jokes:
&nbsp;
My Model Library
Code:
//=============================================
  function m_last_42() {
    $sql  = 'SELECT id, title FROM jokes ORDER BY id DESC LIMIT 0,22';
    $query = $this->db->query($sql);
        
    $data['thelot'] = array();
    if ($query->num_rows()) {
      foreach($query->result() as $row):
        $tmp = anchor_popup( 'joke/popup/' .$row->id,
                              $row->title,
                              $this->atts);
        $data['thelot'][]    .= $tmp;
      endforeach;
    }//endif
    return $data['thelot'];
  }//endfunc
&nbsp;
&nbsp;
My Controller
Code:
//=============================================
  function last_42() {  
    $data['joke_title'] = 'the Last 42';
    $data['last_42']    = $this->m_lib->m_last_42();
    $data['thelot'][]   = $this->load->view('_box_42', $data, TRUE);

    $this->j_view($data); // calls a common view with header, menus left & right and footer, etc  
  }//endfunc
&nbsp;
&nbsp;
My View
Code:
&lt;html&gt;
   ...
   ...
&lt;body&gt;

   &lt;?php // this is the only bit you are concernced with  
                        // works for lists, Pictures only Searches
      if (is_array($thelot)) {
         foreach($thelot as $row):
            echo '<br />' .$row;
         endforeach;    
      }//endif
   ?&gt;

&lt;/body&gt;
&lt;/html&gt;
&nbsp;
&nbsp;
See it in action here: http://johns-jokes.com/joke/last_42
&nbsp;
&nbsp;


Trying to use MVC - El Forum - 11-08-2008

[eluser]d4v0[/eluser]
Hey man!... Thanks a lot! ... I know how to do it now!...


Trying to use MVC - El Forum - 11-08-2008

[eluser]John_Betong[/eluser]
[quote author="d4v0" date="1226213399"]Hey man!... Thanks a lot! ... I know how to do it now!...[/quote]
&nbsp;
Your welcome, I am pleased to be able to help and looking forward to seeing your new site.
&nbsp;
&nbsp;
&nbsp;


Trying to use MVC - El Forum - 12-10-2008

[eluser]Unknown[/eluser]
I am a good procedural dude, but new to oop. This was a better example than the user guide. Thanks,

The Maestro


Trying to use MVC - El Forum - 12-11-2008

[eluser]John_Betong[/eluser]
[quote author="Maestrog" date="1228940367"]I am a good procedural dude, but new to oop. This was a better example than the user guide. Thanks,

The Maestro[/quote]
&nbsp;
I am pleased I was able to help.
&nbsp;
I find the user guide very good but a bit too cryptic for me. I would like to see more examples since a good working example is far more useful in trying to understand a new topic.
&nbsp;
Vague memories this topic has been previously raised and answered at length.
&nbsp;
&nbsp;