PDA

View Full Version : Applying CSS to PHP



jc_gmk
05-18-2007, 03:08 PM
If I want to use PHP to print some HTML, can I apply a class to it from CSS?

e.g.


<?php
print "
<table border cellpadding=3>
<tr>
<td class="tdclass">Type:</td>
<td>Name:</td>
<td>Address:</td>
<td>Telephone:</td>
</tr>";
?>


Thanks

jc_gmk
05-18-2007, 03:23 PM
Just solved it.

Had to put a \ before the "

doh!

mwinter
05-18-2007, 03:24 PM
I'm not entirely certain what you're asking. Are you trying to find out if a rule involving .tdclass would be applied? The answer's yes, and you could have just tried something simple (like changing text colour) to find out for yourself.

Rather than using print or echo to output large amounts of literal text, just drop out of PHP mode:



/* ... */
?>
<table border cellpadding=3>
<tr>
<td class="tdclass">Type:</td>
<td>Name:</td>
<td>Address:</td>
<td>Telephone:</td>
</tr>
<?php
/* ... */

There's an error in the table start-tag, though the border attribute should be removed entirely and implemented using CSS. In addition, "tdclass" isn't a very good class name. Choose something that conveys what the class represents, rather than how it looks or what element it gets used with.

alexjewell
05-19-2007, 12:19 AM
One way to escape using the slash is to use single quotes for the php:



<?php
print '
<table cellpadding="3">
<tr>
<td class="tdclass">Type:</td>
<td>Name:</td>
<td>Address:</td>
<td>Telephone:</td>
</tr>';
?>


I also agree with Winter on two points. One, the border should be declared in the CSS. Two, if there's no PHP used in that block of HTML, drop out of PHP. Also, he has a point with the class name. But I'm not innocent of using vague class names, either, so I wont shove the idea down your throat. But it's smart to name it something more specific.