Page 1 of 1
Use of CSS within PHP
Posted: Tue Jul 12, 2005 6:35 am
by CutAndPaste
Can anyone tell me the proper way to put in CSS info into a php echo? Not sure if this should go here or in CSS forum so please move if wrong.
I've got a little php expanding menu system which the links below are part of.
My PHP Menu
Code: Select all
echo '<a href=\"mypage.php?cpane=calendar\">Calendar</a>';
echo '<br><span class="leftnavlv1">',"<a href=\"mypage.php?cpane=calendar\"></a>",'</span>';
echo '<br><span class="leftnavlv1"><a href=\mypage.php?cpane=calendar">Item 1</a></span>';
if(isset($_GET['cpane']) && preg_match('/^calendar/',$_GET['cpane']) ){
//Show sub menu
echo '<span class="leftnavlv1"><br><img src="images/bullet.gif" width="6" height="11">',"<a href=\"mypage.php?cpane=calendar-item1\">Item1</a>",'</span>';
My CSS (part of)
Code: Select all
a:link,a:active,a:visited { color : #000000; }
a:hover { text-decoration: underline; color : #757978; }
hr { height: 0px; border: solid #D1D7DC 0px; border-top-width: 1px;}
.leftnavlv1 {
FONT-WEIGHT: bold; FONT-SIZE: 10px; COLOR: #333333; FONT-FAMILY: Verdana, Geneva, Arial, helvetica, sans-serif; TEXT-DECORATION: none
}
.leftnavlv1:hover {
TEXT-DECORATION: underline
The code below is picking up the plain page link style and shows an underline on the link:
Code: Select all
echo '<a href=\"mypage.php?cpane=menu1\">Item 1</a>"';
BUT it mangles the URL which shows as:
Code: Select all
mydomain.com/\"e;mypage.php?cpane=menu1\"e;
The code below is picking up the leftnavlv1 style BUT is showing it with an underline all the time. The leftnavlv1 style does not show an underline elsewhere on the page, only when used as above.
Code: Select all
echo '<br><span class="leftnavlv1">',"<a href=\"mypage.php?cpane=menu1\">Item 1</a>",'</span>';
Elsewhere on the page I've got:
Code:
Code: Select all
<a href="e;mypage.php"e; class="e;leftnavlv1"e;>Home</a>
which works fine and picks up the CSS style properly.
What am I doing wrong?
thanks.
Posted: Tue Jul 12, 2005 6:41 am
by tores
You don't need the slash before the quotes...
Change
Code: Select all
echo '<a href=\"mypage.php?cpane=calendar\">Calendar</a>';
to
Code: Select all
echo '<a href="mypage.php?cpane=calendar">Calendar</a>';
Posted: Tue Jul 12, 2005 7:28 am
by CutAndPaste
thanks for the reply,
I think the slashes are required to make the PHP menu to work properly, when I remove them I get this error:
Code: Select all
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/myusername/public_html/myfile.php on line 26
Posted: Tue Jul 12, 2005 7:45 pm
by andre_c
Code: Select all
"e;<a href=\"e;mypage.php?cpane=calendar-item1\"e;>Item1</a>"e;
this is the only place you need slashes since you're using double quotes to echo this string...
or you could change it into this
Code: Select all
'<a href="e;mypage.php?cpane=calendar-item1"e;>Item1</a>'
remove slashes everywhere else, as tores said, you don't need them
Posted: Wed Jul 13, 2005 12:02 am
by theda
I have a question for the thread creator... Why do you echo each and every line... You could save yourself some time by printing a bunch of lines...
Code: Select all
print '
<a href=\"mypage.php?cpane=calendar\">Calendar</a><br>
<span class="leftnavlv1">
<a href=\"mypage.php?cpane=calendar\"></a>
</span>
<span class="leftnavlv1">
<a href=\mypage.php?cpane=calendar">Item 1</a>
</span>';
if(isset($_GET['cpane']) && preg_match('/^calendar/',$_GET['cpane']) ){
//Show sub menu
print '<span class="leftnavlv1"><br><img src="images/bullet.gif" width="6" height="11">';
} else { print 'blah blah blah';
I don't know if you can still use the ' and not have to put \ before all quotes.
Also a great suggestion for programming: Avoid BR! If you have to use BR, make sure to escape it, by doing:
Posted: Wed Jul 13, 2005 12:07 am
by harrisonad
Code: Select all
$url = "mypage.php?cpane=calendar";
echo "
<a href='$url'>Calendar</a><br>
<span class='leftnavlv1'>
<a href='$url'></a>
</span>
<span class='leftnavlv1'>
<a href='$url'>Item 1</a>
</span>";
just remove redundancies and error potentials

Posted: Wed Jul 13, 2005 12:27 am
by Burrito
better yet, use heredoc for something like that....
CSS display in php still not right :-(
Posted: Wed Jul 13, 2005 5:31 am
by CutAndPaste
andre_c & torres:
Ok, got the menu to function ok with the slashes removed, thanks.
theda
I have a question for the thread creator... Why do you echo each and every line... You could save yourself some time by printing a bunch of lines...
Just learning php and it was a code snippet I picked up...
andre_c
Also a great suggestion for programming: Avoid BR! If you have to use BR, make sure to escape it, by doing:
I'd got the <br> in to make the menu item appear on the next line, otherwise it appeared next to the previous menu item. In this situation is <br /> ok?
harrisonad & theda
I'll try your improved code to dispaly the menu items, thanks.
Anyone/everyone
Thanks for all of the PHP tips

, I'm still left with the problem of my CSS display inconsistency. As a work around I put:
Code: Select all
<style type="e;text/css"e;>
<!--
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
-->
</style>
in the top of the php include page which did the trick, but of course this throws out other links which I want to show as underlined!
Thanks.
Posted: Wed Jul 13, 2005 11:45 am
by andre_c
theda wrote:
Also a great suggestion for programming: Avoid BR! If you have to use BR, make sure to escape it, by doing:
huh?
<br> has nothing to do with programming...
<br> vs <br /> is a choice between HTML an XHTML, nothing wrong with either one...
using the / means closing the tag, not escaping it
why do you recommend avoiding <br> ?
Re: CSS display in php still not right :-(
Posted: Wed Jul 13, 2005 3:32 pm
by theda
Hey CutAndPaste, if you ever want your code cleaned up, just PM me and I'd be glad to help you. My expertise is mostly novice PHP smf intermediate XHTML 1.1. My website is 100% valid XHTML 1.1 and CSS2 (Minus my hosts adverts that cause invalidation) and I also code in PHP as a base for my website (with HTML implanted into it...sort of like what you're doing right now).
And also: There's a little nifty thing called <p> that you can use in HTML (I use it and it helps make my site simpler coding while being valid), that if you do
Blah and Blah 2 will be on seperate lines. BR is just as easy as using P, but I think BR isn't valid in XHTML 1.1 or XHTML 1.0 Strict.
Posted: Wed Jul 13, 2005 3:33 pm
by theda
And Andre, personally, I don't like BRs. And also, wrong choice of words, but still with the same outcome
http://dumbass.ionichost.com <- my website (not a senseless plug), it doesn't use BRs (that I know of ^_^)
Re: CSS display in php still not right :-(
Posted: Wed Jul 13, 2005 5:53 pm
by Roja
theda wrote:
And also: There's a little nifty thing called <p> that you can use in HTML (I use it and it helps make my site simpler coding while being valid), that if you do
Blah and Blah 2 will be on seperate lines. BR is just as easy as using P, but I think BR isn't valid in XHTML 1.1 or XHTML 1.0 Strict.
Several comments there:
1. BR is perfectly valid in both XHTML 1.0 and 1.1, strict and transitional.
2. br/ is easier than p /p p /p - its one tag, instead of FOUR (open/close/open/close) to get the same effect. Technically, you could do p /p or even p/, but that would be an empty paragraph, which while valid, is semantically meaningless, while br is defined.
3. Speaking of semantic meaning, a line break should be a br. Thats exactly what its defined as. p's should be used for actual paragraphs. There are times when you will want two spaces between paragraphs, in which case br is right, and p/end-p is wrong.
4. You are entirely welcome to prefer p's to br's. It doesnt change br's into invalid code.

Posted: Wed Jul 13, 2005 8:11 pm
by theda
I could have sworn xhtml 1.1 got rid of br...
Edit: Meh, oh well. Still prefer P, makes my site look nicer

And I can control it with CSS.
Posted: Wed Jul 13, 2005 8:19 pm
by Roja
theda wrote:Still prefer P, makes my site look nicer
Now hang on. You were just arguing they were interchangable, now you are arguing they are different?

You were right the first time. They are interchangable. The only difference is under the hood.
theda wrote:And I can control it with CSS.
You can control and shape BR's with css as well.
Posted: Wed Jul 13, 2005 11:43 pm
by theda
Shut up you <_< lol.