Welcome Guest, Not a member yet? Register   Sign In
Creating an "if else" statement inside a view.
#1

[eluser]Sebish[/eluser]
I'm having some issues with creating an "if else" statement within a view of my current project, I have searched the forums and the user documention and been able to find suitable information to help me. I have a container that is the main view that all my other views load from depending on the link it is given, and I am trying to make a menu that changes depending on what section of the website you are in. I'm assuming that the easiest way to do this is an "if else" statement, however I cannot seem to get a variable to check it by, I can do this in PHP without a framework very easily, however with code igniter I am seeming to have problems. In essence I want it to check if a controller/model/view are loaded for example customers, and if this is the case it brings up the customers menu, simple (not for me, I'm sure there must be a simple solution to this problem.)

Any help would be great, am I going about this the wrong way? Or is there somehow I can create a check inside a controller or a model that I can reference inside the view. Cheers in advance for any help!

Code:
<? if (something == 'customer'): ?>
<? $this->load->view('layout/navigation/customers'); ?>
<? else: ?>
<? $this->load->view('layout/navigation/general'); ?>
<? endif; ?>
#2

[eluser]young[/eluser]
why you want to load view in a view page?
the 'view' should be loaded in controller
the controller choose which view to load
#3

[eluser]Sebish[/eluser]
I am trying to make it dynamic; here is some sample code from my container view:

Code:
<div id="container">
    <div id="header">
        <h1><a href="&lt;?=base_url();?&gt;">Web Design</a></h1>
    </div>
    
    <div id="main-nav">
        &lt;? $this->load->view('admin/layout/menu/mainmenu'); ?&gt;
    </div>
                  
    <div class="main">
        <div id="content-supp">
            &lt;? $this->load->view('admin/layout/menu/navigation'); ?&gt;
        </div>
        
        <div id="content-main">
            <div class="content">
                &lt;? $this->load->view('admin/layout/pages/' . $page); ?&gt;
            </div>
            <div class="clearer"></div>  
        </div>
    </div>
    
    <div id="footer">
        &lt;? $this->load->module_language('admin', 'footer');?&gt;
        &lt;? $this->load->view('admin/layout/footer'); ?&gt;
        <div id="bamboo"></div>
        <div class="clearer"></div>
    </div>    
</div>

Basically, the template remains the same, but the information in my "content" div loads the individual page:

Code:
&lt;? $this->load->view('admin/layout/pages/' . $page); ?&gt;
#4

[eluser]young[/eluser]
if you just want to implement the layout or template, you can search 'codeigniter layout' on google
#5

[eluser]Sebish[/eluser]
Thank you, I will have a look into that.
#6

[eluser]Sebish[/eluser]
Thank you for your help young, I figured it out (with a little help of Google). For future reference or anyone else who comes across a simular problem all I had to do was add:

Code:
$data['page_menu'] = 'example-nav.php';
$this->template['page']    = "example/display";
$this->system->load($this->template['page'], $data, TRUE);

Into the each controller, and then add:

Code:
&lt;? $this->load->view('layout/menu/navigation/' . $page_menu); ?&gt;

Into the container view.

Cheers again for your help Smile
#7

[eluser]Colin Williams[/eluser]
Giving the controller this bit of logic seems to be correct. When the question is "what is shown?" then you should do the logic in your controller. If the question is "how is this shown?" then you should run the logic in your view.
#8

[eluser]Sebish[/eluser]
[quote author="Colin Williams" date="1233921027"]Giving the controller this bit of logic seems to be correct. When the question is "what is shown?" then you should do the logic in your controller. If the question is "how is this shown?" then you should run the logic in your view.[/quote]

Thanks Colin, its all working how I wanted it now, and thats for that little tip helps makes things a little bit easier for me.




Theme © iAndrew 2016 - Forum software by © MyBB