axtg86 Posted July 6, 2009 Share Posted July 6, 2009 Hi, A newly formulated question which hopefully someone can point me in the right direction for. This is what I'd like to accomplish: I've got a .htaccess file that rewrites any *.html file to index.php?page=*. So when page welcome.html is requested it gets rerouted to index.php?page=welcome. This works fine, but the next step is causing me trouble. Note that content are saved in separate text files. I'm running a query (WHERE page=$_GET['page']) to select $row['template'] for the requested file. In the templates directory such a template exists (/templates/$row['template'].php) which I now want to apply to the contents (which can include PHP as well) of the designated page (/contents/$_GET['page'].php). Unfortunately I haven't been able to find a solution for this without e.g. using a header(location) function (I'd like to keep the address in the address bar in tact) or frame. A CMS such as Joomla is able to do this. But that system uses a database to store contents. How could I proceed? Thank you! -- P.s.: I've had a topic before, but my vision has changed in the mean time. I tried closing it, but no luck. Since it is currently at page 10+ I don't consider it disturbing. Quote Link to comment Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Unless I'm missing something, what you're asking can be accomplished by simply including the file specified in the $_GET var. include("/templates/{$row['template']}.php"); This wouldn't cause the url to change, and will automatically load the file within the current page. Quote Link to comment Share on other sites More sharing options...
axtg86 Posted July 6, 2009 Author Share Posted July 6, 2009 Hi p2grace, I admit I had difficulties formulating the problem. I think I could have better used the term "merge" in there. I have two files (template file and content file). The content file does not contain any layout mark-up, just content. Based on the specified template in the DB for that content file (specified by $_GET['page'] the appropriate template file has to be applied to it. Causing the two to merge. Much like e.g. Drupal, Joomla where you have a template file that can be applied to certain content. Except now not using a database that holds the content, but an actual file. Does that clarify things? Thanks for your reply! Quote Link to comment Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 In this case, I'd load the template using the include trick, and I'd create a class for content loading that can be called by each included template file. Workflow: Main page -> Includes Template Page -> Template page calls loadContent function from content class Does that make sense? Quote Link to comment Share on other sites More sharing options...
axtg86 Posted July 6, 2009 Author Share Posted July 6, 2009 Hi again, It certainly makes sense, but I can't see how the content of the template file that is included can then run code on its own. But that might have something to do with me never having tried it... So: index.php // Part 1 function getContent() { $filestream = @fopen("content.php", "r"); $filecontent = @fread($filestream, @filesize("content.php")); echo $filecontent; } // Part 2 $result = "SELECT * FROM `pages` WHERE `page` = '".$_GET['page']; // As illustration $row = mysql_fetch_row($result); include_once("/templates/".$row['template'].".php"); template.php ... <body> <p><? getContent; ?></p> ... I'll give it a try, but it appears kind of nested to me .. Thanks! Quote Link to comment Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Move the getContent function to its own class, then instantiate the class in each template, and call the function within it. Make sense? Quote Link to comment Share on other sites More sharing options...
axtg86 Posted July 6, 2009 Author Share Posted July 6, 2009 Short: no it doesn't... I don't get to use classes much (just db actions), but I'm having the most trouble with "instantiate the class in each template". Put: class content{ public function getContent(){ $filestream = @fopen("content.php", "r"); $filecontent = @fread($filestream, @filesize("content.php")); echo $filecontent; } } in a separate file and call $content->getContent(); in each template file? Quote Link to comment Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Ahh wait one sec, does the content within the content file never change? If it doesn't you can use includes for both. Like so: <?php // index php $result = "SELECT * FROM `pages` WHERE `page` = '".$_GET['page']; // As illustration $row = mysql_fetch_row($result); include_once("/templates/".$row['template'].".php"); // template page ?> <body> <p><? include("content.php"); ?></p> Quote Link to comment Share on other sites More sharing options...
axtg86 Posted July 7, 2009 Author Share Posted July 7, 2009 That is going to be worth a try tomorrow morning. To prevent myself of skipping sleep I've set my PC to auto-shutdown at 2.10am. Which is in 9 minutes . I'll try the latest suggestion and possible other thoughts tomorrow and will report back (either solved or with follow-up). Thanks for your time (so far )! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.