I only want the CSS if the PHP is true (HELP!!)

Discussion in 'PHP' started by Jim bob 9 pants, Jan 9, 2007.

  1. #1
    Hi

    Currently I am using this

    <span class="bits"><?php if ($row_rs_newprop['beds'] > 0){echo $tag2, $row_rs_newprop['beds'],", ";} ?></span>

    and of course the span still shows, how do I make it so the css wont show at all if the result is negative.

    Hope this makes sense

    Jamie
     
    Jim bob 9 pants, Jan 9, 2007 IP
  2. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this:

    
    <?php
    
    if ($row_rs_newprop['beds'] > 0) {
      print "<span class=\"bits\">";
      echo $tag2, $row_rs_newprop['beds'],", ";
      print "</span>";
    } 
    
    ?>
    Code (markup):
     
    daboss, Jan 9, 2007 IP
  3. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #3
    Just set the variables in an if statement before you echo them:
    
    <?php 
    if ($row_rs_newprop['beds'] > 0) {
       $tag2 = ;// whatever you want it to equal;
       $class = "bits";
    } ?>
    <span class="<?=$class;?>"><?= $tag2; ?></span>
    
    Code (markup):
     
    frankcow, Jan 9, 2007 IP
  4. Jim bob 9 pants

    Jim bob 9 pants Peon

    Messages:
    890
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you both, PERFECT
     
    Jim bob 9 pants, Jan 9, 2007 IP
  5. ConnorWilson

    ConnorWilson Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to do it simpler, you could do this.
    
    <span class="<? echo ($row_rs_newprop['beds'] > 0) ? "bits" : "" ?>"></span>
    
    PHP:
    Thats using ternary operators :)
     
    ConnorWilson, Jan 9, 2007 IP
  6. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #6
    that doesn't make any sense in this case, there's no alternate or 'else' condition, so a ternary operator is unnecessary
     
    frankcow, Jan 10, 2007 IP
  7. ConnorWilson

    ConnorWilson Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It makes perfect sense. It uses less code than all the other examples. A false result isn't entirely necessary, but it gets the job get more efficiently and faster.

    Using a whole if statement and putting it to a var, then echoing the var, now thats unnecessary ;)
     
    ConnorWilson, Jan 10, 2007 IP