473,597 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to change css id in span element?

How to change a link's color based on a php variable?

I have a number of links on a page:

<p><a href="page1.php ">Page 1</a></p>
<p><a href="page2.php ">Page 2</a></p>
<p><a href="page3.php ">Page 3</a></p>
<p><a href="page4.php ">Page 4</a></p>

I want the link to be red if $nav has a value that corresponds to the page
number: If $nav = 1, then the page1.php link should be red; if $nav = 2,
then the page2.php link should be red, and so on.

This is how I'd normally code the html to make a red link:

<p><span id="red"><a href="page1.php ">Page 1</a></span></p>

But how do I check the value of $nav and insert the appropriate span id for
each link? Can I somehow make the span id a variable? How to I jump in and
out of php within a tag?

If $nav does not correspond with the link's page number, then the link
should remain the default color - which is blue:

<p><span id="blue"><a href="page1.php ">Page 1</a></span></p>

Any help is appreciated. Thanks in advance.
Jul 17 '05 #1
7 6003
On Wed, 04 Aug 2004 01:58:30 +0000, deko wrote:
How to change a link's color based on a php variable?

I have a number of links on a page:

<p><a href="page1.php ">Page 1</a></p>
<p><a href="page2.php ">Page 2</a></p>
<p><a href="page3.php ">Page 3</a></p>
<p><a href="page4.php ">Page 4</a></p>

I want the link to be red if $nav has a value that corresponds to the page
number: If $nav = 1, then the page1.php link should be red; if $nav = 2,
then the page2.php link should be red, and so on.

This is how I'd normally code the html to make a red link:

<p><span id="red"><a href="page1.php ">Page 1</a></span></p>

But how do I check the value of $nav and insert the appropriate span id for
each link? Can I somehow make the span id a variable? How to I jump in and
out of php within a tag?

If $nav does not correspond with the link's page number, then the link
should remain the default color - which is blue:

<p><span id="blue"><a href="page1.php ">Page 1</a></span></p>

Any help is appreciated. Thanks in advance.

alt.comp.homewo rk -----------> [this way]
I've only been back reading this group again for a week, and so far,
you've asked enough basic questions here for people to have written almost
your entire project. How about using php.net or google rather than being
spoon fed?

I'm all for helping where I can, but there's 'help', and there's 'do it
for me'.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2
I noticed that Message-ID: <ap************ @newssvr27.news .prodigy.com>
from deko contained the following:
This is how I'd normally code the html to make a red link:

<p><span id="red"><a href="page1.php ">Page 1</a></span></p> id should be used where the element is unique. Otherwise use class. And
why have a link to itself?
But how do I check the value of $nav and insert the appropriate span id for
each link? Can I somehow make the span id a variable? How to I jump in and
out of php within a tag?
You need to adopt a more holistic approach to this one. :-)

If $nav does not correspond with the link's page number, then the link
should remain the default color - which is blue:

<p><span id="blue"><a href="page1.php ">Page 1</a></span></p>

I wouldn't use <span> here. Define your link colours in CSS. a:link,
a:visited etc. Again use a class if you want these links to be
different.

Check this out:

<?php
$links="";
$nav=$_SERVER['PHP_SELF'];
$pages=4;
for($i=1;$i<=$p ages;$i++){
if(strpos($nav, "$i")===fal se){
$links.="<a href=\"page$i.p hp\">Page $i</a><br>";
}
else{
$links.="<span style=\"color: red\">Page $i</span><br>";
}
}

?>
Save and include in all pages. echo $links wherever you need it.
Making it dynamic by counting the number of pages in the directory is
left as an A* exercise...
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
I noticed that Message-ID:
<pa************ *************** *@bubbleboy.dig iserv.net> from Ian.H
contained the following:
I've only been back reading this group again for a week, and so far,
you've asked enough basic questions here for people to have written almost
your entire project. How about using php.net or google rather than being
spoon fed?

I'm all for helping where I can, but there's 'help', and there's 'do it
for me'.


Ah, but if we do he'll probably fail because the tutor won't believe
it's his own work >:-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4
> You need to adopt a more holistic approach to this one. :-)

