Forum Moderators: coopster

Message Too Old, No Replies

PHP "if" statement to include content based on ID

tell include to include another file depending on page ID

         

luispunchy

1:54 am on Oct 8, 2007 (gmt 0)

10+ Year Member



Hi... I need help calling in a PHP include file based on an ID placed on the 'body' element, but can't figure out the proper "if" statement to do that. I know just enough PHP to be dangerous ;) so if there is a simple and easily explained solution, I'd very much appreciate your help.

details:

I have a standard sidebar of modules, which is being included in all pages via a simple 'php include ()' statement:


<?php // INCLUDE SIDEBAR.PHP?>
<?php include('includes/sidebar.php');?>
<?php // END SIDEBAR.PHP?>

This gets included on *all* pages.

Then I have a few unique pages - one is 'articles.php' - which require that same sidebar to be included, but with one additional module appended - which is itself a PHP include file called 'unique-module.php'.

This extra module is *only* to be included in the sidebar on these few unique pages. Additionally, this extra module needs to be placed in the middle of the overall sidebar.php file, so I can't just call it in as a separate include in the base pages. (if that makes sense).

I also can't use a simple CSS solution to hide it or show it (i.e., with 'display' definitions based on whether particular selector combinations are present on the calling page) because that will still leave the unique content showing when styles are turned off.

I need a truly dynamic control here, and I think a php "if" statement will do the trick, I just can't figure it out on my own.

I am thinking I can do this using the unique page ID attributes I've put on each pages' 'body' tag. For example, for the unique "articles.php" page, the 'body' tag has an ID:


<body id="articles">

So I am guessing I should be able to use a php "if" / include statement in the actual 'sidebar.php' include file itself that basically says "if body ID = 'articles', then include 'unique-module.php'. This way, when the 'sidebar.php' include file gets called by the base page, it can be passed that ID and be instructed to include its interior 'unique-module.php' include file.

In layman's terms, that's the goal. Make sense? I'm sure it's easy enough, but I haven't found it yet.

Thanks for any help!

vincevincevince

2:00 am on Oct 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't confuse HTML IDs and javascript variables with PHP, they are totally separate.

In the file which is parsed first and which is unique to that page, you set a PHP variable:
<?php
$include_in_sidebar="uniquemodule.php";
?>

Then, later, in your sidebar file, at the correct place:
<?php
if ($include_in_sidebar) include($include_in_sidebar);
?>

gettopreacherman

8:04 pm on Oct 8, 2007 (gmt 0)

10+ Year Member



you can always use querystrings on it and put the include in an if:
include("file.php?query=blah");

file.php
-------------

$value = $_REQUEST['query'];

if($value == "blah")
{
include('otherfile.php');
}