Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I obviously need some logic that tells the form to pre-populate the form fields (if I'm editing a post) or to leave everything blank (if it's a new post).</p> </blockquote> <p>Check out the set_value() function in the <a href="http://codeigniter.com/user_guide/helpers/form_helper.html" rel="nofollow">Form Helper</a>. You can do something like this in your view:</p> <pre><code>&lt;input type="text" name="subject" value="&lt;?php echo set_value('subject', $subject); ?&gt;" size="50" /&gt; </code></pre> <p>If it's a new post, pass empty string as $subject when loading the View from the Controller. </p> <p>If it's editing, pass the subject of the post that is being edited as $subject. </p> <p>Also, if a user submits the form and there are errors and you need to reload the form, set_value() will return what the user just posted (i.e. $_POST['subject']).</p> <blockquote> <p>There's also other things such as the view page needs to know what title to display at the top of the page: "Create New Post" or "Edit Post".</p> </blockquote> <p>Just pass a variable named $page_title from your Controller to your View, and set the value accordingly. This is pretty common, especially when you start building reusable templates.</p> <blockquote> <p>Where does this logic go? Does it go in the View? Or does it go in the Controller? and Why?</p> </blockquote> <p>There should be almost no logic in the View. Maybe simple if/else statements if you must, and loops to iterate through arrays of data.</p> <p>Even Controller should not have much logic in it. It is mainly responsible for acting as a middle-man between the Models, the Views and the libraries. </p> <blockquote> <p>I've read that I should keep the controller code minimal</p> </blockquote> <p>That has to do with Controllers vs. Models, not the Views. It is good practice to keep Controllers smaller, and put as much of the logic as possible into the Models and the libraries. Also referred to as "Skinny Controllers, Fat Models".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload