Part of the EllisLab Network
   
 
Path to CSS doesn’t work a second time
Posted: 30 October 2007 02:11 PM   [ Ignore ]  
Summer Student
Total Posts:  12
Joined  10-29-2007

I have read the topics on CSS and JS but I still don’t understand why this is happening. If i call a view with a link to ‘default.css’ it works and if i press a menu item (for example /welcome/news) then using the same path to ‘default.css’ it doesn’t seem to work anymore, why is this?

Here is the ‘default.css’ link:

<link rel="stylesheet" href="system/application/views/default/css/default.css" type="text/css" /> 

first item:

index.php 

second item:

index.php/welcome/news 

both use the same ‘default.css’ link/path

Profile
 
 
Posted: 30 October 2007 02:20 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

Scouser,

It’s not a good pratice do this.

Why you create a folder to keep all these kind? (css, images, javascript)

I use a /public folder. In the same level than system.

So i call like that

<img src="/public/images/find.png" /> 

Or, use

<img src="<?=base_url();?>public/images/find.png" /> 

Get it?

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 30 October 2007 04:16 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  12
Joined  10-29-2007

The reason why I do this is because I want to be able to use multiple layouts. So I want to separate all the stuff like img/js/css and even the html views apart. In my DB ik keep a link to the active layout. If i want to create or use another layout, like for example during the christmas time or something I can switch over by just renaming my field in the DB

Profile
 
 
Posted: 30 October 2007 04:27 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  332
Joined  09-11-2007

like you wrote in your first post, what do you want will be impossible.

you can have dynamic content (images, css, even view), but you still need to show this to user. Browser don’t executes php smile

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 30 October 2007 09:56 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

Hi Scouser,

<link rel="stylesheet" href="system/application/views/default/css/default.css" type="text/css" /> 

 

The specified path in your link is relative to the residing folder. When you change folders to index.php/welcome/news I believe it is looking for and fails to find:

/welcome/news/system/application/views/default/css/default.css 

Try this debug code immediately before your header <link statement /> to see where you are:

<?php echo 'cwd --> ' .getcwd(); die; ?> 

 
Numerous people have opted to hard-code their css, js and img files in the root directory using this code:

<link rel="stylesheet" href="/css/default.css" type="text/css" /> 

Search the forum for CSS and you will find other options.

Cheers,

John_Betong
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 02 November 2007 09:39 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  31
Joined  10-30-2007

Hey John,

<link rel=“stylesheet” href=”/css/default.css” type=“text/css” />

/css doesn’t work for me either.

The only way I’ve found that works is to prepend <?php echo base_url();?> to the path, which seems like a hack.

cheers,
Sherban

Profile
 
 
Posted: 03 November 2007 06:59 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

Hi Sherbo,

In my index.php I have the following function defined which I find is handy in tracing the elusive paths. Personalise it and let us know the results.

index.php

//=================================================
  
