SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Wasteland
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    giving an active link a specific color

    I want to give an active category/link a specific color...

    the following script works fine.

    PHP Code:
    <a href="index.php?cat=1"<?if($cat==1) echo ' style="color:#EC008C;"';?>">category1</a>

    <a href="index.php?cat=2"<?if($cat==2) echo ' style="color:#EC008C;"';?>">category2</a>

    etc. ...
    I would like to load the categories from a db....
    PHP Code:
    ....
    while ( 
    $array mysql_fetch_assoc($result) ) {
    echo 
    "&nbsp;|&nbsp;<a href=index.php?cat=" $array["ID"] . ">" $array["Name"] . "</a>&nbsp;|&nbsp;";
    .... 

    That's what I tried

    PHP Code:
    ....
    while ( 
    $array mysql_fetch_assoc($result) ) {
    echo 
    "&nbsp;|&nbsp;<a href=index.php?cat=" $array["ID"] . if($cat==$array["ID"]) ' style="color:#EC008C;"'">" $array["Name"] . "</a>&nbsp;|&nbsp;";
    .... 

    I tried to combine the two snippets, but I got errors all the time.
    Any suggestions?

  2. #2
    SitePoint Wizard bronze trophy JRMillion's Avatar
    Join Date
    Apr 2004
    Location
    Arlington VA
    Posts
    2,094
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put quotes around the page you are linking to

    you also have other errors in that statement, you cant do an if inside an echo (at least I dont think you can).

    this should work

    Code:
    &nbsp;|&nbsp;<a href=index.php?cat=<?php echo $array['ID']; if($cat==$array['ID']) echo 'style="color:#EC008C;";'?>><?php echo $array['Name'];?></a>&nbsp;|&nbsp;
    note: this line should not be surround by php tags

  3. #3
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Wasteland
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    thank you.

  4. #4
    SitePoint Wizard bronze trophy JRMillion's Avatar
    Join Date
    Apr 2004
    Location
    Arlington VA
    Posts
    2,094
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem. you could also do it the way you had it before, but you would need to format it a little differently. You had quotes left out and there was the if inside echo problem.

    good luck.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •