Forum Moderators: coopster

Message Too Old, No Replies

How to create new pages automatically from a form with PHP?

         

ToxinMan

6:53 am on Jan 19, 2008 (gmt 0)

10+ Year Member



Hey there guys!

I was just wondering how would i go about making automated image portal that when you fill out a form and upload your picture to the database, it creates a new page from a template and includes that picture within it?

I know the basics of php and mysql but don't know how to automatically make a new .html or .php from a template and include that uploaded picture on submit?

Thanks HEAPS in advance!
Ricky :)

ToxinMan

8:56 am on Jan 19, 2008 (gmt 0)

10+ Year Member



Please help

venelin13

9:53 am on Jan 19, 2008 (gmt 0)

10+ Year Member



Welcome to the forum!

To explain you how to do this task, let me first set a couple of configuration settings.

//name of the template file
$tpl_file = "profile.html";

//path to the directory at the server, where you store the "template.html" file.
$tpl_path = "/home/ricky/public_html/templates/";

//path to the directory where you will store the auto-generated members pages.
$members_path = "/home/ricky/public_html/members/";

At your template.html file you are using "placeholders", which will be replaced with the posted data. The file looks like:

<html>
<head>
<title>{username} Home Page</title>
</head>
<body>
Hello,<br />
this is my new home page. Here is my data:<br /><br />

{first_name} {last_name}<br />
{city}, {street_address}<br />
Click <a href="mailto:{email_address}">here</a> to send me an email.<br /><br />

</body>
Also, lets suppose your form contains these fields:
username, first_name, last_name, city, street_address, email_address

What I will miss here, because of simplicity:
- I will not explain how to filter and/or validate the posted data;
- I will not explain how to handle the uploaded image;

Step #1
The user has posted the form. Your PHP code has already filtered and validated the posted data and all of it is stored in a new array:

$data['username'] = "king";
$data['first_name'] = "John";
$data['last_name'] = "Doe";
$data['city'] = "New York";
$data['street_address'] = "1 Main Str.";
$data['email_address'] = "john@example.org";

Step #2
You need to store all of the template placeholders into an array.

$placeholders = array("{username}", "{first_name}", "{last_name}", "{city}", "{street_address}", "{email_address}");

Note: the elements possition from the both arrays should match!

Step #3
Get the template.html as a string:
$tpl = file_get_contents($tpl_path.$tpl_file);

Step #4
PHP provides you with a lot of functions to manipulate strings. For this case I prefer to use str_replace() [php.net]

We need now to replate the template placeholders with their correspond values.

$new_member_file = str_replace($placeholders, $data, $tpl);

Step #5
Now we will create a new html file. We will use the submited username to create the file name.

$html_file_name = $data['username'].".html";

Now we will write this file to the members directory:

$fp = fopen($members_path.$html_file_name, "w");
fwrite($fp, $new_member_file);
fclose($fp);

That is all! Now you should be able to access the file as:
http://www.example.org/members/{username}.html

ToxinMan

10:13 am on Jan 19, 2008 (gmt 0)

10+ Year Member



WOW Thanks you so much for that tut!
Thats EXCACTLY what i was wanting :)

BTW if you have any time left over :) can you please explain what this bit of code means.. like fopen and "w"?


$fp = fopen($members_path.$html_file_name, "w");
fwrite($fp, $new_member_file);
fclose($fp);

Thanks so much venelin13!
Ricky

Marcia

10:45 am on Jan 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>can you please explain what this bit of code means.. like fopen and "w"?

fopen [us3.php.net] opens a file.

ToxinMan, it's usually a good idea to get foundation in the fundamental basics and syntax of a language when using scripts, so if anything goes wrong you at least know what to start looking for.

You can get full documentation and look up what anything means at the official PHP [php.net] site, and they've also got a beginning tutorial:

PHP: A Simple Tutorial [us2.php.net]

There's also good information and a resource thread in the forum library [webmasterworld.com] that has references to several good resources. Another thing that's helpful is to read book excerpts; you can find them on just about anything with Google's Book Search, and there's often just enough to explain things that aren't clear enough for non-programmers in official documentation or specifications.

ToxinMan

11:13 am on Jan 19, 2008 (gmt 0)

10+ Year Member



ohok thank you guys