0

I want to call other views in single controller function and want to change one view on clicking the link.

For example when I click the about_us link it should be open the about_us and when I click on contact_us link it should be replaced with about_us view but whole views remain same using jQuery and ajax.I don't know about jQuery and ajax. How it possible to do this?

My view code is

<ul>
<li class="active"><a href="<?php echo site_url() ?>/about_us">About Us</a></li>
<li><a href="<?php echo site_url() ?>/contact_us">Company Profile</a></li>
<li><a href="<?php echo site_url() ?>/faq">Why Choose Jan Japan</a></li>

my controller is

class Home extends CI_Controller
{
    $this->load->view('include/header');
    $this->load->view('top_strip');
    $this->load->view('top_menue);
    $this->load->view('about_us');
    $this->load->view('footer');
}
Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
  • 1
    You can not load multiple views in a controller though you can load a view in another view. here is you detail answer. http://stackoverflow.com/questions/9402924/how-to-load-view-into-another-view-codeigniter-2-1 – Haroon Jun 16 '15 at 05:45
  • first you need to write codes inside a function of a class. You cannot write code as you did.And you can load multiple view inside a controller. – Shaiful Islam Jun 16 '15 at 06:45
  • 1
    @Haroon you are wrong.You can load multiple view from controller too. – Shaiful Islam Jun 16 '15 at 06:47
  • @Shaiful You are quite correct. [Here is link](http://www.codeigniter.com/userguide3/tutorial/static_pages.html#adding-logic-to-the-controller) to documentation that proves it. – Tpojka Jun 16 '15 at 09:32

3 Answers3

3

View(Header.php):

    <ul>
    <li class="active"><a href="<?php echo site_url() ?>/about_us" onclick="loadaboutus()">About Us</a></li>
    <li><a href="<?php echo site_url() ?>/contact_us">Company Profile</a></li>
    <li><a href="<?php echo site_url() ?>/faq">Why Choose Jan Japan</a></li>
</ul>

<div id="divDynamic"></div>

in this view i have written inline JavaScript

onclick="loadaboutus()"

within tag. so when user click on About Us link, loadaboutus() function of javascript will be call a controller's loadaboutus() function.

Javascript-Ajax:

function loadaboutus()
{
$.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>home/loadaboutus",
                success: function (data) {
                   $('#divDynamic').html(data);
                }
            });
}

Controller:

function loadaboutus()
{
  $this->load->view('about_us');
}
Ashok Gujjar
  • 441
  • 5
  • 21
1

You have three links:

  1. /home
  2. /contact_us
  3. /faq

These are three separate controllers. When the links are clicked, the page is reloaded.

So you need three controllers.

class Home extends CI_Controller
{
    public function index()
    {
        $this->load->view('include/header');
        $this->load->view('top_strip');
        $this->load->view('top_menu');
        $this->load->view('home_page');
        $this->load->view('footer');
    }
}

class About_us extends CI_Controller
{
    public function index()
    {
        $this->load->view('include/header');
        $this->load->view('top_strip');
        $this->load->view('top_menu');
        $this->load->view('about_us');
        $this->load->view('footer');
    }
}

class Contact_us extends CI_Controller
{
    public function index()
    {
        $this->load->view('include/header');
        $this->load->view('top_strip');
        $this->load->view('top_menu');
        $this->load->view('contact_us');
        $this->load->view('footer');
    }
}

This is probably not the ideal way of doing it however as you are a new it is a good learning step.

LifeSteala
  • 63
  • 7
0

for this you have to make a common file template.php with the following code:

   foreach ($views as $view) {
      $this->load->view($view);
   }

now to called it from controller :

just do the following :

public function index()
    {
        $data=array('view1'=>"include/header",
                    'view2'=>"top_strip",
                    'view3'=>"top_menu",
                    'view4'=>"contact_us",
                    'view5'=>"footer"
          );
        $this->load->view('template',$data);
    }
Abdul Manan
  • 2,255
  • 3
  • 27
  • 51