function debug($dir='not specified'$die=false{
      
echo '<br />index.php-> function debug(...)<br />';
      echo 
'<br />'   .'LOCALHOST             ==> ' .'TRUE';
      ECHO 
'<br />getcwd() --> ' .$dir;
      
      echo 
'<br />'   .'$_SERVER[SERVER_NAME] ==> ' .$_SERVER['SERVER_NAME'];
      echo 
'<br />'   .'$_SERVER[HTTP_HOST]   ==> ' .$_SERVER['HTTP_HOST'];
      echo 
'<br />'   .'APPPATH               ==> ' .APPPATH ;           // application/
      
echo '<br />'   .'BASEPATH              ==> ' .BASEPATH ;          // C:/awww/ncgecom/system/
      
echo '<br />'   .'EXT                   ==> ' .EXT      ;          // php
      
echo '<br />'   .'SESSION[_MENU_]           ==> ' .$_SESSION['_MENU_'];    // _menu.php

      
if (isset($application_folder))
        echo 
'<br />'   .'application_folder ==> ' .$application_folder;

      if (isset(
$system_folder))
        echo 
'<br />'   .'system_folder ==> ' .$system_folder;

      if (
function_exists('site_url'))
        echo 
'<br />'   .'site_url()    ==> ' .site_url(''); 
 
      if (
function_exists('base_url'))
        echo 
'<br />'   .'base_url()    ==> ' .base_url('');

      if (
function_exists('system_url'))
        echo 
'<br />'   .'system_url()  ==> ' .system_url('');

      if (
function_exists('debug'))
        echo 
'<br />Yes we have debug()';

      if (isset(
$db['default']['hostname'])) {
    
echo '$db[default][hostname] ==> ' .$db['default']['hostname'];
            
}
      
if ($die){
        
die;
      
}

  }
//endfunc
  // echo debug(getcwd(), 0); die;

 
Try calling the function immediately before calling the

<link rel=“stylesheet” href=”/css/default.css” type=“text/css” />
 
Cheers,

John_Betong
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 03 November 2007 11:48 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  709
Joined  06-07-2007

John_Beton, you might want to use a switch statement, it’s much more efficient that a series of if statements, less repetitive too, easier to read.
Also, instead of all those echo statements you can use one, with double quotes, and use curly brackets for your variables ( echo “\”{$this-img_src}=\”>”; )
just to make it a little faster/cleaner.

 Signature 

jtaby.com

Profile
 
 
Posted: 03 November 2007 09:40 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

Hi Zaatar,

Check the code and you will notice that every if statement is a unique test. By using the switch statement I would only be able to select one item from a group.

The individual if statements was necessary to prevent PHP undeclared variable warnings and errors.

I would be grateful for a better suggestion on how to simplify the code.

Cheers,

John_Betong
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 03 November 2007 09:46 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  709
Joined  06-07-2007

Oh, I’m sorry John, I didn’t notice, apologies.

 Signature 

jtaby.com

Profile
 
 
Posted: 04 November 2007 07:50 AM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3150
Joined  06-11-2007

These are all good points but it can be done almost EXACTLY how you have it now, just use:

<link rel="stylesheet" href="/system/application/views/default/css/default.css" type="text/css" /> 

Notice the / at the front. As with others I don’t agree with this method, but it is possible for you to sue it that way.

You could take a look at my asset helper (link in my sig). That helps you store things in modules and use full CI paths that are generated dynamically. Id greatly recommend using that helper over any other method as hardcoded ones are a pain in the ass to update!

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 04 November 2007 11:16 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006
Zaatar - 04 November 2007 02:46 AM

Oh, I’m sorry John, I didn’t notice, apologies.

Hi Zaatar,

Off topic!

Referring to your second point about inserting variables inside double quoted strings, this is the reason and also it is easier for me to debug my frequent typos smile


Single Quotes versus Double Quotes

// http://www.evolt.org/these-things-i-know-php-tips

Any time you put something in “double” quotes, you are asking PHP to check that content for a variable. So even though the following lines do not contain variables within the double quotes, PHP will still waste precious computing time scanning them anyway.
$mytext = “Dental Plan”;
if ($mytext == “Dental Plan”) {
echo “Lisa needs braces”; }

Those same three lines of code could be executed much faster if ‘single’ quotes were used in place of “double” quotes.
$mytext = ‘Dental Plan’;
if ($mytext == ‘Dental Plan’) {
echo ‘Lisa needs braces’; }

Now that may not seem like much, but having PHP check for variables where it doesn’t need to over the course of a larger script, can certainly impede run-time. Just to clarify my point, PHP will not read a variable if it is within ‘single’ quotes.

 

Cheers,

John_Betong
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 05 November 2007 07:30 AM   [ Ignore ]   [ # 12 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3150
Joined  06-11-2007

I knew it! I always thought singles were probably faster but never knew why. Once again my psychic powers do me proud. :D

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 21 January 2011 05:15 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Avatar
Total Posts:  1
Joined  01-21-2011

I have used

<link href="<?php echo base_url();?>/default.css" rel="stylesheet" type="text/css"

kept default.css in htdocs\CodeIgniter folder.

and it works

 Signature 

cool smile

Profile