Generate static HTML from PHP/MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deuce
Forum Newbie
Posts: 12
Joined: Sun Jan 25, 2009 5:23 pm

Generate static HTML from PHP/MySQL

Post by Deuce »

I've been looking around and can't seem to figure out what I need to do here.

I have a script that pulls a list of names from the database, but each name needs to be linkable so when you click on it, you will get a static page that list all of the person's information.

When I add a new person to the database the page should automatically be generated and the URL should be put into the database with the person's info. So later on when the list of names is called the URL is put into the list.

I can do the second part, my trouble is figuring out a way to generate the page and writing it to the database.
Also, if the person is deleted the file should be deleted with it, but I am sure I can figure that part after I can figure out how to create the stinking page.

Any help or pointers would be appreciated. :banghead:
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Generate static HTML from PHP/MySQL

Post by Ziq »

Firstly I cannot figure out why do you want to create a static file with personal information about a name? Maybe better to store this information in a database?

Where exactly do you have the problems? Was you trying to do something yourself?
Deuce
Forum Newbie
Posts: 12
Joined: Sun Jan 25, 2009 5:23 pm

Re: Generate static HTML from PHP/MySQL

Post by Deuce »

I think I figured out what I need to do.
To give each page a unique name that can never be reused I did

Code: Select all

$pageName = md5(date("Ymdis")) . ".php";
And then I used the variable to put it into the database with the rest of my INSERT statement.
Then calling it out is just as easy.

I then used

Code: Select all

$pageFile = fopen("page/".$pageName, 'w');
to generate the actual file (make sure the folder has WRITE properties)

and then I did

Code: Select all

$pageOutput = "<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\" \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\">
    <head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
    <title>Page</title>
    <link rel=\"stylesheet\" type=\"text/css\" href=\"../css/style.css\">
    </head>
    
    <body>
    <div id=\"wrapper\">
    <p>.....</p>
    </div>
    </body>
    </html>";
 
    fwrite($pageFile, $pageOutput);
    fclose($pageFile);
Seems to work like a champ!
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Generate static HTML from PHP/MySQL

Post by Ziq »

Code: Select all

$pageName = md5(date("Ymdis")) . ".php";
Why do you use this?

In 99% of tables should be a primary key. I think better use it for filename.

Code: Select all

$pageName = $unique_id . ".php";
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Generate static HTML from PHP/MySQL

Post by Skoalbasher »

Why do you want to make all these pages? This could be done all with 1 page using php. Not sure your application, but seems like a waste of space to me.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Generate static HTML from PHP/MySQL

Post by John Cartwright »

Skoalbasher wrote:Why do you want to make all these pages? This could be done all with 1 page using php. Not sure your application, but seems like a waste of space to me.
There are many reasons to do this. For instance, an application that is write once - read many, it is pointless to make a database hit for each request considering they will always be the same. Otherwise known as caching.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Generate static HTML from PHP/MySQL

Post by Skoalbasher »

John Cartwright wrote:
Skoalbasher wrote:Why do you want to make all these pages? This could be done all with 1 page using php. Not sure your application, but seems like a waste of space to me.
There are many reasons to do this. For instance, an application that is write once - read many, it is pointless to make a database hit for each request considering they will always be the same. Otherwise known as caching.
But.. they aren't the same. It's a different person on each page. I understand that it can be done. I really just don't see the point. If you want to delete a page, just delete the user from the DB. He doesn't show up in the list anymore and there's nothing else to be done. I mean, you're going to be pulling the same info from the db to create the page.. right? Why not just do it real time, so when something changes, you don't have to change the page?

I really am not trying to be an ass, just trying to understand why you would want to do something like that. It seems like 1 page, pulling up the exact same information except for user data would be better than creating 1 page per user.

On a side note. You're saying having a bunch of pages with the exact same information is good? I've always been taught to duplicate the least amount of data possible. It's not like pulling user data every once in a while from a DB is going to crash or overload the server.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Generate static HTML from PHP/MySQL

Post by John Cartwright »

Skoalbasher wrote: But.. they aren't the same. It's a different person on each page. I understand that it can be done. I really just don't see the point. If you want to delete a page, just delete the user from the DB. He doesn't show up in the list anymore and there's nothing else to be done. I mean, you're going to be pulling the same info from the db to create the page.. right? Why not just do it real time, so when something changes, you don't have to change the page?
I never said each person's page is going to be the same. I said if a particular persons page is not going to change often then it makes sense to implement caching.
Skoalbasher wrote:On a side note. You're saying having a bunch of pages with the exact same information is good? I've always been taught to duplicate the least amount of data possible. It's not like pulling user data every once in a while from a DB is going to crash or overload the server.
It's entirely dependant on your requirments. Like I said a system that is write once - read many should generally implement a caching mechanism of some sort. What if this site was extremely popupular and instead of "pulling user data once in awhile from the databse", try pulling thousands of users information many thousands of times a day. It doesn't make sense to retrieve the information we already know hasn't change (and if it is changed it is a simple matter of invalidating the cache and regenerating the page). Not only does this improve performance, but also decreases bandwidth. Not to mention it is very possible to implement caching with a single page, and furthurmore, we can even choose to cache only specific information and leave the rest dynamic.



I never said this was the best way to approach this particular situation. You simply asked why someone would want to do this.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Generate static HTML from PHP/MySQL

Post by Skoalbasher »

John Cartwright wrote:
Skoalbasher wrote: But.. they aren't the same. It's a different person on each page. I understand that it can be done. I really just don't see the point. If you want to delete a page, just delete the user from the DB. He doesn't show up in the list anymore and there's nothing else to be done. I mean, you're going to be pulling the same info from the db to create the page.. right? Why not just do it real time, so when something changes, you don't have to change the page?
I never said each person's page is going to be the same. I said if a particular persons page is not going to change often then it makes sense to implement caching.
Skoalbasher wrote:On a side note. You're saying having a bunch of pages with the exact same information is good? I've always been taught to duplicate the least amount of data possible. It's not like pulling user data every once in a while from a DB is going to crash or overload the server.
It's entirely dependant on your requirments. Like I said a system that is write once - read many should generally implement a caching mechanism of some sort. What if this site was extremely popupular and instead of "pulling user data once in awhile from the databse", try pulling thousands of users information many thousands of times a day. It doesn't make sense to retrieve the information we already know hasn't change (and if it is changed it is a simple matter of invalidating the cache and regenerating the page). Not only does this improve performance, but also decreases bandwidth. Not to mention it is very possible to implement caching with a single page, and furthurmore, we can even choose to cache only specific information and leave the rest dynamic.



I never said this was the best way to approach this particular situation. You simply asked why someone would want to do this.
Thanks for explaining that to me. The first time you told me, I didn't really understand. That makes a whole lot more sense. :D
Post Reply