PHP!

So. I’m learning PHP in a fairly random manner. I’m-a try my hand at having one PHP file which has the formats and layouts and whatnot, and it reads the title, body, etc. from other files, in a manner which isn’t entirely unlike Wikipedia. Anyone got any suggestions for the best way to do this?

Someone else suggested that I use the “include” command on other PHP files with the content, which would merely have the content and other variables like the title, but when I asked how it might be possible to get variables from a PHP before that (i.e. getting the title before I actually want the text to fill the screen), she suddenly ceased to be helpful in any way that would pass scrutiny from the mods on this forum, and linked me to a website which now makes me question whether or not doing so is an actual option. And I’m not going to make PHP files containing

$content = "<p>The entire text of the page!</p>
<h3>A subsection!</h3>
<p>See, now we're REALLY going nuts.</p>"

I mean … I could, theoretically, but (a) that would be really stupid because (b) what if I wanted to include actual PHP code? Also © there should be a better way, using PHP as the other type of file or not.

Using include() allows you to fill in variables before you have to print them. So for example,


Contents of file "include.php":

<?php
$title = "Hello World";
$background="#FFFFFF";
?>

Contents of file "main.php":
<?php include("include.php");?>
<HTML><HEAD><TITLE><?=$title?></TITLE></HEAD>
<BODY BGCOLOR="<?=$background?>"><H2><?=$title?></H2>

Is that what you’re looking for? I’m not sure I understood your problem 100% clearly.

Ah yes, precisely, thanks. (Although I’m using CSS instead of body bgcolor=etc, but anyway!)

Is there a better way of doing that kind of thing, though, than Including another PHP file at all? I’d been thinking of trying to use XML, or something, but …

There are several ways you could do it:
-Stick it in a plain file.
-Stick it in a database (MySQL).
-Stick it in an XML file.

But the truth is that these are all really analogous to each other. XML files in particular are really just slightly slower, less secure/robust, but far more flexible databases.

Okay, update on the previous issue:

The problem is that I want to do it like this.

File 1 is:

<?PHP
$title = "Main Page"
?>
Hello world!

The problem is that the “Hello world!” bit then appears twice – one where it’s supposed to, and one before the header info. Is there a way to include stuff, but doesn’t actually display literal text like that? I mean, without causing a catastrophe elsewhere somehow? I mean … I tried to do it like this:

      ob_start();
      include(<whatever>);
      ob_end_clean();

Which seemed to work right up to the minute I tried to call a function that did that from within another file that was being included. At which point the PHP crashed and I got a “Connection was reset” error in Firefox (nothing loaded on a given page until I removed the offending code).

Oh yes – another thing I want to do is: search through all PHP files in a given directory to see which of them set a particular variable to a specified value (for the record, the variable in question is $parent). Is there a halfway-efficient way of doing this? Or should I do things differently than import PHP files if I want to do it like that?

