ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

MVC help for a newbie (very newbie)

March 15, 2010 3:57pm

Subscribe [2]
  • #1 / Mar 15, 2010 3:57pm

    Juan Velandia

    101 posts

    Hello Guys

    I’m starting to work with the MVC approach, but after some hours I’ve decided to ask for help, it’s a simple matter: I can’t get this example to work, could anyone take a look and give me some hints, thanks a lot. I’m trying to have a working example to understand the MVC architecture.

    I’m just trying to get a list of records (logo names) from a table database: cms_proveedor

    Controller:

    <?php
    class Logo_controller extends Controller {
    var $data;
    function Logo_controller(){
        parent::Controller();
        $this->load->model('logo_model');
    }
    function index(){
        $this->data['logo'] = $this->?????->show_logo_all();
        $this->load->view('logo_view', $this->data);
    }
    }
    ?>

    Model:

    <?
    
    class Logo_model extends Model {
    function Logo_model(){
    parent::Model();
    }
    
    function show_logo_all(){
        $this->db->select('logo');
        $query = $this->db->get('cms_proveedor');
        
        $logos_array = array();
        foreach ($query->result() as $row):;
        $logos_array[] = $row[0]; 
        endforeach;
        
        return $logos_array;
    }
    }
    ?>

    View:

    <? echo 'Logos Listado';?>
    <? foreach ($logos_array as $row):?>
    <h1><?echo $row->logo ?></h1>
    <?php endforeach;?>

    Surely is all wrong, but i’m just starting..

    thanks!

  • #2 / Mar 15, 2010 4:37pm

    danmontgomery

    1802 posts

    foreach ($query->result() as $row):;
        $logos_array[] = $row[0];
    endforeach;

    You’ve got a stray ; which shouldn’t be there.

    $query->result() returns an array of objects, not a numeric array. You would reference fields by their actual names (if the field in the database is ‘logo’, it would be):

    foreach ($query->result() as $row):
        $logos_array[] = $row->logo;
    endforeach;

    Then, in your view, you want to reference the data you pass it, not the data from the model function. You are giving the view $this->data, which only has one element, ‘logo’. Also, you’ve constructed a one-dimensional array, so the “logo” index doesn’t exist here… It’s just an array of the logos.

    <? foreach ($logo as $row):?>
        <h1><?echo $row; ?></h1>
    <?php endforeach;?>

    If that doesn’t work for you, can you be more specific about what the error is?

  • #3 / Mar 15, 2010 4:41pm

    cahva

    662 posts

    You got it almost right.

    In the model section, you dont have to iterate the results there as you will iterate the results in the view.

    And loaded model is called with: $this->logo_model->show_logo_all()

    One other thing is that you call your controller “Something_controller”. I would dump the _controller from the end as then your urls will have that _controller also(unless you strip them in routes but thats another matter 😊 ). So just use class called Logo. Models are good to name like you did no prob.

    Heres the “correct” or should I say my way to do things 😉

    Model Logo_model

    class Logo_model extends Model {
    
        function Logo_model()
        {
            parent::Model();
        }
    
        function show_logo_all()
        {
    
            $this->db->select('logo');
            $query = $this->db->get('cms_proveedor');
            
            return $query->result();
            
        }
    }

    Controller

    class Logo extends Controller {
    
        var $data;
        
        function Logo()
        {
            parent::Controller();
            $this->load->model('logo_model');
        }
        
        
        function index()
        {
            $this->data['logos'] = $this->logo_model->show_logo_all();
            $this->load->view('logo_view', $this->data);
        }
        
    }

    ..and the view:

    <?php foreach ($logos as $row): ?>
        <h1><?php echo $row->logo ?></h1>
    <?php endforeach; ?>
  • #4 / Mar 15, 2010 4:42pm

    Juan Velandia

    101 posts

    Thanks noctrum, I’ll try to fix it and I’ll post the code and errors. I really appreciate it!

  • #5 / Mar 15, 2010 4:44pm

    Juan Velandia

    101 posts

    And thanks cahva, I’ll work over it!! I really appreciate it!

  • #6 / Mar 15, 2010 5:17pm

    Juan Velandia

    101 posts

    Thanks guys, it worked perfectly!

    http://192.168.1.2/ci4/index.php/logo

    Best regards!

  • #7 / Mar 15, 2010 5:18pm

    Juan Velandia

    101 posts

  • #8 / Mar 16, 2010 6:13am

    n0xie

    1381 posts

    /ci4/

    And here I was all excited about CI2.0 and you’re already up to 4 :gulp:

  • #9 / Mar 16, 2010 9:39am

    Juan Velandia

    101 posts

    @n0xie: ja ja ja, It´s just the name of the folder… but we are looking forward to it!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases