Sebastien
Sebastien

Reputation: 1112

CodeIgniter - Loading a view from another function in the controller

Here's the issue :

If I call www.mysite.com/quizz, it loads my 'quizz' page.

But if I call www.mysite.com/quizz/start (containing the same code), the view is messed up because of broken links (images, css, ...)

Here is the content :

public function index()
{
       $this->load->helper('quizzsettings');
       $data['questions'] = getSettings();
       $this->load->view('head'); 
       $this->load->view('quizz', $data);
}

If I call another function (www.mysite.com/quizz/start), the layout fails :

public function start()
{
       $this->load->helper('quizzsettings');
       $data['questions'] = getSettings();
       $this->load->view('head'); 
       $this->load->view('quizz', $data);
}

As you know, CodeIgniter reads the URL like www.mysite.com/class/method, so it should be good, right?

Upvotes: 0

Views: 1464

Answers (2)

ied3vil
ied3vil

Reputation: 939

You can call $this->function(); in your method but it is not advised. The broken code you are speaking of, from what I understand is the HTML which is loaded fine, but the images/css etc are broken because you are not calling the files properly.

Can you post a sample of HTML you get that is "broken" please? (from the browser, with view source or ctrl+U' orcommand+U` in 90% of the browsers)

I advise you to review your view's html and how you load images and css files, and you should be set.

lookup please codeigniter URL helper (it might be URI helper in the docs, don't recall) and they have a simple site_url() function that i advise you use... you can call it either by site_url('controller/method') or site_url('3rdparty/jQuery/jQuery.min.js') - nice hack that works great with both controller methods and files, and you don't have to care about where you install the software

Upvotes: 1

Pattle
Pattle

Reputation: 6014

You want to call your CSS, JS and images using an absolute path. I suspect you are using a relative path. If you haven't already it's best to create a common header view and use base_url(). For example

<!-- Load CSS -->
<link type="text/css" rel="stylesheet" href="<?php echo base_url(); ?>css/style.css" />

<!-- Load JS -->
<script type="text/javascript" src="<?php echo base_url(); ?>js/script.js"></script>

Obviously change the path to the files to be correct for your installation

Upvotes: 1

Related Questions