I’m not quite sure I get the problems above. I’d really have to see the whole files to try and diagnose it. 3 lines of code won’t help much. 8-(

As for your last question… no, there’s no easy way of doing that. If you want to do that you may want to look into another way of storing it. Including PHP files is only useful if the files are included once and only set variables, not do weird things on the side. Included files should NEVER automatically print stuff to output, since that makes them very hard to debug.
I guess a better question would be why you want to do that rather than how to do it.

Okay, I’ve figured out the first problem: it was going into an infinite loop. I suppose I can get around this by putting an “if($somevariable)” around the whole PHP code …

The “why” I wanted to do the second thing is that I want to set certain files as “parents” of other files. The search was going to be “for all files that have X as its parent” (for indexes, etc). I currently parent files store their own children, but it’d be much more efficient to have the parent/child relationship of a given file be stored only in one place.

The reason I’m using PHP files is that I may want them to run PHP code (in fact, I’ve got several of them running their own PHP code, some of it unique to that page), and I’m not quite sure how to do that in, say, an XML file. (Although I think I might know how to do it from an XML file if I don’t want to include PHP …)

Also try the include_once() function for the first problem.

I’m still not sure I get the whole point of having parents and children of files. What exactly does it mean for a file to be a “parent” of another file?

From what little I know, it seems you definitely want to store the structure somewhere specific (in a single include file). As to running PHP code, you can store and call function names like this:


function myFunc() {...}

$func = "myFunction";
$func();

So you can have one big array of functions and call them that way if you like, maybe based on the filename of the calling page.

Sorry, I suppose I could explain better. I meant having them be hierarchical, with the “parent” being analagous to a subdirectory with an index page, and the “child” being files in that subdirectory, but (a) I want the directive to be “…/php/index.php?id=parent” and “…/php/index.php?id=child”, not “…/php/index.php?id=parent” and “…/php/index.php?id=parent/child”*, which means (b) I want to have each page have a unique filename.

As for the “array of functions on the main index” page (assuming I’m reading you right) … my response to that is basically, why? I don’t want to have to update the main index.php every single time I want to update the code in one of the sub-files. I want “the code I’m only ever going to use in this specific file, made specifically for this specific file”, to be just in that specific file. And, uh … if that’s not what you meant, uh, I was referring to putting PHP code in XML files …

*Actually, I’d really like to be able to just do “…/php/parent” and “…/php/child”, but I think that’s gonna have to be server-side, not PHP-side. And if it is, it’s still beyond my means right now …

Nope, I’m still not getting it. What does a parent-child relationship MEAN, then? And if everything is off of a main index.php, why would you care about files in the directory?

I think you’ll have to explain from the start what exactly you’re doing. I’m getting a dim inkling of the how, but none of the why, and I can’t really help out without that since this isn’t a simple problem.

Uh … hmm. Now that I really sit back and really think about what I’m doing … I just realized that what I’m going for amounts to ripping off the website of the How To Destroy The Earth guy. (I hate it when I do things like that without noticing it …)

I will say, though, that having really short URLs is kinda convenient … I mean, it’s much easier to remember and intantly type “http://qntm.org/destroy” than, say, “http://qntm.org/misc/geocide/destroy” …

Um… okay. Still doesn’t explain what you’re trying to do on a high level.

So you’re trying to use PHP to make the directory structure look better? Why not just use GET to change the content using if isset() etc. and suppressing notices via the PHP config file (Which I’m assuming you have access to) about certain variables being unset?

The How To Destroy the Earth page is just using anchors and CSS liberally. Nothing really involving PHP.

Okay.

First of all, Steve, just because you can’t see the PHP doesn’t mean it isn’t there – the site map explicitly states that the site as a whole is run entirely on PHP. I’m guessing he’s storing just the text/HTML/whatever somewhere, and they get sent through a PHP parser which actually displays the title, the content, the navigational sidebar … Well, see below.

Hmm. Okay, I think I’ve got down what I’m going for … I want to be able to make a PHP-based website which does the following:
[ul]
[li]Have a hierarchical structure, so that a site map would look more or less like the one I linked to above. Any page can be an index/category/section/what-have-you (hereinafter “index page”), with one or more “child” pages. Any child can also be an index page. All pages must have precisely one parent except the main, which doesn’t have any. The “parents” must all lead back to the main page. Example: “email” is child of “contact” is child of “meta” is child of the index.
[/li][li]However, also like qntm.org, I want to be able to have all the files in one directory: I don’t want to type “index.php?id=index/meta/contact/email”, I just want to be able to type “index.php?id=email”.
[/li][li]Each index page must have a section in which all its immediate children are listed. (In qntm.org, they’re headed by “Today in [name] …”) I want to be able to have text both above and below this section, if I want.
[/li][li]A navigation sidebar bar which lists (a) all parents and grandparents recursively, and (b) all pages which have the same parent. (I’ll store the positions and stuff using CSS.
[/li][li]Store each page separately from the <html>, <head>, <body>, etc. Just the title, parent, and content.
[/li][li]Need to be able to access the title and parent from other pages, like for the navbar and the “Today In [name]”.
[/li][li]Should be able to put whatever HTML codes I want (within W3C standards, of course, but!) in the content itself. The ability to parse PHP code is also greatly preferred.
[/li][li]An optional “External Links” section at the end, but that would be relatively easy, and I have an idea of how to code that, assuming it’ll be using PHP …
[/li][/ul]
I have only a vague idea how to use this with storing the stuff in PHP files, and it involves Including whatever file I need (helpfully hidden inside a function so as not to, y’know, hurt anything). As well as hiding custom-for-this-one-file PHP inside a string and then using eval(), which is apparently fairly … unhealthy.

It sounds like what you want is an XML file. XML is tree-based, which is exactly what this does. Inside XML content you can have whatever HTML and PHP content you like. Just grab the contents of the XML tag and do something like


eval('<?' . $tagContents . '?>');

Your XML file might look something like this:

<fileStructure>
 <file name="main" title="Main Page">
    <content>Here is the page content.</content>
    <file name="sub1" title="Sub Page 1">
      <content>Sub page content</content>
    <file name="sub2" title="Sub Page 2">
      <content>Sub Page 2 content</content>
      <file name="sub2a" title="Sub Page 2a">
        <content>Sub Page 2a content</content>  
    </file>
  </file>
</fileStructure>

You can navigate around the XML tree using XML parsing methods. You can build an object out of it, for example, with each object having references to parent (one reference) and children (array of references), and you can then navigate around that object easily.

EDIT: You may want to Google search for a visual XML editor. That’ll make adding and editing content much easier. Alternatively, you can go the database route (using a table with fields “parent”, “title”, “content” and getting the whole table at once so you can build an object dynamically) but that doesn’t provide you with an easily displayed tree view.

Hmm … no … No, that’s not really what I’m looking for. I still want to be able to have each page in a separate file. If I’m doing it all in one file, it would get atrociously huge really quickly. Yeah, it kinda works with four “files”, each a single short sentence long, and three “generations” deep, but what about twenty essay-length files? Fifty, with potentially ten degrees of depth? Navigation through it alone is going to be a headache, not to mention if I want to move something, currently with five essay-length children, to a different parent altogether. Then it’s harder to regulate, say, trying to give two files the same name by accident … I could go on like this, but I think you get the idea.

Then in the “content” field, simply put a filename rather than the entire contents of the file, and read the filename in.