Welcome Guest, Not a member yet? Register   Sign In
Load a view if file exists - how?
#1

[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 (@file_exists(APPPATH."view/menu/{$controllername}_menu_view.php"))
{
   $this->load->view("menu/{$controllername}_menu_view.php");
}

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
#2

[eluser]pistolPete[/eluser]
How do the menu views differ?
What should happen when there is no corresponding view file? Display nothing?
#3

[eluser]Colin Williams[/eluser]
You need a dollar sign to reference variables, try:

Code:
$controllername = $this->uri->segment(1);
if (@file_exists(APPPATH."view/menu/{$controllername}_menu_view.php"))
{
   $this->load->view("menu/{$controllername}_menu_view.php");
}
#4

[eluser]Keem1[/eluser]
Menu views are simple html files (with some PHP codes), no difference with other views.
Yes, display nothing (without error messages :lolSmile when this views aren't in the view/menu/ folder.
#5

[eluser]Keem1[/eluser]
[quote author="Colin Williams" date="1240076843"]You need a dollar sign to reference variables, try:

Code:
$controllername = $this->uri->segment(1);
if (@file_exists(APPPATH."view/menu/{$controllername}_menu_view.php"))
{
   $this->load->view("menu/{$controllername}_menu_view.php");
}
[/quote]

Hm... my code contains this dollar characters, they was stolen on copy-paste(??)
Thanks, but the problem isn't caused by the dollars.
#6

[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.
#7

[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?
#8

[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 :-)




Theme © iAndrew 2016 - Forum software by © MyBB