inserting php within an html tag

Discussion in 'PHP' started by pitto, Feb 10, 2008.

  1. #1
    Is the following code legitimate?

    <li<?php if ($page_identity == "About Us")
    echo " id=\"currentpage\""; ?>>
    <a href="AboutUs.php" title="...">About Us</a>
    </li>

    I.e. can I insert the php code within the <li> tag?

    It seemed to work before but now by text editor (BBedit) doesn't seem to be recognizing it and this is confirmed when I run the code in my browsers.

    Pitto
     
    pitto, Feb 10, 2008 IP
  2. pitto

    pitto Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Also, must I escape the ">" character in order to print it within an echo statement?

    Take my previous example,
    <li<?php if ($page_identity == "About Us")
    echo " id=\"currentpage\""; ?>>
    <a href="AboutUs.php" title="...">About Us</a>
    </li>

    Could I add a ">" like this(see bold, underlined >),
    <li<?php if ($page_identity == "About Us")
    echo " id=\"currentpage\">About Us"; ?>>
    <a href="AboutUs.php" title="...">About Us</a>
    </li>

    or do I need to escape it with a "\"?

    If this isn't allowed it could be my problem.

    Thanks again,
    Pitto
     
    pitto, Feb 10, 2008 IP
  3. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #3
    Yes you can drop into PHP. And no you don't have to escape the >. If you did escape the output would be:
    About Us> About Us .
    Problem must be somewhere else.
     
    shallowink, Feb 10, 2008 IP
  4. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Why not simply echo the '>' as well?

    <li<?php if ($page_identity == "About Us") {
    echo " id=\"currentpage\"" . ">"; } ?>
    <a href="AboutUs.php" title="...">About Us</a>
    </li>

    Try that.
     
    dwayne12, Feb 10, 2008 IP
  5. jasondavis

    jasondavis Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Another easiar to read way

    
    <?PHP
    if ($page_identity == "About Us") {
    	$id = 'id="currentpage"';
    }else{
    	$id = ''
    }
    ?>
    <li <?php echo $id;?>>
    <a href="AboutUs.php" title="...">About Us</a>
    </li>
    
    PHP:
     
    jasondavis, Feb 10, 2008 IP
  6. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Jason in your example you forgot to add the id attribute for the li tag.

    It should be:
    <li id=<?=$id;?>>
     
    dwayne12, Feb 12, 2008 IP
  7. pitto

    pitto Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks guys. That got it working.
     
    pitto, Feb 29, 2008 IP