Welcome Guest, Not a member yet? Register   Sign In
trouble getting drop menu from model
#1

[eluser]trevor2[/eluser]
I'm having issues getting my drop menu from the model. The foreach section works if I have it in the view.

Model
Code:
class Submit_model extends Model {
    function dropmenu()
    {
        foreach($query->result() as $row):
            echo '<option value="' . $row->id . '">' . $row->Name . '</option>';
        endforeach;
        
    }

Controller
Code:
function submit()
        {    
            $this->load->database();
            $this->load->model('Submit_model');
            $this->load->helper('html');
            $this->load->helper('url');
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            
            $data['menu'] = $this->model->dropmenu();
            
            if ($this->form_validation->run()== FALSE)
            {
                
                $this->load->view('submitform', $data);
            }
            else
            {
                $this->load->view('formsuccess');
            }
        }
View
Code:
<select>
&lt;?php echo $data;?&gt;
</select>

My error
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Portal::$model

Filename: controllers/portal.php

Line Number: 22

Fatal error: Call to a member function dropmenu() on a non-object in C:\xampp\htdocs\codeigniter\system\application\controllers\
#2

[eluser]trevor2[/eluser]
This doesn't work either.

Code:
function statemenu()
    {
        $query = $this->db->get('states');
        $x = '<option value="' . $row->state_id . '">' . $row->sName . '</option>';
        foreach($query->result() as $row):
            echo $x;
        endforeach;
        
        return $x;
    }
#3

[eluser]pistolPete[/eluser]
First of all, you NEVER EVER echo in a model!
Read through
http://en.wikipedia.org/wiki/Model–view–controller
http://ellislab.com/codeigniter/user-gui...w/mvc.html

Since your model is called Submit_model, you access its object using:
Code:
...
$data['menu'] = $this->submit_model->dropmenu();
...
#4

[eluser]trevor2[/eluser]
Guess I don't understand how to properly echo $data in the view. Since there should be no looping in the view, the foreach loop generating the <option> tags would have to be in the controller, but it's not working.
#5

[eluser]pistolPete[/eluser]
[quote author="trevor2" date="1234745912"]Guess I don't understand how to properly echo $data in the view. Since there should be no looping in the view,...[/quote]
Not necessarily, if you don't try to follow the MVC approach too strict, you can of course have loops in your views.
Have a look at the user guide: http://ellislab.com/codeigniter/user-gui...views.html

[quote author="trevor2" date="1234745912"]... the foreach loop generating the <option> tags would have to be in the controller, but it's not working.[/quote]
"It's not working" isn't a good explanation of your problem... be more precise!
#6

[eluser]trevor2[/eluser]
It's soo confusing learning this class and object stuff right out the gate.

This works. My problem was that I was echoing $data in the view,
when it should have been the array deal, state_menu, in this part:
$data['state_menu'] = $this->submit_model->getstates();

I would prefer to have foreach in the controller, so I can just "echo $whatever", but my head is hurting a bit at the moment.

Model
Code:
class Submit_model extends Model {
    function Submit_model()
    {
        parent::Model();
    }
    
    function getstates()
    {
    $query = $this->db->get('states');
    return $query;
    }

Controller
Code:
class Portal extends Controller{
    function portal()
        {
            parent::Controller();
            
            $this->load->helper('url');
            $this->load->helper('html');
            $this->load->helper(array('form', 'url'));
            $this->load->database();
        }
        
    function submit()
    {    
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->database();
        $this->load->model('Submit_model');
        $data['state_menu'] = $this->submit_model->getstates();

        if ($this->form_validation->run()== FALSE)
        {
                
            $this->load->view('submitform', $data);
        }
        else
        {
            $this->load->view('formsuccess');
        }
        }
}

View
Code:
<select>
&lt;?php
foreach($state_menu->result() as $row)
{
    echo '<option value="' . $row->id . '">'. $row->Name . '</option>';    
}
?&gt;
</select>
#7

[eluser]pistolPete[/eluser]
[quote author="trevor2" date="1234755676"]
I would prefer to have foreach in the controller, so I can just "echo $whatever", but my head is hurting a bit at the moment.
[/quote]

The way you used your model, controller and view is similar to my structure.
You could of course generate a string containing all the contents in your model / controller and pass it to the view, but what do you gain from it?
#8

[eluser]obiron2[/eluser]
There is nothing wrong with looping in the view to generate list data (e.g. dropdown options) however if you do this a lot, you may want to farm it off to a function that generates the html and returns it to the view as an html string, but this should be called from withing the view as it is a presentation decision, not a business logic one.

I use this process for rendering forms. I build all the form data in the controller and then call a generate_form($formdata) helper which produces the form with all the correct labels, ids and formatting DIVs that I require.

The big advantage is that if you want to change the rendering of repeated information, you simply have to change the function and not every view that uses it.

Obiron




Theme © iAndrew 2016 - Forum software by © MyBB