Welcome Guest, Not a member yet? Register   Sign In
Can you call a controller from a body/view?
#1

[eluser]MTWIGG[/eluser]
I am currently writing a blog which includes polls. The main blog screen uses the jQuery $(window).scroll function to make an Ajax call to a CodeIgniter controller which loads a specified number of comments.

My problem is that the comment contains one or more polls which, because they are can be updated from the main screen, I would like to also be in a controller. However the following structure...

MainScreen controller
MainScreen body .... Makes an Ajax call to:
Comment controller
Comment body ... Makes a load call to:
Poll controller
Poll body

will not work as I can not find any equivalent to $this->load->view('poll_body', $poll_body_data) which will load a controller to form part of a screen.

I know I could, in this instance make the Poll controller code into some form of function and add it to the Comment controller, but I would like to know if there is a more general way around this as it would be useful if you could do something like the following (in a Body)

Code:
<div>
    <div>
        <div>
            // Add code here to load a controller to display a diary
        </div>
        <div>
            // Add code here to load a controller to display a clock
        </div>
    </div>
    <div>
        // Add code here to load a controller to display a note book
    </div>
    <div>
        <div>
            &lt;?php
            for ($i=1; $i<=$numberofalbums; $i++)
            {
                ?&gt;
            <div>
                // Add code here to load a controller to display photo gallery $i
            </div>
                &lt;?php
            }
            ?&gt;
        </div>
    </div>
</div>

etc. (Obviously the actual div structure could be a lot more complex than this).
#2

[eluser]wiredesignz[/eluser]
Learn to use the forum code tags
#3

[eluser]MTWIGG[/eluser]
OK. I have edited my post to make the code clearer

I had not noticed that option :red: ... thanks for pointing it out
#4

[eluser]Aken[/eluser]
I don't see why you'd want to do it that way - so you're saying you'd have at LEAST five server requests for a single page load, and even more depending on the number of galleries there are?

You can't rely on Javascript to load all of your content in that way, not stuck in the middle of a normal HTML page like that. You're asking for a debug nightmare, far more server load than you need, possible issues with visitors loading your website, etc.

If you use a dynamic method of loading comments based on the user scrolling, then that's great. If a comment contains a poll, it should be returned with the comment itself. You should only need a single call to pull X number of comments and all their related data.
#5

[eluser]Vheissu[/eluser]
There shouldn't be any situation where you need to call a controller. Controllers are nothing more than model interfaces really. Wouldn't it be best to have your functions that create the polls and fetch the comments be inside your model as presumably they're interfacing with the database anyway to get the information? You can call models from inside views.
#6

[eluser]MTWIGG[/eluser]
[quote author="Aken" date="1313211989"]I don't see why you'd want to do it that way - so you're saying you'd have at LEAST five server requests for a single page load, and even more depending on the number of galleries there are?[/quote]

Maybe that was a bad example. I was simply trying to come up with a situation where you are using the same controller multiple times on the same screen with different information. In the example I was placing them all together but it could be that in one case the div was top left of the screen and the other one bottom right.

[quote author="Vheissu" date="1313231891"]There shouldn't be any situation where you need to call a controller. Controllers are nothing more than model interfaces really. Wouldn't it be best to have your functions that create the polls and fetch the comments be inside your model as presumably they're interfacing with the database anyway to get the information? You can call models from inside views.[/quote]

The poll results will change depending on the users interactions with the screen so when the screen first loads the code will be called to create the poll graph, but then the same code must be callable via an Ajax call when the data is changed, hence since the Ajax will call a controller I thought "shouldn't the same controller be able to be called to create the original screen?"

My understanding of CodeIgniter (and I am a newbee) was that the Controller defines the data and the Body/View defines the layout. If, therefore, I wanted to include (for example) a DIV that contains a diary then as that diary will be defined by both data and layout it is a Controller I should be loading.

However, from your response it seems as though what I should be doing is writing a 'naked' view (with no controller) for the diary or poll results (or any other type of re-usable DIV) which calls Models to get the data it needs. If, and only if, I also need to be able to call the same DIV from an Ajax call do I need to create a Controller which does nothing more than pass the Ajax variables into the view via an appropriate $this->load->view call.

Have I got this right?




Theme © iAndrew 2016 - Forum software by © MyBB