CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: CSS from PHP - why doesn't it work?  (Read 12064 times) Bookmark and Share
cuberat
Newbie
*
Posts: 40


View Profile
« on: Mar 23, 2011, 10:11:49 am »

I'm using PHP to compose some CSS.  It's created correctly, and delivered to the browser, but it isn't taking effect.

What's wrong?

 Huh
Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #1 on: Mar 23, 2011, 10:30:18 am »

Could be quite a number of reasons...

How are you delivering it to the browser?
Is it within <style> tags?
Does the CSS validate properly?



I'm using PHP to compose some CSS.  It's created correctly, and delivered to the browser, but it isn't taking effect.

What's wrong?

 Huh
Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #2 on: Mar 23, 2011, 03:12:37 pm »

common.php just has session initialization stuff.

Code:  
Highlight Mode: (PHP)
  1. <?php require_once 'common.php'; ?>
  2. @import "http://o.aolcdn.com/dojo/1.4/dojo/resources/dojo.css";
  3. @import "http://o.aolcdn.com/dojo/1.4/dojo/resources/dnd.css";
  4. @import "http://o.aolcdn.com/dojo/1.4/dijit/themes/tundra.css";
  5. body
  6. {
  7. background-color:pink;
  8. }
  9. div#divCapuchin div#divSourceContainer,div#divCapuchin div#divTargetContainer
  10. {
  11. width:300px;
  12. height:200px;
  13. }
  14. div#divCapuchin div.segment
  15. {
  16. width:48px;
  17. height:181px;
  18. float:left;
  19. margin:1px;
  20. overflow:hidden;
  21. }
  22. <?php for ($i=0;$i<SEGMENTS;$i++): ?>
  23. div#divCapuchin div#divTargetContainer div#divTarget-<?php echo $i ?>
  24. {
  25. float:left;
  26. background:transparent url(images/cats-light.jpg) no-repeat -<?php echo (SEGMENT_WIDTH*$i)+1 ?>px 0px;
  27. }
  28. <?php endfor ?>
  29. .break
  30. {
  31. float:none;
  32. clear:both;
  33. }
  34.  
 

Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #3 on: Mar 24, 2011, 04:59:35 am »

How are you getting the CSS to the browser?  Is it a link tag?  Style?  Server-side include?

Also - the browser may not want to display a pink background.  Smiley
Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #4 on: Mar 24, 2011, 05:42:02 pm »

I'm using dojo to load it, and the CSS depends on an id, which is what PHP is adding.

These are the lines in the XHTML document:

Code:  
Highlight Mode: (HTML)
  1. <link type="text/css" src="" rel="stylesheet" id="lnkCapuchin" />
  2. <script type="text/javascript" src="http://o.aolcdn.com/dojo/1.5/dojo/dojo.xd.js">var djConfig={parseOnLoad:true};</script>
  3. <script type="text/javascript" src="<?php echo $iId ?>-capuchin.js" id="srcCapuchin"></script>
 

The <link> tag is just a marker, when distributed, people will receive a key, they will add instead of the echo $iId, and I wanted the code to run where they just paste the above three lines in an XHTML document and a few tags in a form later.

In the script file, which is javascript processed through PHP, the href is set.

Code:  
Highlight Mode: (Javascript)
  1. dojo.addOnLoad(function()
  2. {
  3. dojo.byId('lnkCapuchin').href='<?php echo $iId ?>-capuchin.css';
 

These are the rewrite rules - the id is stripped off and passed on the query string.

Code:  
RewriteRule ^([\d]+)-capuchin.js$ capuchin.js.php?$1 [L]
RewriteRule ^([\d]+)-capuchin.css$ capuchin.css.php?$1 [L]

I saw a post here about running all .js requests through PHP - but I really only want these two requests rewritten.

Maybe the best answer would be to just put the id in the <link> tag?
« Last Edit: Mar 24, 2011, 05:44:53 pm by cuberat » Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #5 on: Mar 26, 2011, 07:02:45 am »

I found the answer by comparing the headers sent by .js and .css files that didn't run through PHP using FireBug.  I needed to set a the Content-Type header, because the rewrite was causing the Content-Type to be set as "Content-Type   text/html; charset=UTF-8"

Code:  
Highlight Mode: (PHP)
  1. <?php require_once 'common.php'; ?>
  2. <?php header('Content-Type:text/css; charset=utf-8;') ?>
 

Worked - for the CSS, and I put it in the js, too.
Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #6 on: Mar 26, 2011, 08:35:42 am »

Good job on finding that. I'd probably have puzzled over this until the Sun burned out.


I found the answer by comparing the headers sent by .js and .css files that didn't run through PHP using FireBug.  I needed to set a the Content-Type header, because the rewrite was causing the Content-Type to be set as "Content-Type   text/html; charset=UTF-8"

Code:  
Highlight Mode: (PHP)
  1. <?php require_once 'common.php'; ?>
  2. <?php header('Content-Type:text/css; charset=utf-8;') ?>
 

Worked - for the CSS, and I put it in the js, too.

Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #7 on: Mar 26, 2011, 07:47:01 pm »

It took awhile.   Smiley
Logged
Tags:
Pages: 1
  Print  
 
Jump to: