Welcome Guest, Not a member yet? Register   Sign In
I can't understand how to use MVC ...
#1

[eluser]Ali Kimiagar[/eluser]
Hi,
I've got a question ...

Think , we've got a simple website witch has top menu , sidebar and content area.
Someone wants to access http://my-codeignitor.com/content/show/1

How can I load other views such as top menu , side bar beside the content!?

You know I'm confused because these (top menu , sidebar ) doesn't belong to content controller and model ...
#2

[eluser]Bart v B[/eluser]
Wel, it is not complicated. Smile

The controller controles things.
The model does the things.
The view is for presentation.
So with that knowlege we can do a step more.

The presentation with alway's the same menu and top, then you can do that like this:
create in the view folder a new foldes named: includes.
So /application/view/includes/

In that folder create a new php file called: template.php
And add some code like this:
Code:
<?php
// load the header
$this->load->view('includes/topmenu');

// load the sidebar
$this->load->view('includes/sidebar');

// load the main_content variabele so we can dynamic call other views
$this->load->view($main_content);

// load the footer
$this->load->view(includes/footer);
    
?>

in the includes/topmenu.php you put the code from your menu.
in the includes/sidebar.php you put the code from your sidebar

$main_content... // later Tongue

In the includes/footer.php you put the code from your footer. (something like </bode>&lt;/html&gtWink

Now we have a variabele to do someting with.

in your controller:

Code:
&lt;?php

class Content extends CI_Controller
{
   public funtion index()
   {

       // add some text
       $data['hello'] = 'Hello Codeigniter';
      // here we create a view with the content what you wanna show.
      $data['main_content'] = 'content';

     // here we call the template and show something.
     $this->load->view('includes/template', $data);
   }
}

Now when you create a content.php in your view folder.

Code:
<h1>&lt;?php echo $hello?;></h1>

And now you should see 'Hello Codeigniter' when you call it from your url.
#3

[eluser]Ali Kimiagar[/eluser]
Bart v B:
Well , first of all thanks.
you mean I don't have to write controllers for top menu and sidebar!?
our imaginary top menu and side bar are not static ... they get their data from database.
#4

[eluser]Bart v B[/eluser]
Ok topmenu and side bar are not static..

How are they build on?
I mean, they are depending on the controllername or something?
Then you can also do that the same way as i explained.

Other option is to use a template library.
for example https://github.com/philsturgeon/codeigniter-template/

In that case you can do something like this:
http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY

I don't know how complicated your site is, but if there ar just a few classes, models, and view files, then you better off with my first example.
#5

[eluser]Ali Kimiagar[/eluser]
[quote author="Bart v B" date="1312586404"]Ok topmenu and side bar are not static..

How are they build on?
I mean, they are depending on the controllername or something?
Then you can also do that the same way as i explained.

Other option is to use a template library.
for example https://github.com/philsturgeon/codeigniter-template/

In that case you can do something like this:
http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY

I don't know how complicated your site is, but if there ar just a few classes, models, and view files, then you better off with my first example.[/quote]

Thanks for everything ...
You're information is really useful.
#6

[eluser]Bart v B[/eluser]
Ok, i think i got the point..
you mean something like this?

your model test_model.php
Code:
&lt;?php

class Test_model extends CI_Model
{
   function GetMenu()
   {
     $query = $this->db->query("SELECT *
                                 FROM
                                 menu
                                 WHERE
                                 controller_name = '".$this->uri->segment(2)."'
                           ");
                          
        if($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
               //Here an array() from the result...
                $aData[] = $row;
            }
        }
          return $aData;
    }
}

the controller:

Code:
&lt;?php

class Content extends CI_Controller
{
    function __construct();
    {
        parent::__construct();
        $this->load->model('test_model');
    }
       public function index()
       {
    
           // add some menu
           $data['menus'] = $this->test_model->GetMenu();
           // some text or content
           $data['hello'] = 'Hello Codeigniter';
          // here we create a view with the content what you wanna show.
          $data['main_content'] = 'content';
    
         // here we call the template and show something.
         $this->load->view('includes/template', $data);
       }
}

views/includes/topmenu.php
Code:
&lt;?php

if(!empty($menus)):
    foreach($menus as $menu):
     echo '<li><a >name'">'.$menu->title.'</a>';
endforeach;
endif;
?&gt;

As you see, you don't have to create a new controller, only some tweaking with the variabele. Tongue

EDIT something strange is going on the forum..
can't make an hyperlink Sad
#7

[eluser]Ali Kimiagar[/eluser]
Bedankt my Dutch friend, it's what I want ...

P.S : What is that link!? Tongue
#8

[eluser]Bart v B[/eluser]
You speak dutch? Smile

what do you mean with that link?

i can not make an hyperlink tag here.. so the code breaks..
#9

[eluser]Ali Kimiagar[/eluser]
[quote author="Bart v B" date="1312588844"]You speak dutch? Smile
[/quote]

I wish I could speak Dutch , Deutsch , Italian , Spanish and ...

I as a Persian would love to speak foreign languages.


Code:
what do you mean with that link?

i can not make an hyperlink tag here.. so the code breaks..
aha ... lol


The way that you introduced me is kinda useful ...
#10

[eluser]Bart v B[/eluser]
your welcome Smile




Theme © iAndrew 2016 - Forum software by © MyBB