Hi,

I've just begun learning PHP and have purchased books etc and researched on websites different areas of the language however I've noticed that there is not much information available relating to the actual appearances of websites through PHP.

I know the standard way is using HTML and CSS but i was wondering how can you actually change the HTML and CSS features within the PHP code. I know i am not being very specific - just looking for some tips or any websites associated to altering website appearances within the PHP code itself (an example could be to change the text colour or font of a certain paragraph if a variable is changed?)

Any help will be much appreciated, thanks.

You can, dynamically output anything that will end up in the final HTML document, including style tags or style attributes to tags. For example, this is commonly done to achieve "Zebra Striping" in table rows.

//...
$ctr = 0;
foreach($dbResults as $record) {
   $rowColor = $ctr %2 ? '#cfc' : '#fff';
   echo '<tr style="background-color:"'.$rowColor.'">';
   // .. output columns here.
   echo '</tr>';
   $ctr++;
}

PHP is almost never used to alter a website's appearance. It's designed to run in the background, before the HTML gets outputted.

The only way I know in which PHP does change a website's appearance, is for example when forum posts are retrieved from a database and outputted to the screen - that's HTML output (like madCoder said).

Yeah, i know it's not used on a major scale for the appearance. Just wanted to find out if you can change minor parts for instance, changing the background colour of a website for each different day of the week (which i guess from what you guys have said can be done with maybe a date variable in the database and PHP code that checks the condition and outputs the background colour based on it similar to madCoder's snippet above).

Is there any way to change the appearances of just a paragraph of writing e.g. with id p1, instead of changing all text?

Thanks for the help so far guys :)

put the css in the css file, class for a multi-member group, id for a singleton group
in php, however the class/id is determined
very similar to madCoder's above

<!-- boring html -->
<div id='<?php isset($special_condition) ? "special_id" : "boring_id"; ?>'> blah </div>
<div class='<?php isset($special_condition2) ? "special_class" : "boring_class"; ?>'> RULE 34 </div>
<!-- boring html -->

isset($special_condition) is of course any date, variable, size, _get _post value, file_exist(), mysql(anything), any combination of anything, that can be examined to compare data with any choice of comparables, not just isset()

Ah right, i'm sort of getting the idea of it now, i'll take a deeper look into isset, thanks for the help.

Member Avatar for iamthwee

You can actually use a .php file instead of a .css which you would normally call.

This allows you to dynamically change the variables. The css file normally controls the styling and positioning of all your content.

All you would do is add a special parameter to the top of the php file so the import reads it as a .css style sheet.

I think the following link is a good example:

http://davidwalsh.name/css-variables-php-dynamic

Thanks :) i've just had a look through, i don't understand some parts of the code as i'm not that experienced with PHP yet but i can see the concept- bookmarked the link for future reference, thanks!

I think you should go with the video tutorials which are easily available online and you can get more information from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.