Forum Moderators: coopster

Message Too Old, No Replies

PHP conditional include, help needed.

I think that's what I want...

         

Craig_F

5:57 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



I just set up a perl script that outputs a single html file. Works great, but I need to be able to customize that one file for different sections of the site. Specifically I need different navigation and heading on the page. I'm not a programmer, but this sounds like I want a conditional include to show what I want when I want, right? If so, can anyone give me any idea how to get started with something like this? I figured I'd try with PHP because it sounds easier than fooling with perl.

Also, doesn't a conditional statement generally work from a variable or something else in the page that changes? If so, what do I do when the page stays the same and doesn't include variables?

Warboss Alex

6:17 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Well, you need some sort of variable to identify your pages. Or perhaps to check which directory they're in, or something similar.

For my site, I've got a page generation class, which creates my pages. The class defines all the methods which generate the page content (header, footer, nav bars, content block, etc..). Then I've got child class definitions on each 'type' of page I want, i.e. a main site page, forums page, whatever..

Something like this.

//class.php
class page {
//class definition
}

//on the pages, i.e. main.php for a main site page
class main_page extends page {
//class definition, methods load content for a 'main' page
}

//on forums.php or forums/main.php or whatever
class forums_page extends page {
//class definition, methods load forum content
}

etc ...

I think it's something like this you want. It may be too complicated, or too simple, I don't know. It works for me..

Craig_F

6:47 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



thanks alex. I think I've found the solution I need. new question...

I'm now using php includes to do some of what I want but I get a parse error at this line:

<?xml version="1.0" encoding="iso-8859-1"?>

If I take it out eveything works, but all my styles and stuff get a little wacked out. Any ideas how to fix that?

Warboss Alex

7:12 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Heh. I used to get that error too.

Basically the '<?' in "<?xml version="1.0" encoding="iso-8859-1"?>" has the interpreter expecting php code (starting with the <?php tag), and, obviously 'xml version.. etc' doesn't parse as correct php.

To get round this, just output the xml tag like so:
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";?>

Or put your html in a seperate file which won't be parsed as php, i.e. with an extension of '.htm' ..

Craig_F

8:29 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



thanks, I wouldn't have known that solution.

fwiw, I did find out that "<?xml version="1.0" encoding="iso-8859-1"?>" isn't needed unless you plan to output your pages in xml, BUT it can cause IE to do buggy things.

I think that's why my styles change when I remove it...they change to what they really are rather than what IE thinks they are when the xml line is in there.

Warboss Alex

5:24 am on Mar 25, 2004 (gmt 0)

10+ Year Member



That kinda reminds me of what Dreamweaver tends to throw into your pages when you want 'em to be XHTML-compliant. You don't really need that line for html-only pages.