PDA

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


luispunchy
October 8th, 2007, 03:25
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 easyily 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!

inimino
October 8th, 2007, 03:41
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.

Well, you can't do that. :)

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'.

The goal of PHP (and indeed any HTTP server) is to push the bits out to the client as soon as possible. Since the PHP is generating your HTML output (not the other way around) it's assumed that you already know what your HTML contains. In other words, PHP doesn't parse the HTML on the server, and there's no appropriate way for your PHP to query attributes on the HTML in which that PHP is embedded.

Instead, you could "step up" to the level of PHP, and set a variable there on each appropriate page.

//in the containing page...
$include_sidebar_extras = true;
...
include('sidebar.php');

//in sidebar.php...
if($include_sidebar_extras) {
...
}

luispunchy
October 8th, 2007, 04:46
Steppin' up! ;) I do understand all that, I guess I just didn't explain myself well enough. Your example is pretty much exactly what I was thinking of, I just don't know the syntax for writing it :)

I tried and I get an error message:

Parse error: parse error, unexpected '}'


I think maybe I'm using incorrect syntax - here's what I have inside my sidebar.php:

<?php
if ( $include_sidebar_extras ) {
include("sidebar-extra.php")
}
?>


can you tell me, is a semicolon needed to close an include function that is contained within that if statement?

inimino
October 8th, 2007, 05:35
The semicolon is required, that's why there's a parse error.

Servetech
October 8th, 2007, 11:03
One of the best ways to learn is trial and error, so the speediest option there (in my opinion) would of just to tried throwing a semicolon onto the end of the include statement (to find that the error dissapears!) :)

william smith
April 29th, 2011, 20:27
It depends upon how he has .htaccess set and his File permissions. Starter (http://www.buyautoparts.com/starter.htm)

michaeljii
May 18th, 2011, 06:50
I am very glad to know that your site is upgrading from the simplest to faster and synchronized form. I am quite familiar of a lot of sites since I work as a freelance writer and one of the sites that I find evolve is your site respectively. I am looking more on your site progress as well as new version that is more convenient and helpful to the members like me. I really appreciate the move that you made.
1z0-053 (http://www.examsheets.com/exam/1z0-053.htm)\646-578 (http://www.examsheets.com/exam/646-578.htm)\642-873 (http://www.examsheets.com/exam/642-873.htm)\642-611 (http://www.examsheets.com/exam/642-611.htm)\1Y0-A08 (http://www.examsheets.com/exam/1Y0-A08.htm)\642-647 (http://www.examsheets.com/exam/642-647.htm)\1Y1-A19 (http://www.examsheets.com/exam/1Y1-A19.htm)\70-511 (http://www.examsheets.com/exam/70-511.htm)

alton100
June 22nd, 2011, 06:48
Thanks for sharing this information. I really like your way of expressing the opinions and sharing the information. It is good to move as chance bring new things in life, paves the way for advancement, etc. But it is well known to everyone that moving to new location with bulk of goods is not an easy task to move or shift from one place to other place because I have experienced about that and I face the problem like that. There I go to village near to my city faced that problem there.
642-359 (http://www.certidea.eu/642-359-exam.html) \\ 640-863 (http://www.certidea.eu/640-863-exam.html) \\ 642-627 (http://www.certidea.eu/642-627-exam.html) \\ 70-511 (http://www.certidea.eu/70-511-exam.html) \\ 1Y0-A08 (http://www.certidea.eu/1Y0-A08-exam.html) \\ 70-516 (http://www.certidea.eu/70-516-exam.html) \\ 1z0-052 (http://www.certidea.eu/1Z0-052-exam.html) \\70-663 (http://www.certidea.eu/70-663-exam.html) \\ 1z0-053 (http://www.certidea.eu/1Z0-053-exam.html) \\ 117-101 (http://www.certidea.eu/117-101-exam.html)

clark40
July 25th, 2011, 13:00
I just love your blog.Thanks for posting it. it have something that someone comeback again….there is a lot of useful information a person can get from here…I must say,well done.A good blog with great discussion and a good users,which contribute in the blog..Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!pass4sure 70-659 (http://www.thepass4sure.info/70-659-test.html) | pass4sure 70-660 (http://www.thepass4sure.info/70-660-test.html) | pass4sure 70-662 (http://www.thepass4sure.info/70-662-test.html) | pass4sure 70-663 (http://www.thepass4sure.info/70-663-test.html) | pass4sure 70-664 (http://www.thepass4sure.info/70-664-test.html) | pass4sure 70-665 (http://www.thepass4sure.info/70-665-test.html) | pass4sure 70-667 (http://www.thepass4sure.info/70-667-test.html) | pass4sure 70-668 (http://www.thepass4sure.info/70-668-test.html)

johnmartin
August 11th, 2011, 07:36
The main idea of online shopping is not just in having a good looking website that could be listed in a lot of search engines or the art behind the site. It also is not only just about disseminating information, because it is also about building relationships and making money. Mostly, 642-637 dumps (http://www.brain-dumps.biz/exam/642-637.html) / 640-721 dumps (http://www.brain-dumps.biz/exam/640-721.html) / HP0-Y30 dumps (http://www.brain-dumps.biz/exam/HP0-Y30.html) / 70-290 dumps (http://www.brain-dumps.biz/exam/70-290.html) / 650-195 dumps (http://www.brain-dumps.biz/exam/650-195.html) / 000-115 dumps (http://www.brain-dumps.biz/exam/000-115.html) / 70-513 dumps (http://www.brain-dumps.biz/exam/70-513.html) / 646-671 dumps (http://www.brain-dumps.biz/exam/646-671.html) organizations try to adopt techniques of online shopping without understanding these techniques and/or without a sound business model. Rather than supporting the organization's culture and brand name, the website should satisfy consumer's expectations.