PDA

View Full Version : PHP "if" statement to include content based on ID


luispunchy
10-08-2007, 02:29 AM
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!

name _F1
10-08-2007, 02:40 AM
There's no easy way to do this with PHP, AFAIK. You can't just do if (body.id == 'XXX'). Is there any reason you can't just use a $_GET variable (so your page URL would be page.php?page=articles). That would be a lot simpler.

matak
10-08-2007, 02:59 AM
like name F1 said, it's all in get.

just realize http and thing like ?id=something

if your link is something like index.php?id=something then you'll get this result with code

echo $_GET['id']

//will echo 'something'


if your link is different will echo something different...

you can also use it with include, something like this

include ("$_GET['id'].php");

will include something.php, if your id was something..

luispunchy
10-08-2007, 03:00 AM
There's no easy way to do this with PHP, AFAIK. You can't just do if (body.id == 'XXX').

That's essentially what I am looking to do... hmm... there must be a way (i'm always learning that there's always *some* way...)

Is there any reason you can't just use a $_GET variable (so your page URL would be page.php?page=articles). That would be a lot simpler.

Right, only I can't do that for the simple reason that i can't have the urls looking like that :(

matak
10-08-2007, 03:03 AM
Right, only I can't do that for the simple reason that i can't have the urls looking like that :(

then you can override them with .htacces mod_rewrite rule . and that's hard :mad:

luispunchy
10-08-2007, 03:05 AM
@matak - right, same problem tho that I am restricted in using a GET method, for a few reasons. Basically, it's a template and i need to have common pieces assembled on different pages... and this one sidebar element can only show on certain pages. I understand the $_GET variable approach, but it won't work for my end purposes...

i guess i can just duplicate 'sidebar.php' as 'sidebar2.php' and in that copied version put the unique module, and then call 'sidebar2.php' on those few pages... I was just hoping for a more dynamic solution (save me work and cluttered files).

any other ideas?

matak
10-08-2007, 03:14 AM
tho that I am restricted in using a GET method

can i just ask one question.

does your sidebar.php call articles.php by using $_GET? if that's so, then i think the answer is simple, but it depends on your code... (maybe if you go into oop?)

luispunchy
10-08-2007, 03:57 AM
does your sidebar.php call articles.php by using $_GET?

nope - not using GET, not passing anything in the URL. I am simply trying assemble common pieces via includes so that I can save myself the work -- nothing dynamic happening with user, just me being lazy ;)

I am using the following (suggestion from elsewhere) to set a variable on the appropriate base pages -


<?php $include_sidebar_events = true; // setting variable
include('includes/templates/sidebar.php'); // include sidebar.php file
?>



Then i reference that variable in the sidebar.php file, to call in the "extra" bits that should only go on those appropriate pages:


<?php
if ( $include_sidebar_extras ) { // check for that variable having been passed by base page
include("sidebar/ebc-sidebar.php"); // include the "extra" module in sidebar.php
}
?>


However, this doesn't seem to be working. Is my syntax incorrect?

Inigoesdr
10-08-2007, 06:25 AM
Your syntax is correct if you set the variable earlier in the script. You can try var_dump($include_sidebar_extras); Or something similar to find out more information.

matak
10-08-2007, 04:22 PM
<?php
if ( $include_sidebar_extras ) { // check for that variable having been passed by base page
include("sidebar/ebc-sidebar.php"); // include the "extra" module in sidebar.php
}
?>


However, this doesn't seem to be working. Is my syntax incorrect?

Shouldn't your if statement look like


<?php
if ( $include_sidebar_extras == TRUE ) { // check for that variable having been passed by base page
include("sidebar/ebc-sidebar.php"); // include the "extra" module in sidebar.php
}
?>

Inigoesdr
10-08-2007, 06:23 PM
Either way will work. The expression "$include_sidebar_extras" when evaluated by the parser will return true if it's value is true, and false if it's value is not.

fl00d
10-09-2007, 12:57 AM
you can also use it with include, something like this

include ("$_GET['id'].php");

will include something.php, if your id was something..

thats a good way to open up the site to attacks (shells).

matak
10-09-2007, 11:43 AM
easily avoided.

$included_files = array (file1, file2);

if in_array { do something else}
else {
start a ban function :)
}

Codeheed
10-09-2007, 01:09 PM
<?php
if ( $include_sidebar_extras ) { // check for that variable having been passed by base page
include("sidebar/ebc-sidebar.php"); // include the "extra" module in sidebar.php
}
?>


if this isnt working for you you need to be looking elsewhere for the problem , the code is sound. I think it might be a file / directory problem. I have no clue as to what kind of hosting / server setup you are using so may be a shot in the dark.

I will try and explain what I think may be happening.

I have a page a.php
<? include("b/b.php"); ?>

a.php includes a page b.php which is in a directory called b

b.php includes a file c.php which is in a directory called c
<? include("c/c.php"); ?>

Now in order for this to work properly the directory structure would have to look something like this

|-a.php
|-b/
...|-b.php
...|-c/
......|-c.php

The important thing to notice is that the directory c/ is inside the directory b/
this is because [usually] the included path is relative to the document that the include("file.php") is in so although b.php is included in a.php the
<? include("c/c.php"); ?> is relative to b.php.

I hope that makes sense.