Trium918 Posted January 31, 2008 Share Posted January 31, 2008 Is it possible to embed a PHP variable inside of a CSS file? If so, what would be the easy way to insert the variable? Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/ Share on other sites More sharing options...
aebstract Posted January 31, 2008 Share Posted January 31, 2008 I'm 99% sure you can NOT. What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454393 Share on other sites More sharing options...
kenrbnsn Posted January 31, 2008 Share Posted January 31, 2008 You can. Read Generating Dynamic CSS with PHP to find out how. Ken Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454397 Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 It can be done, but you are defeating one of the major points behind CSS files - that a file only has to be downloaded once and can be used by other pages in your site. A better practice is to write multiple classes in your CSS file for what you want to do, then change the classname dynamically with the php in your php script. Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454400 Share on other sites More sharing options...
aebstract Posted January 31, 2008 Share Posted January 31, 2008 A better practice is to write multiple classes in your CSS file for what you want to do Problem is.. he never stated what he wants to do. Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454403 Share on other sites More sharing options...
Trium918 Posted January 31, 2008 Author Share Posted January 31, 2008 I'm 99% sure you can NOT. What are you trying to do? #left_content { float: left; width: $PHP_variable; } #right_content { float: right; width: $PHP_variable; } There are two columns called left_content and right_content. I am trying to get the width for both left and right content to change when a user switches pages. The widths are stored in MySQL database under the page name. Example: Home.php left_content width = 370px; and right_content width = 370px; The user clicks on the browse.php link and the width for both columns changes to. Browse.php left_content width = 150px; and right_content width = 550px; Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454419 Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 If its a reasonable amount of possible widths (which it sounds like it should be), just create a number of classes in a normal CSS file. For the two examples you gave, you could call them this: #left_content_370 { float: left; width: 370px; } #right_content_370 { float: right; width: 370px; } #left_content_150 { float: left; width: 150px; } #right_content_550 { float: right; width: 550px; } Then, in your php file, you call it this way (for example) <div id="left_content_<?php echo $php_left_width_variable; ?>"></div> <div id="right_content_<?php echo $php_right_width_variable; ?>"></div> $left_width_variable and $right_width_variable are called from your database, and appended onto the end of the CSS ID name. The CSS IDs have already been set in your CSS file, so the applicable one will be attached to the <div> Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454426 Share on other sites More sharing options...
elpaisa Posted January 31, 2008 Share Posted January 31, 2008 This should Work: if(isset($_GET['page']) { if($_GET['page'] == 'next') { echo'<div style="float:left width:'.$row['width_two'].'"></div> <div style="float:right width:'.$row['width_two'].'"></div>'; } else { echo'<div style="float:left width:'.$row['width_one'].'"></div> <div style="float:right width:'.$row['width_one'].'"></div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454473 Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 That will work, but its bad practice. CSS shouldn't be written inline, it should be linked to externally. Separation of markup (xhtml) design (CSS) and function (javascript). They should all be in separate files. Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454484 Share on other sites More sharing options...
elpaisa Posted January 31, 2008 Share Posted January 31, 2008 There's no other way since the "widths" are taken from database!. Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454493 Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 You mean no way other than the solution I already gave? Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454494 Share on other sites More sharing options...
nikefido Posted January 31, 2008 Share Posted January 31, 2008 That will work, but its bad practice. CSS shouldn't be written inline, it should be linked to externally. Separation of markup (xhtml) design (CSS) and function (javascript). They should all be in separate files. Well "best practice" seems to be in conflict with PHP and CSS so I would go with with inline CSS in this case (see elpaisa's post). Especially if it's just a specific use like this. You mean no way other than the solution I already gave? what if it's a dynamic width? there could be any # of possible widths (potentially). Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454496 Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 How many pages can he have? But even if it is purely dynamic, he would be best outputting a class dynamically in the head of his (x)html document, then applying that class to the tag at hand. Inline CSS is the worst practice as far as CSS is concerned. Quote Link to comment https://forums.phpfreaks.com/topic/88725-php-css-question/#findComment-454507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.