Using PHP to change CSS element values?

Can PHP be used in a similar way (or rather, to achieve a similar end result) as javascript that is used to alter CSS element values? For example, using PHP to achieve the same result as this does (in this case, changing the display style of a specific element to ‘none’):


document.getElementById('Menu'+Menu[i]).style.display = 'none';

i don’ tsee any problems. you can also use dynamic style sheets like

style_sheet.php


<?php
 header('Content-Type: text/css');
?>
.text{color:black;}

this way you can also store user preferneces in database and then retirve them and use them in one style sheet which is dynamically changing with each diffferent user. something like
style_sheet.php


<?php
 header('Content-Type: text/css');
// mysql_connect() here
// mysql_query() here
$color = $row['color'];

?>
.text{color:<?php echo $color;?>;}

i saw PHPMyAdmin doing that and i like it.(not the database part)

hope this helps

I don’t think that’s quite what I am looking for, but maybe I am misunderstanding it somewhat. :slight_smile:

The javascript that I was looking to replace changes the style of a specific element on certain pages, depending on their location. So, if the user loads up a page in /Folder1, they get a different result than if they load up a page in /Folder2.

More specifically, I need to load a specific menu depending on where on the site the user is. I have an array in PHP that I use to determine the location of the page, and to set a variable that is used to identify the location. My idea was to have an if statement for each menu, which in the case that the variable is set to the right value, would change the display for that specific menu from none to block.

Currently, this display change is done through a combination of PHP and javascript, but I think that all PHP would be a bit faster and neater.

if you want to get that elelment by elementId then you can do it with JavaScript NOT with PHP because php is server side and cannot change elements by IDs on client side. so, you have to use combination of JavaScript and PHP. In this case yo uwill generate JavaScript with PHP, so you’ll get teh power of both. somthing like


<?php
  $menu[0] = 'something';

?>
document.getElementById('Menu'+<?php echo $menu[0];?>.style.display = 'none';

hopefully i am right this time.

Ah, I see. I had hoped there was some way (not necessarily with elementID – any solution that changes the display from none to block would work) I could do it entirely in PHP, because the PHP+javascript solution isn’t ideal. It takes a bit too long for the menu to load, plus the script has to be inserted into each page rather than stored in an external .js file.

Thanks for explaining. :slight_smile: