Jump to content

Different template based on query string


axtg86

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 ;))!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.