Generating Dynamic Pages from MySQL DB

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
flagday
Forum Newbie
Posts: 3
Joined: Mon Jul 28, 2008 11:49 pm

Generating Dynamic Pages from MySQL DB

Post by flagday »

Sorry if I should've put this in the Databases forum, sees like my problem is with php, though.

I have a table with all of the pages and their content already created (fields for URL, Title, Meta, H1, Body, etc.), and I'm able to list all of the records via an array and create a link to the dynamic pages I want to go to:

Code: Select all

while($row = mysql_fetch_array($result)){
    $urlstring = str_replace(" ","-",$row['parent']);
    echo "<a href=\"" .  $urlstring . ".html\">" . $row['parent'] . "</a><br />";
}
This creates a nice home page with links like this:

Code: Select all

<a href="Page-One.html">Page One</a>
<a href="Page-Two.html">Page Two</a>
<a href="Page-Three.html">Page Three</a>
Of course, there is no Page-One.html, etc., and I don't want to have to create them; the whole point is to have them dynamically generated when the DB is altered (via phpMyAdmin). So, how do I go about telling php to generate these pages? Is there a function I'm missing?

I've been looking for this for quite some time, and I'm not even sure it's a php issue. Recent search results have me thinking this is best done via mod_rewrite, but, knowing next to nothing about .htaccess, I'd rather do it in php, if possible. The whole reason I'm not using an off the shelf CMS is because I want to keep it simple so I can fix problems down the road, and php is about all I know when it comes to server-side stuff.

Anyway, any help would be greatly appreciated. TIA.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Generating Dynamic Pages from MySQL DB

Post by WebbieDave »

flagday wrote:I'm not even sure it's a php issue
You're right. The web server will be the one calling your page-generating php script. mod_rewrite is a good way to go. Alternatively, you can make your page generating script the 404 handler (won't work right for POST data, though).

mod_rewrite isn't that difficult if you already have some regex experience. There are many fine apache forums out there to help you with this.

If you don't need to worry about getting form post data, you can have your script handle 404 requests. You need an .htaccess that contains:

Code: Select all

ErrorDocument 404 /your-404-handler.php
Then, your handler can analyze $_SERVER['REQUEST_URI'] and build the right page. Make sure to override the 404 with:

Code: Select all

header('HTTP/1.x 200 OK');
flagday
Forum Newbie
Posts: 3
Joined: Mon Jul 28, 2008 11:49 pm

Re: Generating Dynamic Pages from MySQL DB

Post by flagday »

WebbieDave wrote:you can have your script handle 404 requests.
Wow. I never would've thought of that, because it makes every page a 404, but by handling the 404 like a page, the script will create the clean html files I want.

F'n brilliant. You people really know how to think about this stuff.

Thank you.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Generating Dynamic Pages from MySQL DB

Post by WebbieDave »

You're very welcome.
Post Reply