Converting an app to CI (conceptualizing the breakdown) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Converting an app to CI (conceptualizing the breakdown) (/showthread.php?tid=9223) |
Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-17-2008 [eluser]CheekyGeek[/eluser] I'm a newbie to CI, but pretty excited by what I see in the screencast demos and working my way through the User Guide and Upton book. A couple of questions: Anything glaring "out of date" or deprecated in 1.6.2 from what is in the Upton book? For example, I don't recall the Controller functions in the blog screencast including the Code: {parent:: Controller();} Also, I'd like to convert an existing app to CI, just so I can conceptualize where in the CI folder structure (and MVC concept) the different pieces go. The app uses .php and .phtml pages (where the .phtml mix logic and presentation - an obvious no-no in MVCland). The app also uses a mixture of PHP and Javascript, for example it uses dynamic dropdown menus values and visible labels from a database table. It populates the dropdown menu on page load if a value has been set and modifies the value if the dropdown has been changed on submit. It also uses PHP sessions to set a securitylevel parameter that is checked to show/hide (and limit access to) certain functionality. For example, anything under the "admin/" URI will only be loaded if the session securitylevel is set to admin. The app also does php includes for files like shared/common_lib.php for functions, etc. I get that any HTML goes in the Views folder, but not sure where the Javascript goes or how I divide up the existing code to fit into the CI folder hierarchy. Anybody have any guidelines to follow - or do you suggest just starting to rewrite the existing apps functionality from scratch the CI way? TIA for any opinions you might wish to share. Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-17-2008 [eluser]Mirage[/eluser] I think you want to consider rewriting a lot, because your current application doesn't have a proper separation of logic and presentation. The .phtml would be in the views folder, that is correct. Javascripts, images, etc. should go outside of the application in the typcial public locations, e.g. /js and /img. This his what my my public_html folder of a website looks like: Code: /application Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-17-2008 [eluser]srajibtechno[/eluser] Hello CI experts.... I am a new CI developer..starting to build a web based application. I already created menu using css,php and my sql...but i can not implement it...with codeigniter..I already created model,controller and view files..for this application ..but i can not implement it perfectly ..can u help me...how can i do this??i want to use query1 and query2 in menu view file instead of result and result1(query1 and query2 come from menu_model file).... I attached my code are shown below.. inbook_links.sql CREATE DATABASE `inbook_db`; CREATE TABLE `inbook_links` ( `menu_id` int(9) NOT NULL auto_increment, `main_menu` varchar(150) collate latin1_general_ci NOT NULL, `sub_menu` varchar(150) collate latin1_general_ci NOT NULL, `menu_location` varchar(9) collate latin1_general_ci NOT NULL, `menu_contents` varchar(255) collate latin1_general_ci NOT NULL, PRIMARY KEY (`menu_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ; menu_model.php (Model file) class Menu_model extends Model { function Menu_model() { parent :: Model(); } function menu_getall() { $this->load->database(); $query = $this->db->query("SELECT * FROM inbook_links group by main_menu"); $query2 = $this->db->query("SELECT sub_menu FROM inbook_links"); return $query->result(); } } menu.php (Controller File) class Menu extends Controller { function Menu() { parent :: Controller(); } function GetAll() { $this->load->model('menu_model'); $data=array(); $data['query']=$this->menu_model->menu_getall(); $data['query2']=$this->menu_model->menu_getall(); $this->load->view('menu_view',$data); } } menu_view.php (view file.) <div class="mC"> <!-- Main Menu Start --> <?php $menuID = 1; while($val=mysql_fetch_array($result)) { ?> <div class="mH" onmouseover="toggleMenu('<?php echo 'menu'.$menuID; ?>')"> </div> <? $result = mysql_query("SELECT sub_menu FROM inbook_links where main_menu='$row[main_menu]'"); ?> <div id="<?php echo 'menu'.$menuID; ?>" class="mL" style="display:none;"> <?php while($val2=mysql_fetch_array($result2)){ ?><a class="mO" href="#"><?php echo $val2['sub_menu']; ?></a><? } ?> </div> <!-- Main Menu End --> <?php $menuID++; } ?> </div> cheers...;-) Regards..... SRR Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-18-2008 [eluser]Mirage[/eluser] Well, from an MVC point of view you have a few things in the wrong place. While CI allows it, your view file has way too much business logic in it. Try this: 1. From the controller call your Model to fetch the menu structure 2. In the Model, run all the queries you need to build up the menu and 'model' the complete result e.g. into a nested array or similar. Return that completed structure to the controller. 3. From the controller pass this menu structure to the view. 4. In the view unroll the data in a loop/nested loop. The model is where it all happens. The controller is just a traffic cop and the view just a [dumb] presentation of your data. Cheers! Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-19-2008 [eluser]srajibtechno[/eluser] ok..thank you...here is my modified code...for menu_model.php and menu_view.php but it is not working properly...please check this and give me solution as possible.. menu_model.php (model file) <?php /** Model: Left Menu Model * @author Syed Rajib Rahman , Date : 17-06-2008 * @copyright 2008 */ class Menu_model extends Model { function Menu_model() { parent :: Model(); } function menu_getall() { $this->load->database(); $this->db->select('main_menu'); $this->db->from('inbook_links'); $this->db->group_by("main_menu"); $GLOBALS['query'] = $this->db->get(); return $GLOBALS['query']->result(); } function sub_menu_getall() { $this->load->database(); $sub_menu_array = array(); for ($i=0; $i<count($GLOBALS['query']); $i++) { for ($i=0; $i<count($GLOBALS['query']); $i++) { foreach ($GLOBALS['query'][$i] as $value) { array_push($sub_menu_array,$value); } } } for ($i=0;$i<count($sub_menu_array); $i++) { $this->db->select('sub_menu'); $this->db->from('inbook_links'); $this->db->where('main_menu',$sub_menu_array[$i]); $query2 = $this->db->get(); return $query2->result(); } //} //End Foreach //} // End for } //End sub_menu_getall() } // End class ?> menu.php (controller file) <?php /** Controller : Left Menu Controller * @author Syed Rajib Rahman , Date : 17-06-2008 * @copyright 2008 */ class Menu extends Controller { function Menu() { parent :: Controller(); } function GetAll() { $this->load->model('menu_model'); $data=array(); $data['query']=$this->menu_model->menu_getall(); $data['query2']=$this->menu_model->sub_menu_getall(); // $data['query2']=$this->menu_model->menu_getall(); $this->load->view('menu_view_old',$data); } } ?> menu_view.php (view file) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <div class="mC"> <!-- Main Menu Start --> <?php $menuID = 1; for ($i=0; $i<count($query); $i++) { foreach ($query[$i] as $value) { ?> <div class="mH" onmouseover="toggleMenu('<?php echo 'menu'.$menuID; ?>')"> </div> <div id="<?php echo 'menu'.$menuID; ?>" class="mL" style="display:none;"> <?php foreach($query2 as $row2) { ?> <? } ?> </div> <?php } $menuID++; } ?> </div> </body> </html> Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-19-2008 [eluser]nmweb[/eluser] Please place your code between [ code ] [ / code ] This is unreadable Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-19-2008 [eluser]srajibtechno[/eluser] ok..thank you...here is my modified code...for menu_model.php and menu_view.php but it is not working properly...please check this and give me solution as possible.. menu_model.php (model file) Code: <?php menu.php (controller file) Code: <?php menu_view.php (view file) Code: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Converting an app to CI (conceptualizing the breakdown) - El Forum - 06-19-2008 [eluser]Mirage[/eluser] Hi - Not certain if you're just doing this for testing purposes, but you wouldn't make a specific controller for building a menu. You're essentially on the right path, though it seems like you have a bit of trouble with passing things around and OO practices in general. Controller Code: class SomeController extends Controller { Model Code: class MenuModel extends Model { View Code: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Code is not tested, but I hope this gets you on your way. Good luck! m |