tags:

views:

1905

answers:

7

I have a simple if-else statement--if true, I'd like it to echo this content:

<div id="login">
  <h2>Login</h2>
  <div class="box">
    <form method="POST" action="index.php/login">
      Username/Email:<br />
      <input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
      <?php echo form_error("username"); ?>
      Password:<br />
      <input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
      <?php echo form_error("password"); ?>
      <input type="submit" value="Login" name="login" />
    </form>
  </div>
</div>

and if false do something similar.

How should I approach this? I'm a PHP noob (and I've been annoying the community here!)--so I'm standing at the echo function right now--I think I can set variables, but that's about it.

Thanks so much!

+1  A: 

You can simply break out of PHP in the if/else statement.

if(true)
{
    ?>
    Regular HTML Here
    <?php
}
else
{
    ?>
    Regular HTML Here
    <?php
}

You can break out of PHP at any time using the '?>' operator, and with any control structure, it should show the code just as if you echo'd it. Another option would be to use output buffering.

if(true)
    {
        ob_start();

        ?>
        Regular Code Here
        <?php

        $contents = ob_get_contents();
        ob_end_clean();
    }

This will leave the contents of what outputted between the start and end in $contents.

Finally, you could use an include() to only include your login form when you actually need it. (I prefer this method because I can put login forms almost anywhere on the site very easily)

if(true)
{
    include('loginform.php');
}
Chacha102
eh, my server doesn't allow "include".
Kevin Brown
Thats really weird lol. You could always call a function you put somewhere near the bottom of the code that outputs the login form. (You can break out of PHP in a function aswell, just like the first example)
Chacha102
+1  A: 
<?php if ($condition) { ?>

    <div id="login">
      <h2>Login</h2>
      <div class="box">
        <form method="POST" action="index.php/login">
          Username/Email:<br />
          <input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
          <?php echo form_error("username"); ?>
          Password:<br />
          <input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
          <?php echo form_error("password"); ?>
          <input type="submit" value="Login" name="login" />
        </form>
      </div>
    </div>

<?php } else { ?>

    other html here

<?php } ?>
John Kugelman
A: 

Use something like this

echo htmlentities($string);

htmlentities

Set the string to the html code you want to output.

mrlanrat
That won't actually process the output code.
Chacha102
sorry, my bad. I thought you wanted the output to be the code.
mrlanrat
Haha. No problem!
Kevin Brown
+1  A: 
Freddy
+1  A: 

Just a suggestion; for readability and ease of flexibility, it is better to place what you want to display inside a variable first.

$html = "<form method='post' action='?'> ...... ";

if ($condition)
   $html .= "Error"
else
   $html .= "No problem"
....

<html>
  <body>
    <?php
      echo $html;
    ?>
   </body>
</html>

Or you can use a template. One which I would recommend is EasyTemplate.

Extrakun
+1  A: 

there's a form of php conditional that's somewhat easier to read for this, uses colon notation so instead of

<?php if ($condition) { ?>
...html
<?php } else { ?>
...html
<?php } endif; ?>

you do

<?php if ($condition) : ?>
...html
<?php else : ?>
...html
<?php endif; ?>

also if your config allows it, you can shorthand

<?= $variable ?>

instead of

<?php echo($variable); ?>
Scott Evernden
+1  A: 

Dropping out of php is the easiest way to do this. I find that using the alternate control structures makes this sort of code much more readable than stranded curly braces, especially if you have a lot of html.

<?php if (!$logged_in): ?>

    <div id="login">
      <h2>Login</h2>
      <div class="box">
        <form method="POST" action="index.php/login">
          Username/Email:<br />
          <input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
          <?php echo form_error("username"); ?>
          Password:<br />
          <input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
          <?php echo form_error("password"); ?>
          <input type="submit" value="Login" name="login" />
        </form>
      </div>
    </div>

<?php else: ?>

    // display html for a logged-in user

<?php endif; ?>
ironyCurtain