...

View Full Version : My first PHP code - If statement - why doesnt it work



spadez
02-22-2010, 04:38 PM
Hi,

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


<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:


<?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?

Shauny_B
02-22-2010, 04:44 PM
Try this:



<?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:



<a href="#" class="buttonx"><span class="<?php print (($_product->gettesting() == "Optional") ? "Optional" : "Mandatory"); ?>"><?php echo $_product->gettesting() ?></span></a>

Dormilich
02-22-2010, 04:45 PM
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
if ($_product->gettesting() == "Optional")
{
echo '<a href="#" class="buttonx Optional">Optional</a>';
}
else
{
echo '<a href="#" class="buttonx Mandatory">Mandatory</a>';
}
?>

spadez
02-22-2010, 05:32 PM
Thank you, that works perfectly



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum