?
Solved

CSS with PHP

Posted on 2012-09-02
3
Medium Priority
?
798 Views
Last Modified: 2012-09-03
Hello,

I can't get the test.php file to change the color of the text. where did i do wrong?

css.php file:-
body {
    background: <?php echo $bgColor; ?>;
    color: <?php echo $textColour; ?>;
}
 
#content {
    width: <?php echo $contentWidth; ?>px;
}

test.php file:-
<?php
header('content-type: text/css');

$contentWidth = 500;
$bgColour = '#C0FFEE';
$textColour = '#DECAFF';
$color='red';
$car='BMW';
?>
<html>

 <head>
  <link rel="stylesheet" href="css.php" type="text/css" media="screen" />

  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
 <?php echo "I have a $color $car"; // I have a red BMW
?>
 </body>
</html>
0
Comment
Question by:surework1
3 Comments
 
LVL 8

Assisted Solution

by:Ahmed Merghani
Ahmed Merghani earned 600 total points
ID: 38358629
I think it is better to add your file "css.php" as internal css as follow:
<?php
//header('content-type: text/css');
$contentWidth = 500;
$bgColour = '#C0FFEE';
$textColour = '#DECAFF';
$color='red';
$car='BMW';
?>
<html>
 <head>
  <style type="text/css">
  <?php
  include("css.php");
  ?>
  </style>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
 <?php echo "I have a $color $car"; // I have a red BMW
?>
 </body>
</html>

Open in new window


Reverse your spelling in "css.php" the bgcolor or bgcolour which variable name is the true one
0
 
LVL 16

Accepted Solution

by:
hankknight earned 800 total points
ID: 38358798
css.php file:

<?php
header('content-type: text/css');

if(!empty($_GET['contentWidth'])) $contentWidth = $_GET['contentWidth']; else $contentWidth= '500';
if(!empty($_GET['bgColour'])) $bgColour = $_GET['bgColour']; else $bgColour = '#C0FFEE';
if(!empty($_GET['textColour'])) $textColour = $_GET['textColour']; else $textColour = '#DECAFF';
if(!empty($_GET['color'])) $color = $_GET['color']; else $color = 'red';

?>

body {
    background: <?php echo $bgColour; ?>;
    color: <?php echo $textColour; ?>; 
}
 
#content {
    width: <?php echo $contentWidth; ?>px;
}

Open in new window



test.php file
<?php
$contentWidth = '500';
$bgColour = '#C0FFEE';
$textColour = '#DECAFF';
$color='blue';
$car='BMW';
?>
<html>
 <head>
  <link rel="stylesheet" href="css.php?contentWidth=<?php echo urlencode($contentWidth); ?>&bgColour=<?php echo urlencode($bgColour); ?>&textColour=<?php echo urlencode($textColour); ?>&color=<?php echo urlencode($color); ?>" type="text/css" media="screen" />
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 <?php echo "I have a $color $car"; // I have a red BMW
?>
 </body>
</html>

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 total points
ID: 38358868
I may not love the choice of colors, but I can help with the principles of integration.  When you are developing it is often easier to get quick and accurate results if you have everything in one place.  Then you can see the effects of your changes with a simple "view source" and you don't have to go looking into other PHP or CSS files.  Have a look at this and see if it makes sense to you, then post back with any questions. (Note: The #content div is not used in the original example, but I left it in anyway)
http://www.laprbass.com/RAY_temp_surework1.php

<?php // RAY_temp_surework1.php
error_reporting(E_ALL);


// SET MY VARIABLES (COULD COME FROM THE REQUEST AND A DATA BASE QUERY, ETC)
$contentWidth = 500;
$bgColour     = '#C0FFEE';
$textColour   = '#DECAFF';
$color        = 'red';
$car          = 'BMW';


// USE HEREDOC NOTATION TO CREATE THE CSS STRING
$css = <<<CSS
body {
    background: $bgColour;
    color: $textColour;
}

#content {
    width: $contentWidth px;
}
CSS;


// USE HEREDOC NOTATION TO INCORPORATE THE VARIABLES AND CSS INTO THE WEB PAGE HTML STRING
$html = <<<HTML
<html>
 <head>

  <style type="text/css" media="screen">
  $css
  </style>

  <title>PHP Test</title>
 </head>
 <body>

  <p>Hello World</p>
   I have a $color $car

 </body>
</html>
HTML;


// WRITE THE HTML STRING TO THE BROWSER
echo $html;

Open in new window

HTH, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question