Use of CSS within PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CutAndPaste
Forum Newbie
Posts: 7
Joined: Thu Jun 30, 2005 10:57 am

Use of CSS within PHP

Post 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/\&quote;mypage.php?cpane=menu1\&quote;
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

&lt;a href=&quote;mypage.php&quote; class=&quote;leftnavlv1&quote;&gt;Home&lt;/a&gt;
which works fine and picks up the CSS style properly.

What am I doing wrong?

thanks.
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post 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>';
CutAndPaste
Forum Newbie
Posts: 7
Joined: Thu Jun 30, 2005 10:57 am

Post 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
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Code: Select all

&quote;<a href=\&quote;mypage.php?cpane=calendar-item1\&quote;>Item1</a>&quote;
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=&quote;mypage.php?cpane=calendar-item1&quote;>Item1</a>'
remove slashes everywhere else, as tores said, you don't need them
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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:

Code: Select all

&lt;br /&gt;
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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 :wink:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

better yet, use heredoc for something like that....
CutAndPaste
Forum Newbie
Posts: 7
Joined: Thu Jun 30, 2005 10:57 am

CSS display in php still not right :-(

Post 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=&quote;text/css&quote;>
<!--
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! :evil:

Thanks.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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:

Code: Select all

<br />
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> ?
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Re: CSS display in php still not right :-(

Post 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

Code: Select all

<p>blah</p><p>blah2</p>
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.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

And Andre, personally, I don't like BRs. And also, wrong choice of words, but still with the same outcome :P

http://dumbass.ionichost.com <- my website (not a senseless plug), it doesn't use BRs (that I know of ^_^)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: CSS display in php still not right :-(

Post 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

Code: Select all

<p>blah</p><p>blah2</p>
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. :)
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Shut up you <_< lol.
Post Reply