Thanks for the reply Geoff. I'd say what I came up with is pretty holistic
:)

I put the entire nav list in a separate php file and then just used <?
include $leftnav; ?> in the page. Now the nav list is centralized and
easier to maintain. I had to send a variable with the url to process the
color changes - which work for named anchors in the same page (I thought I
would need JavaScript to do this).

I read somewhere that I should strip any html from $_SERVER['QUERY_STRING']
before using it... not sure about this...

<?php
function linkcolor($arg) {
$nav = $_SERVER['QUERY_STRING'];
if ( $arg == $nav )
{
return "grnlink";
}
else
{
return "blulink";
}
}
echo
"<p><span id=".linkcolor( "home").">< a href=index.php? home>Home
Page</a></span></p>
<p><span id=".linkcolor( "contact"). "><a
href=contact.ph p?contact>Conta ct</a></span></p>
<p><span id=".linkcolor( "services").">< a
href=services.p hp?services>Pro fessional Services</a></span></p>
<p><span id=".linkcolor( "software").">< a
href=software.p hp?software>Sof tware</a></span></p>
<p><span id=".linkcolor( "projmgt"). "><a href=services.p hp?projmgt>Proj ect
Management</a></span></p>
<p><span id=".linkcolor( "netprog"). "><a href=tech.php?n etprog>.NET
Programming</a></span></p>
<p><span id=".linkcolor( "viscount").">< a
href=stats.php? viscount>Viscou nt</a></span></p>
<p><span id=".linkcolor( "oxp")."><a href=software.p hp?oxp>Organize
XP</a></span></p>
<p><span id=".linkcolor( "tech01")." ><a href=tech.php?t ech01#tech01>Te ch
01</a></span></p>
<p><span id=".linkcolor( "tech02")." ><a href=tech.php?t ech02#tech02>Te ch
02</a></span></p>
<p><span id=".linkcolor( "tech03")." ><a href=tech.php?t ech03#tech03>Te ch
03</a></span></p>"
;
?>

Jul 17 '05 #5
I noticed that Message-ID:
<7X************ *****@newssvr21 .news.prodigy.c om> from deko contained the
following:
You need to adopt a more holistic approach to this one. :-)


Thanks for the reply Geoff. I'd say what I came up with is pretty holistic


My way was /much/ neater. :-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6
> My way was /much/ neater. :-)

of course... we all like our own code best :) one problem I'm having with
my (unkempt) code is that I can't call it twice in the same page. For
example, let's say the page has a lot of stuff on it and the height is
2000px or more. The left nav list starts at the top of the page and has
about a dozen items:

this link
that link
other link
etc...

But when viewing content further down on the page, the menu list is not
visible. So I tried this in the leftnav div:

[top of page]
<? include $navlist; ?>
....
<p>nbsp;</p>
<p>nbsp;</p>
....
[middle of page]
<? include $navlist; ?>

The idea is to make the same left nav list available further down on the
page. The problem is that the second attempt to include the script results
in the following error:

Fatal error: Cannot redeclare linkcolor() (previously declared in
/home/clearpoi/public_html/cgi-bin/leftnav.php:2) in
/home/clearpoi/public_html/cgi-bin/leftnav.php on line 2

Is there a way to avoid this? Can I call the script without calling the
function?

Here is the script:

