1

I am having an issue with my php and html. I understand the concept that once <?php ;?> is open you cannot put another line such as <?php ;?> inside that making it look like <?php <?php ;?> ;?> as it throws errors, and if not, it does on my end... Same goes that I understand that using echo, print etc. is more or less what I'm after. My dilemma here is that how would I structure this if using sessions? This is what I have so far, all of which is inside the <body> tag:

<?php echo $_SESSION['id']?'
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
</div>':'
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>';
?>

As you can note, this line: <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2> is what is throwing me errors since it is already inside a previous <?php ;?> statement...

I've tried using <h2>echo "$_SESSION['username'] ? $_SESSION['username'] : 'Guest';"</h2> but that doesn't work either...any help would be much appreciated...

Would I need to create a variable for that session username? So for example:

$username = "<?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?>"; and then place that inside the <h2> element? If so, how would I restructure that to make it work since I've tried that and it throws me errors?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user2732875
  • 269
  • 5
  • 15
  • You realize that you're missing `session_start();` – Funk Forty Niner Dec 19 '13 at 20:12
  • no i have that. - i just didn't include what all i have above the html – user2732875 Dec 19 '13 at 20:12
  • Ok. Had to check. Many are not aware about it and we see a lot of questions related to it. Someone else would've most probably have mentioned the very same thing. – Funk Forty Niner Dec 19 '13 at 20:13
  • no worries, I apologize if what i have isn't worded correctly...but yes, I do have that. My main questions at hand are is how to make what is in the html section, specifically the `

    ` elements read whether or not the user is logged in or not and if so reads their username which is already inside php...

    – user2732875 Dec 19 '13 at 20:16
  • The `` tag can accept some additional parameters, from what I've done before, maybe not in conjunction with PHP, but if the intention is to use PHP inside your `

    ` tag, then you could probably use `

    Text

    ` but that's untested of course ;-)
    – Funk Forty Niner Dec 19 '13 at 20:18
  • you can not use one line conditional (? and :) in this case. you can write this easily in `if ($_SESSION['id']) {...` form. – Positivity Dec 19 '13 at 20:19
  • @Fredd That's what I have and it throws me errors since I have the `

    ` tag inside another php thing already...

    – user2732875 Dec 19 '13 at 20:19
  • Your code (parameters) needs to be inside the `>` like so `

    >

    ` which "could" work, but again, un-tested.
    – Funk Forty Niner Dec 19 '13 at 20:20
  • @Fred Just from looking at that, I don't think that would work - wouldn't I need to specify an `id` or `class` if inside the `

    ` tag like that?

    – user2732875 Dec 19 '13 at 20:34
  • I wasn't sure it was going to work neither, it was just a theory. However, with a bit of tweaking, am sure something could be done with it. – Funk Forty Niner Dec 19 '13 at 20:40

8 Answers8

2

You have very basic errors in the code while mixing HTML and not closed PHP...

<?php echo $_SESSION['id'] ? 
'<div id="top_mid">
    <h2>'.($_SESSION['username'] ? $_SESSION['username'] : 'Guest').'</h2>
</div>' : '<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>';
?>

So, try to make it easier:

<?php echo $_SESSION['id'] ? $string1 : $string2; ?>

That's the basic conditional. String2 is simple string, nothing really special. String1 contains another conditional, that needs to be separated from string and concated back. To make it simplier:

$string1 = '<div id="top_mid"><h2>';
$string1 .= ($_SESSION['username'] ? $_SESSION['username'] : 'Guest');
$string1 .= '</h2></div>';

Hope this will explain it enough, these are really the basics. It must be obvious to you really fast.

Ejzy
  • 400
  • 4
  • 13
1

Look at your code. There are basic errors.

<?php if(isset($_SESSION['id'])): ?>
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
</div>
<?php else: ?>
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>
<?php endif; ?>
Mateusz Nowak
  • 4,021
  • 2
  • 25
  • 37
1

I personally use the ternary operator where an if statement seems a bit excessive, such as:

echo $animal=="fish"?"Wet":"Dry";

For anything else, an if statement is much easier to read. And echo is not always necessary:

<?php if($_SESSION['id']){ ?>
  <div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
  </div>
<?php } else { ?>
  <h2>
    <li id="toggle" style="list-style: none;">
      <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
      </p>
  </h2>
<?php } ?>
rybo111
  • 12,240
  • 4
  • 61
  • 70
1

use this instead:

<?php if($_SESSION['id']):?>
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest';?></h2>
</div>
<?php else: ?>
    <h2>
        <li id="toggle" style="list-style: none;">
            <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                      <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
            </p>
    </h2>
<?php endif; ?>
Positivity
  • 5,406
  • 6
  • 41
  • 61
1

The problem is more that you are trying to call <?php ?> inside of another <?php ?>

That said, you need to parse line 3 of your code as just another part of the string and not as a new block of php. Hopefully my edits to your code make enough sense to you

<?php echo $_SESSION['id']?'
<div id="top_mid">
    //modified to just be part of the string:
    <h2>'.($_SESSION['username'] ? $_SESSION['username'] : 'Guest' ).'</h2>
</div>':'
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
                  <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>';
?>
bobkingof12vs
  • 660
  • 5
  • 22
  • They do, in fact ejzy had that same format as you with what is inside the `

    ` tag. This is what I was looking for as it works, but I don't quite understand why that works, I thought either a `print` or `echo` was needed...

    – user2732875 Dec 19 '13 at 20:28
  • It is because you are already echoing a statement. Because you are embedding [ternary's](http://davidwalsh.name/php-shorthand-if-else-ternary-operators) (sorry don't know the plural of that word) you dont have to write `echo` every time you use a one, just before the first. Just think of it as one big `if...then...` statement. (mind you php has a bunch of notes on ternary's http://php.net/ternary) – bobkingof12vs Dec 19 '13 at 20:33
  • interesting, so if an `echo` is already in place like how i had it, there is no need to have other `echo`'s inside of it? And having what you have inside the `

    ` tag is the correct format for such a thing? I mean, I suppose it is since it works and I'm using it, I just want to make sure this is the best way of coding it in...

    – user2732875 Dec 19 '13 at 20:37
  • Well, what you have is just one long string. and you are interrupting it to put in more data. your middle part there could be simplified like `'this' . $variable . 'That'`. except in this case, your variable is just another ternary. in essence it is like saying `$variable = ($_SESSION['username'] ? $_SESSION['username'] : 'Guest' );` then in you string `'.$variable.'...'` does that make sense? – bobkingof12vs Dec 19 '13 at 20:55
  • Scratch my comments. See ejzy's. His explanation is much more clear than mine – bobkingof12vs Dec 19 '13 at 20:57
1

You are missing '>', and i recommend optimizing the code, something like this...

<?php if($_SESSION['id']): ?>
<div id="top_mid">
    <h2><?php echo $_SESSION['username'] ? $_SESSION['username'] : 'Guest'; ?></h2>
</div>
<?php else: ?>
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please <a id="open" class="open" href="#open">Login | Register</a>
           <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
</h2>
<?php endif; ?>
José Ramírez
  • 369
  • 4
  • 15
0

(Presuming that you've already set session_start();)

You can't nest <?echo and ?> like that. Also note the (isset()) around your inline/shorthand if statement to ensure you're actually checking if it's set. The way you had it isn't actually checking the variable for anything. (Not even sure that's valid syntax?)

Do:

<?php echo $_SESSION['id'] ?> <!-- note the closed ?> tag -->
<div id="top_mid">
    <!-- wrapped your $_SESSION['username'] check in (isset()) -->
    <h2><?php echo (isset($_SESSION['username'])) ? $_SESSION['username'] : 'Guest';?></h2>
</div>
<h2>
    <li id="toggle" style="list-style: none;">
        <p>Please 
            <a id="open" class="open" href="#open">Login | Register</a>
            <a id="close" style="display: none;" class="close" href="#close">Login | Register</a> to access this page!
        </p>
    </li> <!-- you were missing this close tag -->
</h2>
<!-- removed the nested closing ?> tag -->

Update, since we're talking about doing conditionals:

<?php

if (isset($_SESSION['id'])) 
{
    echo "<div id='top_mid'>";
    echo "<h2>".(isset($_SESSION['username'])) ? $_SESSION['username'] : 'Guest';."</h2>";
    echo "</div>";
}
else
{
    //...do other stuff
}

?>

...while this approach gets you into dealing with double quotes, single quotes, or escaped quotes, and does add an extra layer to coding (echo EVERYTHING), it keeps your if/else statements a lot cleaner, and if you're using a code highlighter, you can easily see what's being spit out on the page between conditionals or in functions.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Thank you for pointing that out. I completely forgot to close the `
  • ` tag. I do have a question though regarding your setup: The very first line - `` you closed - does that line apply to what is below? If not, is it even necessary to have it if it is closed right away like that?
  • – user2732875 Dec 19 '13 at 20:32
  • php can handle variables from the start of the page to the end; they continue on through the close and open tags -- the tags are only there to denote what is- and isn't php. In your case, if you *don't* close it, you'll get errors as it tries to parse the html as PHP. Sometimes you need to handle `if` statements across close tags too (though I've never used them in practice): http://stackoverflow.com/questions/564130/difference-between-if-and-if-endif – brandonscript Dec 19 '13 at 20:36
  • Interesting, so with that being said, wouldn't something like `@rybo111` or better yet `@webinan`'s answer be more adequate? – user2732875 Dec 19 '13 at 20:42
  • Well, actually yes -- but I honestly didn't understand what you were trying to do with the code. Generally speaking, I find it much cleaner to echo out everything in php. I'll update my answer with a new example. – brandonscript Dec 19 '13 at 20:58