New to CI, have some questions - 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: New to CI, have some questions (/showthread.php?tid=6414) Pages:
1
2
|
New to CI, have some questions - El Forum - 02-26-2008 [eluser]Kilroy[/eluser] Hello! I recently discovered CodeIgniter, and for the last couple of days I've been playing around a bit with it. My plan is to find a nice php framework that allows me to separate presentation, data and logic and eventually migrate an entire website to it. However, I encountered problems right away. For creating the main menu, I have a system where the "current" link gets a class tag, like this: <a href="something.php" class="active">Some link</a>. This is based on the filename, and the logic looks something like this: if($filename == 'something.php') echo 'class="active'; Now, I wouldn't know how to do this with CI, since it always loads from index.php. My first idea was to compare it to the first segment of the URI instead of the filename, but that wouldn't work either, since having a default controller permits URIs without a first segment. My second idea was to set a variable in the controller, so the view will know which controller called it. I haven't tried this yet, but I am sure it would work, even though it would not be a very elegant solution. How do you solve this? Anyway, on to my next question. I have quite lots of things that simply need fetching from a database. Say I want to get the content-field from a table called 'news' where the id is 5. Right now, I do it this way (don't mind the tinymce-code): Code: $row = $this->db->where('id', 5)->get('news', 1); Of course there aren't only drawbacks with CodeIgniter, it would probably be a huge lift for my administration system. Just a last question. If I have tons of data that I need to pass to the view, is passing it through $this->load->view('someview', $data); the only way? It seems like this is a huge waste of memory. Kilroy New to CI, have some questions - El Forum - 02-26-2008 [eluser]xwero[/eluser] 1) you could create a helper function Code: function set_current($search,$match,$html = ' class="active"') Code: //controller 2) It's best to put your database queries in models so more than one controller/library can call them Code: // model New to CI, have some questions - El Forum - 02-26-2008 [eluser]adamp1[/eluser] Hi and welcome to the Community (hopefully for a while if you get to see why we love CI). However, I encountered problems right away. For creating the main menu, I have a system where the “current” link gets a class tag Normally for the thing with the menu being made active. I do as you say. For each one I just search the uri for the controller name and apply an 'active' class to the link. But as you said having a default controller gets around this, well why not instead of using $this->uri->uri_string() use $this->uri->ruri_string(). That way you get the routed uri address and this will always have a controller in it. I have quite lots of things that simply need fetching from a database. As said use models, means the same code is only written once. If I have tons of data that I need to pass to the view, is passing it through $this->load->view(’someview’, $data); the only way? It seems like this is a huge waste of memory. I suppose if you didn't want to pass a whole new variable and waste double the space you could do: Code: $this->load->view(’someview’, &$data); New to CI, have some questions - El Forum - 02-26-2008 [eluser]xwero[/eluser] Of course adamp1 that is even simpler Code: function set_current($match,$html = ' class="active"') Code: <ul> New to CI, have some questions - El Forum - 02-26-2008 [eluser]adamp1[/eluser] Thing is won't it have to use $this->uri->ruri_string() otherwise if no controller is given then the default route will be taken, and uri_string() won't return anything. But ruri_string() will. New to CI, have some questions - El Forum - 02-26-2008 [eluser]pickledegg2[/eluser] I'm learning jquery for adding styles to html on the fly, so far I've been blown away by it. You can style an entire document without adding any classes into the html directly, very flexible. I've PM'ed you Kilroy New to CI, have some questions - El Forum - 02-26-2008 [eluser]adamp1[/eluser] Problem is what if they don't have javascript enabled? Then your document has no styling and it means even more overhead. New to CI, have some questions - El Forum - 02-26-2008 [eluser]Kilroy[/eluser] Thanks for your quick replies! I am so new I apparently forgot all about Models. :red: Thanks for the tip! If I use models, I can reduce those three lines of code to just one: Code: $data['news'] = $this->news->get_latest(); As for the menu thing, I didn't know about ruri_string, and thank you again for the tip on that function! I used to utilize a function to generate the menu, it looks like this: Code: function menu_generator($menu_id, $menu) // menu_generator("menu", array("Start" => "index.php", "Users" => "users.php", "Functions" => "functions.php")); Oh, and thank you adamp1 for the tip on using references to pass the data, it never even crossed my mind. New to CI, have some questions - El Forum - 02-26-2008 [eluser]pickledegg2[/eluser] Quote:Problem is what if they don’t have javascript enabled? True enough! New to CI, have some questions - El Forum - 02-26-2008 [eluser]adamp1[/eluser] The replies are so quick I know for me because I'm sat here having to proof read a 100 page document. Boring as hell so helping problem solve is far more fun |