![]() |
Load a view if file exists - how? - 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: Load a view if file exists - how? (/showthread.php?tid=17870) |
Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Keem1[/eluser] Hi Guys! I need a controller-depended menu (view), what loads if a file exists in view directory, like this: Example controller: contact. If you navigate to mypage.com/contact/, and the following file exists: view/menu/contact_menu_view.php, this view loads, else do nothing. I've tried this, but not worked: Code: $controllername = $this->uri->segment(1); If comment the "if" line, and the view file exists, load it fine. The problems begin there when no view files with same syntax the name of controller (an error message will be thrown). What should I do to solving this problem? Edit: I've edited the missing dollar signs Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]pistolPete[/eluser] How do the menu views differ? What should happen when there is no corresponding view file? Display nothing? Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Colin Williams[/eluser] You need a dollar sign to reference variables, try: Code: $controllername = $this->uri->segment(1); Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Keem1[/eluser] Menu views are simple html files (with some PHP codes), no difference with other views. Yes, display nothing (without error messages :lol ![]() Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Keem1[/eluser] [quote author="Colin Williams" date="1240076843"]You need a dollar sign to reference variables, try: Code: $controllername = $this->uri->segment(1); Hm... my code contains this dollar characters, they was stolen on copy-paste(??) Thanks, but the problem isn't caused by the dollars. Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Colin Williams[/eluser] And are you positive that $controllername holds what you expect? If you are routing, segment 1 won't necessarily be your controller name. Use rsegment to be safe. Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Keem1[/eluser] [quote author="Colin Williams" date="1240077423"]And are you positive that $controllername holds what you expect? If you are routing, segment 1 won't necessarily be your controller name. Use rsegment to be safe.[/quote] I felt that will be difficulties here. Ok, thanks! And the original problem? What should I do in your opinion? Load a view if file exists - how? - El Forum - 04-18-2009 [eluser]Keem1[/eluser] How can I be so stupid??? I can't believe it! The correct name of the folder isn't "view", it's name is "views". So, works well my idea I wrote in my first post :-) |