View Full Version : PHP Include/Not Include?
Alright you code geniuses, I got a headscratcher. I'm sure it should be easy, but I'm trying to think of the least complex way to do this. On my website, DrewPerry.com (http://www.drewperry.com) , I have a sidebar on every one of my pages. In order to make updating this sidebar a little less tedious, I use ?php include("mysidebarpage.php");? on every page, and then have only to edit the sidebar php page once. Works great.
The issue is, I want a section of that sidebar page NOT to show up on my photopage. One piece of the sidebar is a random image with a link to the photopage. Hence, I don't want that bit of code to parse and load on the photopage. How do I tell that to not load on that particular page? Currently, I just have two different sidebar pages and one of them doesn't have that code section. But I'd like to only have the one. After all, that's what dynamic code is for, ease of use.
Any ideas, oh great ones?
Marble
08-26-2004, 08:38 PM
There are probably more than a few ways around this, but one that I thought of right away is set a variable at the top of each page.... call it something like $tofu and then set it to true or false. $tofu = true; or $tofu = false;
Then in your nav include page, just do an if statement around that menu and check whether $tofu is true or false.
if ($tofu) {
... show menu ...
}
If $tofu is false is will not show. So at the top of the photo page set $tofu = false;
One thing to note. includes are not "true" functions , so you don't need parenthesis. Just saves on some typing. Not a bit of difference.
include "somepage.php";
I'm trying to keep the extra code to a minimum. Is there not a way to do like an, "If this page is photo.php, don't show this code?" Kind of an if self deal?
Marble
08-26-2004, 09:23 PM
It's not really that much code.
<?php
$tofu = true;
?>
For each page, defined before the included section.
Then just keep the menu exactly how it is, except wrap the optional section between the if statement.
The other option is make 2 versions, one with and one without.
okay, so I did that, I tried it like
<?php if ($tofu){
stuff I want to include
};?>
and I got a parse error. Unexpected <
But without it, I just get the
if ($tofu){
showing on the page
So here's what I did
<?php if ($tofu)
echo
"Photo code
I wanted
to exclude";?>
Works great! Thanks
Sardtok
08-29-2004, 04:26 AM
http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server
Check out this,
the most interesting one would be PHP_SELF of course, which returns the filename of the currently executing script,
I think it only returns the top-level script, so it could be put in the sidebar script,
but of course you should test it first, but if you want it to work on several pages in a directory you might have to use the DOCUMENT_ROOT variable instead.
Of course this is just an option, and would not be a much longer piece of code than the one that you are currently using.
So what's the syntax for that?
If I want to show code only on page test.php would it be like,
if PHP_SELF(test.php) {
code to run
}
or what?
igner
08-31-2004, 11:29 AM
no, it'd be
if (basename($_SERVER['PHP_SELF']) == "test.php" )
{
\\do stuff
}
this assumes that you just want to match the file name (and not the path from DOCUMENT_ROOT). If you want to match the full path, then get rid of the basename() function, and just compare the two items.
Hope that helps.
Perfect! Maximum functionality with minimum lines of code!
Graci!!!
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.