0

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?

2
  • The views got messed up? Then whats the url source for the images and css if they got messed. See the errors on Network Console. Commented Apr 10, 2014 at 15:17
  • Are you using relative paths? Commented Apr 10, 2014 at 15:18

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I fixed it but as a new user I can't answer my question :/ Here it is : "Ok, got it. I was using base_url() for my js but not for my css, I added it for the css and it was ok. Then I added base_url() in my views for the images and it's fixed too. Thanks for showing me the right way ! ;)"
1

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

4 Comments

I call my js/css this way, all is placed in head.php : <script type="text/javascript" src="<?php echo base_url();?>js/script.js"></script> in my view file, images are called this way : <img src="./images/quizz/youtube.png"> It works when I call my page the standard way (/quizz), but not if I call the function (/quizz/function). I should use base_url() everywhere in my views ?
Probably not the solution because I call my css in head.php using base_url(), but here the link when I analyse the source : http://localhost:8888/sanofi/**quizz/**css/style.css. quiz/ is added in the path, I don't know why, it works if I remove it...
As you point out above, yes, you should use base_url() to make absolute path to the resources. That way, it wouldn't matter from where you're calling it.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.