views:

126

answers:

3

TL;DR: I use PHP inside an external stylesheet, but it doesnt recognize the '/' as a PHP divider operator, this messes up my css any help is appreciated

OK so here's the deal.

I'm working on a simple website (I'm an intern, the assignment is not that hard, but i want to do it right) which loads the menu items dynamically from a page table in the dbase. with a recursive algorithm i get all the pages, and the child pages at the right place. In this function i count the amount of main menu items.

I do this because i need to make every <li> in the main menu <ul> such a width that it covers the whole width of the menu bar. I want this to be done dynamically so that if more pages are added as a main menu item, nothing has to change, only an insert to the database.

Now my question. i read right here: http://www.webmasterworld.com/forum83/2201.htm that its possible to configure apache so that it reads css as php files. Works as a charm, nothing wrong. Now i have:

<link rel="Stylesheet" type="text/css" href="<?php echo base_url().'css/css-reset.css'.'?item_count='.$menu_count; ?>" />

In my header ( as yuou can see i pass the menu item count as a post variable. This also works. My css:

<?php Header ("Content-type: text/css");?>
<?php
$wrapper_width = 900;
$menu_item_count = (int)$_REQUEST['item_count'];
$menu_item_width = $wrapper_width / $menu_item_count;
?>

/*lots more bhroing css */

div#wrapper
{
text-align:left;
width:<?php echo $wrapper_width.'px'; ?>;
}

div#menu a
{
display:block;
height:1.7em;
width:<?php echo $menu_item_width.'px'; ?>;
}

And this is where a weird thing comes up. if i set the $menu_item_count to be just an int (ex: $menu_item_count=150) it works as it should. But as soon as i use the divider operator between the two variables the css gets fucked up. other operators work.

the way i see it, the divider operator is special to css ( because of the comments?) and it doesnt read it as a php divider operator but as a css symbol.

Ive tried putting it in a function, the function works, but the divider operator fucks it up again.

So my questions are: Anyone ever worked with PHP in external Stylesheets? If so, can you help me figure out this problem?

Regards,

Menno

A: 

The Division symbol is running within PHP and shouldn't be causing you any trouble. Might I recommend using HEREDOC instead of opening and closing the PHP like that. It could make it easier to manage.

<?php 
header ("Content-type: text/css");
$wrapper_width = 900;
$menu_item_count = (int)$_REQUEST['item_count'];
$menu_item_width = $wrapper_width / $menu_item_count;

echo <<<EOL
/*lots more boring css */

div#wrapper {
  text-align: left;
  width: $wrapper_width px;
}
EOL;
?>

To solve your problem, can you post the output of that file?

St. John Johnson
+1  A: 

Check the output from your php /css script. You can do this by entering the address to your script in a browser, and if you have set up apache correctly, the results of your php calculations should be there. My guess is that the division operator may return some decimal number which is not compatible with css. Try this:

$menu_item_width = (int)$wrapper_width / $menu_item_count;
Tewr
So ok, sorry to all that i didnt describe it as good as i shoul;d. and i shouldve tought of checking the css output myself *facepalm* this: `$menu_item_width = (int)$wrapper_width / (int)$menu_item_count;` outputs: ` <br /><b>Warning</b>: Division by zero in <b>C:\xampp\htdocs\hanseclub-limburg.nl\docs\css\css-base.css</b> on line <b>5</b><br />`So something goes wrong while getting the $request, ill try $post in a sec. Because the menu_item_count in my header.php echoes 5. as it should
Quaze
A: 

Ok sorry for bothering everyone. Very Very Very stupid mistake on my behalf.

This is what went wrong:

I added the query string to the wrong CSS file >.< Nothing wrong with the division operator or anything, just my stupid mind not working to well from staring at my monitor to long ;P

Thanks for the help!

Quaze