Page 1 of 1

Wrap if statement around HTML/PHP??

Posted: Thu May 31, 2007 7:33 am
by flashpipe
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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:

Code: Select all

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

Code: Select all

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

}
Thanks!!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu May 31, 2007 7:39 am
by feyd
You start and stop PHP multiple times in the first snippet. That's exactly what you would likely do for the second to integrate them.

Posted: Thu May 31, 2007 8:06 am
by flashpipe
Cool...thanks for switching code to php...didn't see that...

So, I tried this and it worked, but I need to make the display of the second input type (Add to Cart) conditional as well and I'm not sure where to put the if statement. I imagine I'll have to switch some of the parameters to a php call??

Code: Select all

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

Posted: Thu May 31, 2007 8:09 am
by feyd
Unless you're echoing new elements, I think it's better to set the variables based on your conditions since those repeat themselves.

Posted: Thu May 31, 2007 8:35 am
by flashpipe
Hmm...I'm afraid I'm not following you. I tried to wrap a php tag around the entire second input name line, but setting the entire line in an echo came back with an error...

are there certain things you can't include in an echo statement?

I apologize for my noob-ness...

Posted: Thu May 31, 2007 8:40 am
by superdezign
feyd wrote:Unless you're echoing new elements, I think it's better to set the variables based on your conditions since those repeat themselves.

Code: Select all

$isValid = (isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0");
if($isValid)
{
   // Do whatever
}

Posted: Thu May 31, 2007 9:18 am
by flashpipe
Ah, I see...so, create a condition and wrap the entire html chunk inside it...got it...

I'll try it out and see if I can get it working...

Thanks!!

Posted: Thu May 31, 2007 11:47 am
by RobertGonzalez

Code: Select all

<?php
$show_chunk = false;
$upper = 10;
$lower = 5;

// Get ready to bop out of PHP
if ($show_chunk):
?>
<p>This means that Show Chunk was set to true because you see this.</p>
<?php 
// Okay, bop back in
endif;

// Now lets try it again
if ($lower < $upper):
?>
<p>This means that the variable lower is less than the variable upper.</p>
<?php endif; ?>
There are a few ways to do what you want. What I posted here is an alternative syntax. You would also do something like this:

Code: Select all

<?php
$show_chunk = false;
$upper = 10;
$lower = 5;

// Get ready to bop out of PHP
if ($show_chunk) {
?>
<p>This means that Show Chunk was set to true because you see this.</p>
<?php 
// Okay, bop back in
}

// Now lets try it again
if ($lower < $upper) {
?>
<p>This means that the variable lower is less than the variable upper.</p>
<?php } ?>
Or even this:

Code: Select all

<?php
$show_chunk = false;
$upper = 10;
$lower = 5;

// Show stuff without leaving PHP
if ($show_chunk) {
    echo '<p>This means that Show Chunk was set to true because you see this.</p>';
}

// Now lets try it again
if ($lower < $upper) {
    echo '<p>This means that the variable lower is less than the variable upper.</p>';
}
?>