Forum Moderators: coopster

Message Too Old, No Replies

PHP Model View Controller for simple but dynamic file?

Title+Content to 1.php from 2.php but in two instances?

         

JAB Creations

4:31 pm on Mar 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am interested in creating a dynamic file handler to serve multiple pages on my site. However the issue is that I wish to pull a title and the content separately from 2.php. So the client sees 1.php?page=thisorthat. I'm not sure how to best approach the scenario. I can work with PHP classes if that helps any. Here is what I'd like to do...

1.php - Needs the HTML title initially but should not include other HTML content from 2.php until later in the body.
2.php - Has the title and the content though needs to serve both to 1.php separately.

Someone suggested Model View Controller though I'm not finding anything on php.net. I'm not exactly sure what I'm looking for.

I want the file 2.php to merely have a title in some sort of variable that I can insert in to the title element which is physically present in file 1.php.

Then I want to have my normal content below the title variable.

The issue is I only want to call the title from file 2.php and not have the body element's content. I don't need to get any more sophisticated then that. I know how to work with PHP classes (I think the original thread is at [webmasterworld.com...]

Oh and no I don't want to use some prebuilt script to achieve this. The complexity of my goal doesn't seem that complex. :)

- John

cameraman

6:31 pm on Mar 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a bunch of different ways to do this. I'm unclear as to whether or not you also want 2.php to be able to render correctly on its own. If you don't then it could just look like:
<?php
$title = 'page2';
$content=<<<EOC
<p>Now is the time for all good men</p>
<p>And then the duck said</p>
EOC;
?>

$content is assigned via the heredoc [us3.php.net] syntax. In 1.php you just include '2.php' and then echo $title and $content in the appropriate spots.

If you need 2.php to be standalone as well as capable of being subcontent for 1.php, 2.php could look something like this:
<!doctype html ....>
<!--BREAK--><!--page2--><!--BREAK-->
<html>
<body>
<!--BREAK--><p>Now is the time for all good men</p>
<p>And then the duck said</p><!--BREAK-->
</body>
</html>

In 1.php use file_get_contents or fread or whatever to get the file into, say, $doc. Then,
$parts = explode("<!--BREAK-->",$doc);
$title = substr($parts[1],4,-3);
$content = $parts[3];