tags:

views:

71

answers:

2

Iam trying to retrieve data from mysql database into stylesheet.php but it is not working.

main page:

<link rel="stylesheet" href="includes/dynamicstyle.php" media="screen">

stylesheet.php

<?php header("Content-type: text/css;"); ?>  
<?php
require 'connect.php';
$userid = $_SESSION['uid'];
$select_query = "SELECT * FROM theme where user_id = $userid";


$primaryTextColor = '#336600';
$secondaryTextColor = '#fff';
$tertiaryTextColor = '#555';
$primaryBGColor = '.$background.';
$secondaryBGColor = '#ccc';
$tertiaryBGColor = '#000';
$primaryTextSize = '10'; //pixels
?>
#pro_theme{
 color: <?php echo $primaryTextColor?>;
 background: <?php echo $primaryBGColor?>;

}
#con_theme {
 background: <?php echo $secondaryBGColor?>;
 font-size: <?php echo 1.1*$primaryTextSize ?>px;
}
#des_theme{
background: <?php echo $secondaryBGColor?>;
 font-size: <?php echo 1.1*$primaryTextSize ?>px;
}
#basic{
background: <?php echo $secondaryBGColor?>;
}
<?php 
$result_select = mysql_query($select_query)or die(mysql_error());
if ($result_select){
     $row = mysql_fetch_assoc($result_select);
     $background = $row['background'];

    }
?>

It is not working for me, I just want to know is that a right way to do or is there anything better way of doing this.

thanks.

Edit:

Thanks for the advice, its working now.

+2  A: 

This line is slightly confusing:

$primaryBGColor = '.$background.';

It means you'll always be outputting this:

#pro_theme{
 color: #336600;
 background: .$background.;
}

The best way to be debugging this is to just open this file in the browser (http://mysite/includes/dynamicstyle.php) so you can see the output clearly.

nickf
+2  A: 

you're not putting the $background value into the output at all. you're outputting the string .$background. (which is not expanded ever, because it's in single quotes) into your output. Then after this has already been sent to the browser (or at least buffered to be sent) you set a variable named $background.

You'll need to set $background before you output your css, and you'll need to change your quoting, so $background is expanded somewhere in your output.

Also, it would seem that you would benefit from a better way to stick variables into your CSS output. as Lucky suggested, heredoc might work well here.

Also, it never hurts to have type="text/css" in the tag. Might even be required by the spec, I forget.

JasonWoof