[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


create multiple php pages from single template source file

Posted on 2005-05-04
Medium Priority
Last Modified: 2008-03-17
Hello Experts! Here's one for ya.

I am needing to create multiple php pages from single template source file.
I need each file to be named from a list of keyphrases that come out of the database.


page names:

In side the title and kywords change based on the keyphrase.

If the key phrases have spaces like keyword more keywords
the page should be: keyword-more-keyphrases.php
dashes in place on the space.
I need help with this ASAP.

Question by:jbrashear72
  • 5
  • 4
  • 2
  • +1

Accepted Solution

punkstar earned 1000 total points
ID: 13933428
Okay.  Lets say that we store all of the pages securely in "_content".

Inside the template you need to have markers for the tiltle ([TITLE]) and for keywords ([KEYWORDS]).

Now for the php.

//get the tempalte file
$template = file_get_contents("template/template.tpl");

//if your keyphrases come out of the database as an array then you will
//need to loop through the array
while($i = mysql_fetch_array($dosql))
  $keyword .= $i." ";

//now we have the list of keywords we clean it up and add
//the dashes

$keyword  = trim($keyword);
$original_kw = $keyword;
$keyword = str_replace(" ","-",$keyword);

//now we have the filename
$content = file_get_contents("_content/".$keyword.".php");
$content = explode(" ",$content);

//now we would loop through the
foreach($content as $key => $value)
     case "[TITLE]":
       //replace with your title result
    case "[KEYWORD]":
      $content[$key] =  $original_kw;

I haventy actually tested that script, so there is bound to be errors.  Also if there are any whitespaces or tabs that are not needed in the template file, it will affect the reading.

Hope this atleast sheds some light on how i would do it!

LVL 25

Expert Comment

by:Marcus Bointon
ID: 13933803
This seems like an ideal application for mod_rewrite. You'd still use the template as punkstar says, but you only need one PHP file to which all keywords are mapped. The rule could be:

RewriteRule ^/keyword-([-\w]*)\.php$ keyword.php?keywords=\1 [L,PT]

This would mean that a link such as:

keyword-something-something.php gets mapped to keyword.php?keywords=something-something

You can then dismantle the keywords in your script using something like:

$keywords = explode('-', $_GET['keywords']);

And pass them into the template as punkstar says. This way you only need one PHP script and one template to handle all possible keyword variations.

Author Comment

ID: 13935159
punkstar you are on the right track!
not sure I know what you mean by:

>>>> Okay.  Lets say that we store all of the pages securely in "_content".

Also is there a way to run this server side? say I have a web script that sets it up as a job.
Runs it then starts a new job doing the same thing with a new list of keywords.
Also after it completes I would like it to send me a mail meesage saying that it was done.
say it was pushing out 1000 pages. I want to run it then move on to a new project.
Also is there a way to do this with multiple servers?
Have a master server send these jobs to other servers to run so I can spread out the load across 3 servers.
BTW <removed - PAQ_Man> is my email address.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 25

Expert Comment

by:Marcus Bointon
ID: 13935276
Why persevere with that approach when URL rewriting will deal with an infinite number of keywords instantaneously? For each keyword combination that you want to handle explicitly, you just need to add handler code for that combination. It's far less wasteful than having thousands of separate files, especially when they will be 99% identical.

All you need to do for multi-server deployment is to copy the updated PHP file to each server, which you could achieve automatically via rsync or an automated subversion checkout.

Author Comment

ID: 13935430
I understand your point but after the files are created they will be moved to different server locations.  another reason is for ranking with google.
The pages need to be real not just created on the fly.  thans but punkstar is giving the answer that I was really looking for. I do apreciate you expertise in this matter.
Thank you.
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13936042
Google can't tell if your pages are real or not. It's only the URLs that lead to them that matter. With a rewrite rule the URLs could be absolutely identical to those for separate page files, so there is no visible external difference, and it saves you from having to actually create and move around several thousand unnecessary files. Almost any article on 'search engine friendly' URLs will also point you in the direction of mod_rewrite for exactly this reason.

Punkstar's example places keywords within the template - it doesn't generate any new files at all, so I don't quite see how you can classify it as a solution. I have a feeling that you may be using Smarty for your template, so much of the code is redundant anyway. There doesn't seem to be any good reason to have external files for each keyword at all, especially when you apparently also have a database record for each.

Author Comment

ID: 13936260
Well again I apreciate this but I do need to loop through the keyphrases and also replace content in the pages.
I have lots of data and don't want thousands of database calls. this is also to relieve some of the stress on the database.
Another reason is I want control of the the files right down to the indavidual file level. My reason is simple I just need to do this as strange as it may sound to you.
Again thank you.
The solution I am looking for is creating a file for each keyphrase and naming it after the keyphrase.
I need to replace content and push these files out on a cron, I can take care of the cron.
Thanks again.
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13937405
OK. Are you using Smarty?

Author Comment

ID: 13937562
No I dont have a template engine at this time. I was looking at Smarty.
Really it is more simple than that.
say I have a teplate file that I need to auto genarate.
File is like this:

<title>[TITLE] [KEYWORD]</title>
<? include ("content.inc"); ?><BR>
<? include ("ads.php?ad=[KEYWORD]"); ?>
I need to loop it with the keyparases and replace the file name with the keyphrase "my key phrase my-key-prase.php"
dump it into a project directory and email me when it is done.
LVL 10

Expert Comment

ID: 13947464
Sorry, just looking through the question jbrashear72, just so you know you cannot use email to communicate the answers as that will destory the PAQ since we would be missing key parts.  I have asked CS to remove it for you.


Expert Comment

ID: 13961733
...i give up!

Sorry for giving an irrelevant answer :-S, hope it kinda helped you in some way.


Author Comment

ID: 13969439

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question