...

View Full Version : Echoing some HTML conditionally?



mothra
02-04-2007, 06:41 PM
What is the best practice for doing something like:



if(expr) {
echo [a bunch of html]
} else {
echo [some other html]
}



I could put the html into separate files and just use an include statement I guess, but I'd like to keep everything in one file.

chump2877
02-04-2007, 06:45 PM
I suppose, ideally, you could just redirect the page? It depends what you are doing...what are you doing with your code?



if (expr)
{
header("Location: page1.php");
}
else
{
header("Location: page2.php");
}

mothra
02-04-2007, 06:52 PM
When a user is not signed in I'm displaying a sign in form, when they are signed in I'm displaying some links etc... where the sign in form was. It's part of a sidebar type of menu system that is on all the pages of the site.

As mentioned above, I want to keep all the code in one file otherwise I would just use include statements.

CFMaBiSmAd
02-04-2007, 07:02 PM
If the question is what is the best way to have a "bunch" of HTML -

If it is only a "bunch" of HTML (no PHP variables...) then put a closing ?> php tag and simply place the HTML in your file.

If it is a "bunch" of HTML that contains PHP variables, then use the HEREDOC method, either in an echo statement or to place it into a string variable.

chump2877
02-04-2007, 07:03 PM
Well, I don;t know necessarily what best practive is, but if you have A LOT of HTML and you don;t want to be bothered with escaping quotes, etc., you could use the heredoc syntax: http://us2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

So in your case, you would have something like:


if (expr)
{
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo $str;
}
else
{
$str2 = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo $str2;
}

mothra
02-04-2007, 07:56 PM
Thanks for the suggestions. I tried there heredoc method but I just get errors. I don't see anything in the manual that says it precludes escaping quotes etc...

This is an example of the html code...



<form method="post" action="<?php $PHP_SELF ?>">
<fieldset>
<div class="err"><?= $err ?></div>
<label for="user">Username:</label><br />
<input type="text" name="un" /><br />
<label for="pw">Password:</label><br />
<input type="password" name="pw" /><br />
<br/>
<input type="image" name="btnSubmit" src="/images/submit.gif" alt="submit form" />
</fieldset>
<a href="./forgot_password.php">Forgot Your Password?</a>
</form>

chump2877
02-04-2007, 08:35 PM
I don't see anything in the manual that says it precludes escaping quotes etc...I gave you the URL in my last post:

Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings. (http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

And the example in the manual at the same URL:


echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;?>The following isn;t tested, but this is the idea (as far as printing your HTML with heredoc):


<?
echo <<<EOT
<form method="post" action="$PHP_SELF">
<fieldset>
<div class="err">$err</div>
<label for="user">Username:</label><br />
<input type="text" name="un" /><br />
<label for="pw">Password:</label><br />
<input type="password" name="pw" /><br />
<br/>
<input type="image" name="btnSubmit" src="/images/submit.gif" alt="submit form" />
</fieldset>
<a href="./forgot_password.php">Forgot Your Password?</a>
</form>
EOT;
?>

mothra
02-04-2007, 09:31 PM
I read right past it, I was probably too busy reading the WARNING, which turned out to be the problem... had a space in front of my closing tag. Removed the space and it works now.

I think this approach will work out, thanks again for your help.


Happy Super Bowl :)

chump2877
02-04-2007, 09:38 PM
Glad it worked out....Go Colts! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum