0
<?php
    if ($username)
        echo $username 
     else
        echo '<form class="navbar-form navbar-right" action="login.php">
                <button class="btn btn-custom">Log ind</button>
              </form>';
?>

I have this code and I want to be able to push my $username variable to the rightside, where my Login button was, when an user logs into my side. How would this be done?

I can't use echo <p class="btn btn-custom"> <? $username; ?> </p> what would be a possible way to do it?

6 Answers 6

2

Wrap the $username in a div/span element and align it like you want with css (maybe you can reuse the css class you used for your login link).

Example:

<?php
    if ($username) {
        echo '<div class="navbar-form navbar-right">' . $username . '</div>';
    } else {
        echo '<form class="navbar-form navbar-right" action="login.php">
                <button class="btn btn-custom">Log ind</button>
              </form>';
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Easiest way to do so is like this: you can echo a variable following a string with

echo 'string whatever'.$variablename.'more string';

<?php
    if ($username)
        echo '<div class="username">'.$username .'</div>';
     else
        echo '<form class="navbar-form navbar-right" action="login.php">
                <button class="btn btn-custom">Log ind</button>
              </form>';
?>

and then simply give the class .username the same positioning as navbar-right

Comments

1

Possibly you looking for this:

You are in right direction but need to use the echo statement to print $username

<?php
    if ($username)
        echo '<p class="btn btn-custom">' . $username . '</p>';
     else
        echo '<form class="navbar-form navbar-right" action="login.php">
                <button class="btn btn-custom"><?php echo $username; ?></button>
              </form>';
?>

Comments

1

Here you need to use div with float right style, float right style push the content on right side.

<?php
if ($username)
    echo '<div style="float:right">' . $username . '</div>';
else
    echo '<form class="navbar-form navbar-right" action="login.php">
        <button class="btn btn-custom">Log ind</button>
      </form>';
?>

2 Comments

Why should the OP "use this code"? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO.
@JayBlanchard ! thanks, I will take care about this also update it. Please let me know if you have any further suggestion.
0
<?php echo $username; ?>

Best way to implement this will be making use of smarty template.

Comments

0
<?php
if ($username) {
  echo '<div style="float:right">'.$username . '</div>';
}
else {
  echo '<form class="navbar-form navbar-right" action="login.php">
    <button class="btn btn-custom">Log ind</button>
   </form>';
}
?>

Use CSS' float:right attribute to push the $username to right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.