Welcome Guest, Not a member yet? Register   Sign In
MVC help for a newbie (very newbie)
#1

[eluser]Juan Velandia[/eluser]
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:

Code:
<?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:

Code:
<?

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:

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

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

thanks!
#2

[eluser]danmontgomery[/eluser]
Code:
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):

Code:
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.

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

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

[eluser]cahva[/eluser]
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 Smile ). 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 Wink

Model Logo_model
Code:
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
Code:
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:
Code:
&lt;?php foreach ($logos as $row): ?&gt;
    <h1>&lt;?php echo $row->logo ?&gt;</h1>
&lt;?php endforeach; ?&gt;
#4

[eluser]Juan Velandia[/eluser]
Thanks noctrum, I'll try to fix it and I'll post the code and errors. I really appreciate it!
#5

[eluser]Juan Velandia[/eluser]
And thanks cahva, I'll work over it!! I really appreciate it!
#6

[eluser]Juan Velandia[/eluser]
Thanks guys, it worked perfectly!

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

Best regards!
#7

[eluser]Juan Velandia[/eluser]
ups... http://190.146.3.147/ci4/index.php/logo

Bye!
#8

[eluser]n0xie[/eluser]
[quote author="Juan Velandia" date="1268702303"]/ci4/[/quote]
And here I was all excited about CI2.0 and you're already up to 4 :gulp:
#9

[eluser]Juan Velandia[/eluser]
@n0xie: ja ja ja, It´s just the name of the folder... but we are looking forward to it!




Theme © iAndrew 2016 - Forum software by © MyBB