tags:

views:

47

answers:

2

Hi,

Is it correct to use one view file with say 2 functions inside it.

For example I have project table in my database and I have 2 functions inside the view file for this specific MySQL query.

Each query returns a specific view so say

function 1 = num_rows function 2 = data inside the rows

Would this be correct or should I do something different ?

+2  A: 

Your View Files should not have functions in them.

The idea of MVC is to separate the Model, e.g. everything not related to presentation from the presentation layer (represented by V and C). M should be oblivious to V and C.

The controller handles any input to the presentation layer / the User interface. It delegates to the Model and sets anything the Model returns to the View.

The View is in charge of rendering your Model data and User Interface. If you need to have functions in the View, those are best kept in View Helpers.

See the Web Presentation Patterns in PoEAA for some ideas about how to best render the View. Parts of the book are available on Google Books.

Gordon
Could you explain I little more, My view file has the loop from a query and thats it,
Oliver Bayes-Shelton
+1  A: 

This type of functions doesn't belong into the view. The only Code that belongs into views is Code required for structuring the data.

This kind of functions belongs to the model, because it works on the data.

Perhaps you should take a look at the Model–view–controller Article on Wikipedia (http://en.wikipedia.org/wiki/Model–view–controller) to get a better understanding of MVC.

Sory, for some reason I can't make a real link to the wiki page.

jigfox
So so if I have my query in the model how do I print out the results if I cannot put the loop in the view ?
Oliver Bayes-Shelton
Why can't you loop in the view? this falls under the category "The only Code that belongs into views is Code required for structuring the data."
jigfox
You can use basic control structures in the view: if..then..else, for loops etc. As long as the query is not being executed there and you are simply passing the result to the view.
robdog
A loop isn't a function. It's a control structure.
robdog
I have put the loop in a function so I can call it rather than using an include.
Oliver Bayes-Shelton