Welcome Guest, Not a member yet? Register   Sign In
Running "if" statements in the View?
#1

[eluser]NateL[/eluser]
Does it go against the MVC architecture if I run if statements inside my view file?

I have a form that displays an animals gender. Male/Female/Unknown.

When someone edits an animal, I need the appropriate radio button to be checked, so here's how I've done it:

Code:
<p><label>Sex</label><br />
    &lt;input type="radio" name="sex" value="Male" &lt;?php if ($animal-&gt;sex == 'Male'){echo "checked=\"checked\"";}?&gt; />Male<br />
    &lt;input type="radio" name="sex" value="Female" &lt;?php if ($animal-&gt;sex == 'Female'){echo "checked=\"checked\"";}?&gt; />Female<br />
    &lt;input type="radio" name="sex" value="Unknown" &lt;?php if ($animal-&gt;sex == 'Unknown'){echo "checked=\"checked\"";}?&gt; />Unknown<br />
</p>

Like I said...It works, but does it go against the MVC architecture? and is there possibly a better way to determine which option is checked in my model or controller?

Thanks Smile
#2

[eluser]umefarooq[/eluser]
i prefer you to do it in controller because in future if you want to change the template than you just change the view files your controller remain same, another thing for our use we can use some conditions in view also.
#3

[eluser]obiron2[/eluser]
I agree, you already know $animal->sex in the controller (or possibly even in the model), so set the checked value of the relevant radio button in there and use the form helper to set it to be selected.

Obiron




Theme © iAndrew 2016 - Forum software by © MyBB