PHP tag as a DIV id

Discussion in 'PHP' started by onslaughtmusic, Nov 22, 2010.

  1. #1
    I have a situation where I need the ID of a div tag to change according to the name of the last button a user clicked. I use this code:

    <div id="<?php echo $last_selected;?>" class="align_left">
    Code (markup):
    This works fine and seems to function in all browsers and so forth but W3C validator doesn't seem to agree and gives me a :

    syntax of attribute value does not conform to declared value
    <div id=" " class="align_left">
    ✉
    The value of an attribute contained something that is not allowed by the specified syntax for that type of attribute. For instance, the “selected” attribute must be either minimized as “selected” or spelled out in full as “selected="selected"”; the variant “selected=""” is not allowed.

    I was wondering if there's a better way to do this? Haven't been able to find an answer anywhere and was hoping someone can make a suggestion.

    Thanks
     
    onslaughtmusic, Nov 22, 2010 IP
  2. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    
    <?php
    if(isset($last_selected) && !empty($last_selected))
    {
    ?>
    <div id="<?php echo $last_selected;?>" class="align_left">
    <?php
    }
    else
    {
    ?>
    <div class="align_left">
    <?php
    }
    ?>
    PHP:
    There is lots of room for improvement of this code, but I have show you that what you can do to remove the error you are getting.

    Best of luck..
     
    xpertdev, Nov 22, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    Based on @xpertdev if there's no value for $last_selected it will echo id="1", of course replace 1 with a desired value

    <div id="<?php if(empty($last_selected)){echo $last_selected="1";}else{echo $last_selected;}?>" class="align_left">
    PHP:
     
    MyVodaFone, Nov 22, 2010 IP
  4. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @MyVodaFone : Yes I have already mentioned that there are lots of room for improvement. I was just pointing him in right direction....
     
    xpertdev, Nov 23, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    Yes, I know good job :)...
     
    MyVodaFone, Nov 23, 2010 IP
  6. onslaughtmusic

    onslaughtmusic Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for all the info, the code example doesn't suit my purpose but just at a glance it made me realize that indeed the original value is empty! DOH!! Sometimes you just need a 2nd pair of eyes to see the obvious. Thank you for the input.

    I decided to reassess my approach and changed all my unique IDs to class, and set a default class to each one, solved my problem and even shrunk my css file a bit.
    Again thank you for the help.
     
    onslaughtmusic, Nov 24, 2010 IP