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:)
    <div class="price">
            <?php if (!$product['special']) { ?>
            <?php echo $product['price']; ?>
            <?php } else { ?>
            <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
            <?php } ?>
    Code (markup):
    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 } else if ($product['price'] == '0.00') { ?>
        echo 'Free';
    <?php } else { ?>
    ...
    PHP:
     
    IGarret, Mar 25, 2013 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #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.

    // assumes you are already in PHP.
    
    function freeCheck($price) {
    	return $price > 0 ? $price : 'free';
    }
    
    echo '
    	<div class="price">
    		', $product['special'] ?
    		'<span class="price-old">'.freeCheck($product['price']).'</span>
    		<span class="price-new">'.freeCheck($product['special']).'</span>' :
    		freeCheck($product['price']),'
    	</div>';
    Code (markup):
    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 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #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 Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #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...:)
    $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>";
    PHP:
    BTW escaping single quotes doesn't work if you have javascript in the string ;)
     
    Strider64, Mar 25, 2013 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #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.

    $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>';
    Code (markup):
    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