Jump to content

[SOLVED] Wrapping if statement around HTML/PHP


flashpipe

Recommended Posts

I'm new to php and I need to wrap an if statement around a group of php/html and don't know how to go about it...if anyone could help, that would be great...

 

I need to make the following chunk of code display based on the if statement below:

 

<form action="<?php echo($cartweaver->thisPageName . "?cartid=" . $_SESSION["CartId"]);?>" method="post" name="addToCart">
<?php cw3ProductOptions($productId, $taxRate);?>
    <input name="prodId" type="hidden" value="<?php //echo($productId);?>">
    <input name="submit" type="submit" class="formButton" value="Add to Cart">
</form> 

 

and this is the if statement I need to wrap around it...

 

if(isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0") {

} 

 

Thanks!!

<?php
if(isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0") {
?>
<form action="<?php echo($cartweaver->thisPageName . "?cartid=" . $_SESSION["CartId"]);?>" method="post" name="addToCart">
<?php cw3ProductOptions($productId, $taxRate);?>
    <input name="prodId" type="hidden" value="<?php //echo($productId);?>">
    <input name="submit" type="submit" class="formButton" value="Add to Cart">
</form> 
<?php
}
?>

That is a shady way to do it, the proper way is to have the data you want printed in a string and print it out at the end of the script to avoid any issues that might come from modifying headers etc. This is the way i would go about doing it:

 

<?php
$form=false;
if(isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0") {
    $form = '<form action="' . $cartweaver->thisPageName . '?cartid=' . $_SESSION["CartId"] . '" method="post" name="addToCart">
     '. cw3ProductOptions($productId, $taxRate) .'
    <input name="prodId" type="hidden" value="'.$productId.'">
    <input name="submit" type="submit" class="formButton" value="Add to Cart">
</form>';
}

if (!$form) { 
    echo $form;
}

 

This way you have full control and do not have to worry about conflicting items such as session_start or headers or even setcookie.

 

Just another option to do it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.