Basic Form Help - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Basic Form Help (/showthread.php?tid=21199) Pages:
1
2
|
Basic Form Help - El Forum - 08-03-2009 [eluser]garycocs[/eluser] Hi Lads, I'm back to have another go at codeigniter, I'm basically starting a new site where the user can add in a youtube link and some data into a database, very very basic stuff. I figured I'd get this site going first with codeigniter and then develop more complex sites. So I can sort out views and all of that: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Which I call from the main controller. My question is about form structure, I have the form set up like so: Code: <h2>Add Video</h2> Which to me is not really using the MVC as there are DB requests etc in the view. Could someone please explain to me the proper conventions for form structure, where do I set it up? Where do I post the inputs to? Where does the validation go? Any help would be much appreciated. I probably could hack it out I just want to do things right since I'm starting! Thanks Gary Basic Form Help - El Forum - 08-03-2009 [eluser]bretticus[/eluser] To be brief, but to the point: Your database calls should be called from your controller and not in your view. You can easily pass the results (or objects to let you iterate the results) via the second parameter in... Code: $this->load->view('your_view', $thisone); Also, if your entire view is PHP code. That is, if you are rendering all the HTML via PHP, you're kinda missing the point. ;-) Basic Form Help - El Forum - 08-03-2009 [eluser]garycocs[/eluser] Nice one for that, changing it around now at the moment. Putting all DB queries into the controller. What about the input form? Where does this go?? (Sorry for the probably stupid question) Basic Form Help - El Forum - 08-03-2009 [eluser]bretticus[/eluser] There are no stupid questions (sorry, not trying to come off snarky.) Forms are HTML structures. Since HTML is your layout (what the user sees) forms belong in the view. The point being...you are trying to separate your business logic from your layout. Basic Form Help - El Forum - 08-03-2009 [eluser]garycocs[/eluser] Starting to get it now slowly, the blog demo is a great help. What do I do to shrink down the code here, this is an example of my controller for the site, you can see a fair bit of duplication in the code etc? What is the best way of getting rid of this? Code: <?php Basic Form Help - El Forum - 08-03-2009 [eluser]bretticus[/eluser] You should turn your videolist and queryheaderlist into models. You can call them from your controller. That way you are not duplicating code. In the blog video, the author elected to not show an example of model usage but the website docs show several examples. Basic Form Help - El Forum - 08-04-2009 [eluser]garycocs[/eluser] Ah right, so all repetitive DB info should become models?? What about inputting data from forms? Should this be model based or controller based? I've been reading Rapid Dev for CodeIgniter and he actually puts the whole MainPage display into a model??????? Thanks again for your help and advice. I think I'm starting to get the hang of it now. Basic Form Help - El Forum - 08-04-2009 [eluser]bretticus[/eluser] I've seen several examples where they just send the whole $_POST array as an argument to the model/db method. However, it's not always the case tat your form completely represents the database. I still like to write a little extra code and have parameters for my models. Any yes, models are a wonderful method for avoiding DNRY when it comes to doing data tier transactions. Of course, everything has to go through the controller at some point. Even my first example has to be called from the controller in some fashion. Making display elements in a model is completely nullifying the entire purpose of MVC. Not so sure about that book. I see someone corroborates my suspicions. Basic Form Help - El Forum - 08-05-2009 [eluser]garycocs[/eluser] Cool, that kinda makes things clearer. Thank god I don't have to go putting displays into the model. Just working on validating my form now, I get that ok it's just returning errors is the issue now. The example is as follows Code: function index() Which is fine when the page I'm returning is a new page but what if the form is only a small part of the page, i.e. if I want to return to the home page, the form being on the left of it? It seems like a lot of work to go through the load->view('home') plus running all the functions in the model for the data etc. Basic Form Help - El Forum - 08-05-2009 [eluser]bretticus[/eluser] Why do you have the form handler as the index method/function anyways? Doesn't it make more sense to put it in login() or some other function? All you have to do is change the action in the html form (or the one parameter in form_open() if you use that) to "controller/login." If you use sessions to keep authentication state (which is the norm) you can simply pass a boolean variable to your home page view to determine whether to show your login form or not. |