conditional statement split between includes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grubstuck
Forum Newbie
Posts: 4
Joined: Thu Dec 23, 2004 6:53 pm

conditional statement split between includes?

Post by grubstuck »

Stupid question...
I am taking a currently entirely public site and making some of its content 'private' so that only logged in members can see content. Currently the bulk of code and html is in a header include and a footer include. I'd like to avoid making any changes to the individual pages (the reason for having includes in the first place!).

What I'd like to do is the equivalent of this:

header.php file...

Code: Select all

<?  $frog=1;
    if ($frog) &#123;  ?>
footer.php...

Code: Select all

<? &#125;  else &#123;  echo "go away"; &#125;  ?>
and the actual page file...

Code: Select all

<? include ('header.php') ?>
   this is the existing content for the page. blah blah.
<? include ('footer.php') ?>
Make sense? This does not, as you know, work as it is written above. Any quick ideas on how I could work this so that I don't have to edit the individual pages? obviously I could just do a quick search and replace on the entire site, but I'd rather not.

thank you!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

if (logged in) {
    // show logged in stuff

    // include() something if you want
} else {
    // dont
    // include() something else f you want
}
grubstuck
Forum Newbie
Posts: 4
Joined: Thu Dec 23, 2004 6:53 pm

Post by grubstuck »

rehfeld wrote:

Code: Select all

if (logged in) {
    // show logged in stuff

    // include() something if you want
} else {
    // dont
    // include() something else f you want
}
Now try putting that if statement in one include and the else in another. :D
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php

// the if
if (logged in) {
    // do something
}

?>

Code: Select all

<?php

// the else
if (not logged in) {
    // do something
}

?>
grubstuck
Forum Newbie
Posts: 4
Joined: Thu Dec 23, 2004 6:53 pm

Post by grubstuck »

nope

see in my example how the conditional statement spans both includes. Opened in the first, and closed in the second, spanning the main content of the page.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

hey that isn't too hard is it. for eg when i saw ur post i immediately brought up notepad and wrote following.

Code: Select all

<?php
$doit = 1;
if($doit==1)
&#123;
echo <<<ITSOK
<html>
<body>
You are Through
</body>
</html>
ITSOK;
&#125;
else
&#123;
echo "<html><body><h1>Shoo! Go Away!</body></html>";
&#125;
?>
and it works fine... :P

just place starting 4 lines above ur html n last 6 lines after it. simple.
one more thing i just took up PHP 3days ago. would ya believe it. and i joined this same day too...
i agree i had been doing coding in um well... lots of lang.s like C++,Java,Perl,Python,JS,VB erc so it becomes tad bit easier to follow any new lang.
Bye 4 now!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

grubstuck-

the examples given to you are valid ways to accomplish what your trying to do.

your trying to have part of a single statement span mutiple files. you cant do that, you have to split the statements up into equivalent peices, that each contain a complete statement.

there is a way you could make what your doing work though. you would have the read all you include files into a string(using file_get_contents() for example), and then, evaluate the php using eval() instead of using include()

that way you kinda "compile" the _entire_ statement into a string before php starts evaluating it. but i would not recomend using eval. usually, if you need to use eval() to solve your problem, your design could benefit greatly by improving it.

i would recomend you take a long hard look at your design and see how you could restructure and improve it.

i still think my second example is exactly what your after, i just dont think you realize it yet.

all you really want is an if{} else{} statement. you can acheive the exact same functionality of that by using 2 if{} statements, just negating the condition in the second if statement, like i have done.



Code: Select all

<?php

$foo = true;

if ($foo) {
    echo 'content';
} else {
    echo 'get lost';
}

// the above is exactly the same as this


if ($foo) {?>
output some stuff <html>
<?php }

if (!$foo) { ?>
<strong>get lost</strong>
<?php }

// either $foo is true or its not true, 
// so only 1 of the 2 conditional statements will be executed
// it doesnt matter if they are in diff files
grubstuck
Forum Newbie
Posts: 4
Joined: Thu Dec 23, 2004 6:53 pm

Post by grubstuck »

Revising the structure obviously will work, but this is exactly what I was trying to avoid. I appreciate the answers as I may not have been clear enough about this.
Post Reply