<?php
function linkcolor($arg) {
$nav = trim(strip_tags ($_SERVER['QUERY_STRING']));
if ( $arg == $nav )
{
return "grnlink";
}
else
{
return "blulink";
}
}
echo
"<p><span id=".linkcolor( "home").">< a href=index.php? home>Home
Page</a></span></p>
<p><span id=".linkcolor( "contact"). "><a
href=contact.ph p?contact>Conta ct</a></span></p>
<p><span id=".linkcolor( "services").">< a
href=services.p hp?services>Pro fessional Services</a></span></p>
<p><span id=".linkcolor( "software").">< a
href=software.p hp?software>Sof tware</a></span></p>
<p><span id=".linkcolor( "projmgt"). "><a href=services.p hp?projmgt>Proj ect
Management</a></span></p>
<p><span id=".linkcolor( "netprog"). "><a href=tech.php?n etprog>.NET
Programming</a></span></p>
<p><span id=".linkcolor( "oxp")."><a href=software.p hp?oxp>Organize
XP</a></span></p>
<p><span id=".linkcolor( "tech01")." ><a href=tech.php?t ech01#tech01>Te ch
01</a></span></p>
<p><span id=".linkcolor( "tech02")." ><a href=tech.php?t ech02#tech02>Te ch
02</a></span></p>
<p><span id=".linkcolor( "tech03")." ><a href=tech.php?t ech03#tech03>Te ch
03</a></span></p>"
;
?>
Jul 17 '05 #7
I noticed that Message-ID: <5j************ **@newssvr29.ne ws.prodigy.com>
from deko contained the following:
Is there a way to avoid this? Can I call the script without calling the
function?


Split them up. Include the function once, script many.

However, bear in mind what I said about id. It's supposed to be for
unique elements. Probably won't make much difference though unless you
want to access them with Javascript later on.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
13350
by: Henning Vestergaard Poulsen | last post by:
Hi, I have a problem that I hope someone can help me with. I'm building a web page with pictures I've taken with my digital camera. I have succeded making a javacript that, when clicking on a thumbnail, it changes the main image. Now, I would like to put an explanation (and date and some EXIF-info) to each photo so some text is shown next to the main photo when loaded. I don't know how to change the text without reloading the whole...
6
9558
by: Csaba2000 | last post by:
How do I detect when the font size has been changed (especially by user action: either Ctrl+Scroll wheel or View/Text Size)? This is just for use on IE 5.5+, but it would be great if there was a generic solution. Thanks, Csaba Gabor from New York
2
1523
by: JCO | last post by:
How would you modify this to include font color to be white: <style type="text/css">ul,ol{color:white;}</style> I tried this, but it did not work: <style type="text/css">font color="#FFFFFF",ul,ol{color:white;}</style>
9
6798
by: Wang, Jay | last post by:
Hello, all, I would like to enable some text between <SPAN url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some javascript and it will extract http://www.testserver.com/ from the the span element when I select the whole text in the SPAN and drag it. However, I want to drag it without have to select the words between the span element. The default mouse action will only select the words when i move the mouse. Can...
9
7046
by: developer | last post by:
Does anyone know what is the way IE treats span tags(<span>) and table tags(<tr>, <td>)? Should the <span> tag be encolsed in tds and trs if it placed with other elements that are in a table? Can the span tag itself contain table tags within it? I have some scripting code and when I wrap the span in table elements it does not find the html within the span. Here is an example.... <tr><td colspan="4" align="left"><span...
3
4683
by: mscir | last post by:
I'm adding text to a div using innerHTML, and watching the width of the div using offsetWidth. In IE the offsetWidth increases when the div gets wider, but in Netscape 7.2 or Firefox 1.0.3 it doesn't. Is there a way I can find when a div increases its width in non-IE browsers? TIA, Mike
7
2083
by: David | last post by:
Hi, I'd really appreciate any help. Im trying to change a label over and over again. I can change it the first time, by using the following code, however it never works again, how can i do this so it uses the same element name? This is driving me insane. On the second call to var spanElm = document.getElementById("FirstNameLengthLabel"); spanElm is set to NULL. <script language=javascript>
2
2558
by: Lorenzo Thurman | last post by:
I would like to have an element, a text string, change into a select when a mouseover occurs and then change back to text when a selection is made or when a mouse out occurs. I looked a t using dojo for this, http://dojotoolkit.org, but this will end up on a production server, where dojo is not installed and cannot be installed. So, I need another solution. Can someone give some pointers on how this might be done? TIA
6
9276
by: silly putty | last post by:
hello. i have a <tablewith each cell having its own unique ID (see below). What i want to do is change the CLASS attribute for the SPAN in one of the cells. i'm currently testing this in IE 6. In my javascript, i wrote document.getElementById('0_1').getElementsByTagName("span").setAttribute("className", "hasEvents"); However, i get the following error message:
0
8263
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2393
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1492
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.