Welcome Guest, Not a member yet? Register   Sign In
Calling model from view
#1

[eluser]nogoodchoices[/eluser]
Hey everyone,

I know this is taboo, but I am using codeigniter for the first time (starting around december...) for my senior project, and I am trying to utilize a model from a view. This thing doesn't have to be perfect, it just has to work before a rapidly approaching deadline, hence my need to break the rules of the architecture.

I am using CSS to create a frame look to my site without using frames. I have one main PHP file that calls a separate view for each section as needed. My need is to display the same content in the right side when a search is submitted on the left side. I am trying to use my models to keep the information displayed (as grabbed from the database) showing correctly.

After all of my research, all I have come up with is that it IS possible, but I can't for the life of me figure out how.

The only thing I have tried is opening the model as I would from a controller ($this->load->model('search_process')Wink and all that does is make my page unhappy and not show anything.

Any insight would be amazing, I am sorry if this was confusing, I have been making myself crazy for the past 5 hours (and that's just today of course).

THANK YOU!
#2

[eluser]maria clara[/eluser]
can you post the error/s if so, and the code pertains to that??
#3

[eluser]nogoodchoices[/eluser]
I am not getting any errors, all it does is completely stop displaying anything in my view as soon as it gets to that pane in the CSS arch.

Here are some snippets from the view and model:

View:
Code:
$this->load->model('search_process');
$found = $this->search_process->searching();
        foreach($found as $step1) {
            if($i == 1) {
                foreach($step1 as $step2) {
                    $names[] = $step2;
                }
            } else if ($i == 2) {
                foreach($step1 as $step2) {
                    $ids[] = $step2;
                }
            }
            $i++;
        }
        for($i = 0; $i < count($names); $i++) {
            echo "&bull;&nbsp;&nbsp;<a href='http://baja.dmhleb.net/baja/profile/view_profile/".$ids[$i]."'>".$names[$i]."</a><br/>";
        }

Model:
Code:
function searching()
    {        
        $query = $this->db->query("SELECT f_name, m_name, l_name, id FROM faculty WHERE f_name LIKE '%".$this->session->userdata('result_info')."%'
                            UNION SELECT f_name, m_name, l_name, id FROM faculty WHERE m_name LIKE '%".$this->session->userdata('result_info')."%'
                            UNION SELECT f_name, m_name, l_name, id FROM faculty WHERE l_name LIKE '%".$this->session->userdata('result_info')."%'");
        // Retrieve reults from query, store them in an object
        $result_name = array();
        $result_id = array();
        if ($query->num_rows() > 0){
            foreach ($query->result() as $row) {
                $result_name[] = $row->f_name." ".$row->l_name;
                $result_id[] = $row->id;
            }
        }
        $results = array($result_name, $result_id);
        return $results;
    }

This code works when the controller is used, but I need to bypass the controller.

Thanks
#4

[eluser]maria clara[/eluser]
[quote author="nogoodchoices" date="1265959968"]

This code works when the controller is used, but I need to bypass the controller.
[/quote]

have you read the rules of the MVC? here take a look: http://codeigniter.com/wiki/MVC/

you can't really access the Model in the View. try putting this line to your controller:

Code:
$this->load->model('search_process');
#5

[eluser]nogoodchoices[/eluser]
It does work when I do that, but my problem is that when the page gets resent then the controller doesn't get called for each view because it's using a different chunk of the page... I think I understand the MVC rules (but probably not), and the only reason I am looking this far into it is because I read a couple of other forums that said you could access the model from the view, much the same as you can from the controller... bleh. I will probably just have to redo my whole setup.
#6

[eluser]gcc.programmer[/eluser]
Hi:

I'm a bit dense, so forgive me if I'm not "getting" your structure. What I understand is that in your controller, at least initially, you're constructing a (master view) composed of X (sub-views). One of the sub views, on the left of the page has a search box, and the results of that search are to be displayed on the right side of the page.

One approach you might take is to compose a master_page function in the controller, that would assemble your completed page from the sub-page views. Said a different way, each section of your master page would have it's own assembly function that creates that portion of the master view. When each section's function has been called and done their respective things, the master_page function would assemble the various parts, and render the view.

Creating your master page then would be an assembly process. So, when your search is done, that section would be updated, and then the master page would be rendered including the new information. I'd go a step further and use ajax to just update that section, so the entire page doesn't have to load each time someone does a search.
#7

[eluser]gcc.programmer[/eluser]
I had a second thought for you, also. Though it goes against MVC (which is what you are wishing to do anyway).

If you look at Codeigniters documentation on MODELS, they state that Models are OPTIONAL. Re-write your Model as a Controller, and use it that way. You will note, however, that a view cannot call a Controller.

I don't know how useful this may be to you. Likely you've already thought of it, but I thought I'd put it out there, just in case.
#8

[eluser]nogoodchoices[/eluser]
Thanks for the thoughts, I appreciate the time you put into it. I definitely agree with your ideas in the first post, and the controller rendering the main page was my backup if I couldn't figure this problem out. It would make the most sense, probably work the best, and look cleaner. If only I had thought of that when I started... the problem is I am pretty much in the middle of my project, to far in to redo the structure, yet too far from the end to just fake my way through it... ugh!

Again, I appreciate everyone's time and thoughts. I am probably going to do some file i/o and includes to fudge my way through it.... yikes.

-Allison
#9

[eluser]gcc.programmer[/eluser]
There is actually one last thing you could try, to get a hold of the model from your view. This is ugly, and I don't know if it would work as I haven't tried it myself. I was curious about your question, and googled, but was unable to find any answers about using a model directly in a view.

We are able to grab the CI Super Object with a function call from system/codeigniter/base (either 4 or 5).php They both return the singleton of this object.

You can grab the CI Super Instance with code like:
Code:
$obj =& get_instance();
Then you might try loading your model:
Code:
$obj->load->model('search_process');
From there, the model *should* be available in your view.

I've never had something like what you're trying to do come up in my daily coding, so I'm curious. ;-)
#10

[eluser]SitesByJoe[/eluser]
Everything about the model in the view aside, let's make sure we understand what the problem actually is.

From what I understand from your original post, you have an interface with an area in it that has changing content. Other content around the panel is to remain fixed.

If all you're trying to do is get that behavior to work you need to do one of a couple things:

1. Load all the content that would go into the box in the beginning and hide it with CSS. Then you can call your model from the controller like normal and load everything before you even get your view loaded. Use javascript to show and hide the box's content as necessary.

2. Load things on the fly (sounds like what you're attempting). In this case you have to have javscript call a url (controller) and have the resulting HTML populate the space, replacing what was there.

In either example, you'd have no reason to call a model in your view.

Maybe I'm off track?




Theme © iAndrew 2016 - Forum software by © MyBB