Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2006
    Posts
    197
    Thanks
    9
    Thanked 0 Times in 0 Posts

    My first PHP code - If statement - why doesnt it work

    Hi,

    Im trying to create an if statement. This is the code I was using:

    Code:
    <a href="#" class="buttonx"><span class="<?php echo $_product->gettesting() ?>"><?php echo $_product->gettesting() ?></span></a>
    Im trying to make it into an If statement. This is my attempt:

    Code:
    		<?php
    if ($_product->gettesting()=="Optional");
    <a href="#" class="buttonx"><span class="Optional">Optional</span></a>;
    else
    <a href="#" class="buttonx"><span class="Mandatory">Mandatory</span></a>;
    ?>
    However, it seems to cause problems on my site, the code doesnt work and also stops a lot of content from displaying. Can anyone point me in the right direction with this?

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    UK, North West
    Posts
    36
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Try this:

    Code:
    <?php print (($_product->gettesting() == "Optional") ? "Optional" : "Mandatory"); ?>
    The above is just like a if statement but more better used for the above situation.

    EDIT so it'll be like this:

    Code:
    <a href="#" class="buttonx"><span class="<?php print (($_product->gettesting() == "Optional") ? "Optional" : "Mandatory"); ?>"><?php echo $_product->gettesting() ?></span></a>
    Last edited by Shauny_B; 02-22-2010 at 04:46 PM.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    the HTML code is inside PHP, so it’s just a bunch of invalid data. to pass it to the HTML page, you need to write it to the output.
    PHP Code:
    <?php
    if ($_product->gettesting() == "Optional"
    {
        echo 
    '<a href="#" class="buttonx Optional">Optional</a>';
    }
    else 
    {
        echo 
    '<a href="#" class="buttonx Mandatory">Mandatory</a>';
    }
    ?>
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder
    Join Date
    Oct 2006
    Posts
    197
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you, that works perfectly


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •