Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Echoing some HTML conditionally?

    What is the best practice for doing something like:

    PHP Code:
    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.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,845
    Thanks
    21
    Thanked 157 Times in 148 Posts
    I suppose, ideally, you could just redirect the page? It depends what you are doing...what are you doing with your code?

    PHP Code:
    if (expr

      
    header("Location: page1.php");

    else 

      
    header("Location: page2.php");

    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,845
    Thanks
    21
    Thanked 157 Times in 148 Posts
    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/languag...syntax.heredoc

    So in your case, you would have something like:

    PHP Code:
    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;

    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

    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>

  • #7
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,845
    Thanks
    21
    Thanked 157 Times in 148 Posts
    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.

    And the example in the manual at the same URL:
    PHP Code:
    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):
    PHP Code:
    <?
     
    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;
    ?>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #8
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #9
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,845
    Thanks
    21
    Thanked 157 Times in 148 Posts
    Glad it worked out....Go Colts!
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •