HjalmarCarlson
HjalmarCarlson

Reputation: 868

Dynamically generate pages from a list with php

I need to write a short function to dynamically generate a few very basic pages. At the top of the file I will have an array that lists all of the current pages. Then I need the function to create a page for each item in the array. Here are the items that the page will generate:

$appArray = array('testAppOne', 'testAppTwo', 'testAppThree');

$fileName = 'info-'.$appName.'html';
$appLogo = 'path/logo.png';
$appName = appArray[1];
$src = '<!DOCTYPE html><html><head></head><body><img src='".$appLogo."'><h1>'.$appName.'</h1></body></html>'

So would I create a foreach statement to iterate through the array and $fwrite each file to the specific directory? Or is there a better way to approach this?

I'm aware this would be much easier if we used a database, but we're trying to avoid that for now.

Upvotes: 1

Views: 367

Answers (2)

Petr Brazdil
Petr Brazdil

Reputation: 241

Try using sqlite3, it's pretty cool for small websites! It's database which can start using immediately. If you will use it with dibi (http://dibiphp.com/), it will be amazing for you, I am pretty sure!

Create and connect do sqlite code:

dibi::connect(array(
    'driver'   => 'sqlite',
    'database' => 'sample.sdb',
));

Give it try and you will never regret!

Upvotes: 1

King Skippus
King Skippus

Reputation: 3826

I can't imagine why you'd want to do this instead of using some dynamic page generation mechanism (i.e. a database), but given the constraints of what you're asking, yes, I think that would be the best way to go. Something like this:

$appArray = array('testAppOne', 'testAppTwo', 'testAppThree');

foreach ($appArray as $app) {
    $fileName = 'info-'.$app.'html';
    $appLogo = 'path/logo.png';
    $src = '<!DOCTYPE html><html><head></head><body><img src="'.$appLogo.'"><h1>'.htmlentities($app).'</h1></body></html>'
    $fh = fopen($fileName, 'w');
    fwrite($fh, $src);
    fclose($fh);
}

Upvotes: 1

Related Questions