![]() |
PHP practices in CodeIgniter - 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: PHP practices in CodeIgniter (/showthread.php?tid=14522) |
PHP practices in CodeIgniter - El Forum - 01-06-2009 [eluser]XORd[/eluser] Hi, folks! I am very excited about CI since the very first time it came out. However, today my excitement was disturbed by the fact that I was not able to perform a moderately complex output to my view - something that I would normally do this way (using my favourite ez_sql database class). I have the corresponding CI instructions as comments. Code: <?php Please, give me an advice on the implementation of such a practice in CI. Thank you for you time to read through the code! -- XORd PHP practices in CodeIgniter - El Forum - 01-06-2009 [eluser]jalalski[/eluser] I'm missing the context of the above code. Is it in a controller? What errors are you getting? How is your $db getting initialized and is it an instance of CI's database layer? If this is a view, why are you doing the database stuff there and not in the controller/model layer? In general, if you have a problem, give as much detail as you can... PHP practices in CodeIgniter - El Forum - 01-06-2009 [eluser]Dready[/eluser] Hello ! , let me try... ;-) In the controller : Code: $this->load->view('item_table_begin'); In 'item_table_begin.php' view file : Code: <table> In 'item_table_row.php' view file : Code: <tr> and in 'item_table_end.php' view file : Code: </table> That's one of the possibilities. You can also prepare all item/subitem pairs (for example in an array) and loop directly inside the view : see Creating loops paragraph in the user guide chapter about views. Doing this you'll only need one view file. PHP practices in CodeIgniter - El Forum - 01-06-2009 [eluser]Popcorn[/eluser] To do that in CodeIgniter, the SQL or any data related functions would go into the model. This keeps it nicely separated from the rest of the code. The (x)HTML should always go into a view file. I wouldn't take your approach Dready as it seems a bit much, but rather do something along the lines of : Code: <table> PHP practices in CodeIgniter - El Forum - 01-06-2009 [eluser]XORd[/eluser] @jalalski : well, like I said, in this example I was using a database wrapper calles Ez_SQL by Justin Vincent. It is very convenient and drops my dev time significantly. @Dready : Thank you for the accurate coding! ![]() However, I am a little disturbed by the fact that I need to have chunks of views here and there. I think I'm going to combine your method with Popcorn's. @popcorn : yes, separation. That's why I chose CI ![]() Thank you! PHP practices in CodeIgniter - El Forum - 01-07-2009 [eluser]Dready[/eluser] @XORd & @popcorn : Ok , because I'm in a good day ;-) , here is the "one view to rule dem all" version : In the controller : Code: $data = array(); And the 'items_table' view file : Code: <table> Have a nice coding day ! |