1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with PHP if/if/else

Discussion in 'PHP' started by trenttdogg, Mar 24, 2013.

  1. #1
    I need help writing an if/else statement. I have a piece of code(below) that displays the price of my product on the page. What I want to do is add a statement that basically says if the price is zero, then display "Free" rather than $0.00
    Any help is appreciated:)
    Code (Text):
    1. <div class="price">
    2.         <?php if (!$product['special']) { ?>
    3.         <?php echo $product['price']; ?>
    4.         <?php } else { ?>
    5.         <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
    6.         <?php } ?>
    Thanks is advance
    trenttdogg, Mar 24, 2013 IP
  2. IGarret

    IGarret Active Member

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    51
    #2
    Just add this.
    PHP:
    1. <?php } else if ($product['price'] == '0.00') { ?>
    2.     echo 'Free';
    3. <?php } else { ?>
    4. ...
    IGarret, Mar 25, 2013 IP
  3. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    826
    Best Answers:
    144
    Trophy Points:
    395
    #3
    STOP OPENING AND CLOSING PHP ON EVERY BLASTED LINE FOR NO GOOD REASON!!!!!!!!! (actually, that needs more exclamation points)

    Seriously, why the blue blazes do people do that?!?

    As to your "problem", I'd lose the if statements for inline evaluation, and since pretty much all those output values need the same check, I'd make a function to handle the 0 or free.

    Code (Text):
    1. // assumes you are already in PHP.
    2.  
    3. function freeCheck($price) {
    4.     return $price > 0 ? $price : 'free';
    5. }
    6.  
    7. echo '
    8.     <div class="price">
    9.         ', $product['special'] ?
    10.         '<span class="price-old">'.freeCheck($product['price']).'</span>
    11.         <span class="price-new">'.freeCheck($product['special']).'</span>' :
    12.         freeCheck($product['price']),'
    13.     </div>';
    I closed the DIV, but that was just a guess. There is NO reason to be constantly opening and closing PHP like that -- which is how the taffer before this post's version is 100% gibberish since the echo isn't even inside PHP. Now I know why I left the keepers.
    Last edited: Mar 25, 2013
    deathshadow, Mar 25, 2013 IP
  4. wiicker95

    wiicker95 Active Member

    Messages:
    437
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    75
    #4
    Because the vast majority of people doesn't know how to escape the quote marks within php, so they open and close it at every single statement. You forget a backslash, your php goes "Syntax Error: Unexpected """ at the line XX". But still, it's not an excuse.

    (actually, it says "Parse error: syntax error, unexpected T_STRING in __ on line XX", but you get the idea.)
    Last edited: Mar 25, 2013
    wiicker95, Mar 25, 2013 IP
  5. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    826
    Best Answers:
    144
    Trophy Points:
    395
    #5
    True enough, with all the halfwits using double quotes for everything -- when if you use singles, you only need to escape singles -- which since you don't need singles in your markup, the only place they should occur is in content... aka something PHP should be plugging into the markup from a variable anyways.
    deathshadow, Mar 25, 2013 IP
  6. Strider64

    Strider64 Member

    Messages:
    40
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    25
    #6
    I must be a halfwit then...:)
    PHP:
    1. $this->thread_box .= "<a class=\"btn_comment_style\" href=\"delete.comment.php?title=" . $row['title'] . "&page=" . $row['thread'] . "\" onclick=\"return confirm('Are you sure you want to delete this comment?');\">Delete</a>";
    BTW escaping single quotes doesn't work if you have javascript in the string ;)
    Strider64, Mar 25, 2013 IP
  7. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    826
    Best Answers:
    144
    Trophy Points:
    395
    #7
    Uhm, since when? Unless you misunderstood what I meant by using single quotes for the PHP and thought I meant for the HTML attributes, which is also stupid.

    Code (Text):
    1. $this->thread_box .= '
    2.     <a
    3.         class="btn_comment_style"
    4.         href="delete.comment.php?title=' . $row['title'] . '&page=' . $row['thread'] . '"
    5.         onclick="return confirm(\'Are you sure you want to delete this comment?\');"
    6.     >Delete</a>';
    Far, FAR simpler to deal with, since you only need to escape two singles instead of every double. I also cleaned it up to be easier to ready by not stuffing everything on one painful to follow line.

    NOT that I believe in using the onevent methods in the markup -- firm believer scripting should be self attaching, not set using attributes, but that's me... Just as I think <?php ?> should be stricken from PHP completely and STYLE should be obsolete as a tag / deprecated as an attribute, I also think the onevent attributes should be deprecated.
    deathshadow, Mar 25, 2013 IP