Wrap if statement around HTML/PHP??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Wrap if statement around HTML/PHP??

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Post 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!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Post 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...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
}
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Post 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!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>';
}
?>
Post Reply