Skip to main content
gamedev.net
🔒 Locked

Website structure

Started by WiseElben May 4, 2004 at 7:28 PM 6 replies 1.5k views
Original Post
WiseElben
WiseElben
I decided to use PHP and MySQL for my website, and ran into a problem: I''m not sure how they are used. For my website (wiseelben.com), I stored information on HTML files and MySQL tables. All my tutorials go into tables, since that will make mangament easier, and all the stuff that wouldn''t be edited much (rules and stuff), are stored in an HTML file and use < ?php include("arg"); ? > . This is a good way of doing things, right? I think I''m doing something wrong since I have to use Javascript to chance the title... Ahh.. there has to be more efficient way somehow... I remember the rules: "If you''re using the same piece of code all the time, there has to be a better way." Now, I know this could be possible to do with a database. I can store the title in the table and echo it at the header ( < title > ). But how would I do this with HTML files? BTW, the tables messes up sometimes in Firefox for some odd reason. Just reload.
"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time." -Chinese Proverb WiseElben.com - Experience Wisdom
Rob Loach
Rob Loach
This is a very difficult thing to figure out as it will determine the structure of your website and future content to come. With the last website I set up (http://www.robloach.net/old I think it is), I didn''t ponder this out and just used PHP without any MySQL implementation. I''m currently re-vamping it though.

The way you''re talking about? You''re suggesting having pointers to HTML content? Yes, that is possible. Just have the table echo out, and then for the cell, put in #include($tablecell). This would equate to something like #include("content/gamereviews/monkeyisland.htm"). And then it would print out all of the HTML file. I would pursonally have all content in the database itself, but that''s just me. Even though it makes it rather tough to edit and everything, it works out in the end. You don''t have to worry about linking up all the database pointers and HTML files.

Also, if the tables mess up sometimes in some browsers, that means that there''s a flaw in your design. Work it out and it''ll make everything fine and dandy.
Rob Loach [Website] [Projects] [
WiseElben
WiseElben
After right-clicking and going to properties on all of your links, I noticed that in your design, you use index.php as the template.

After thinking for a few seconds (still got some voc hw to do), I decided its better if I stick with my dual style, HTML on some, and database on others.

Database:
Articles
Tutorials
Downloads? (Humm... Gotta think some more)

HTML:
Static stuff


"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."
-Chinese Proverb
WiseElben.com - Experience Wisdom
Rob Loach
Rob Loach
Yes, my old website is the lazy-man''s way of doing it. I still haven''t quite figured out how my new one will be done. Keeping content and design seperate is the way to go.
Rob Loach [Website] [Projects] [
GroZZleR
GroZZleR
You're effectively writing a CMS (Content Management System), just without really realizing it.

CMSs work in a similar method to what you have. Content is stored inside of a database table, and _templates_ are stored inside of text files (much like your HTML design).

You'll have one page, index.php which handles the loading of the data, templates and modules.

index.php is really nothing more than this:

<?php
$szModule = "module_{$_GET['Module']}.php";
// Module doesn't exist, load a default one (the news one).

if(!file_exists($szModule))
$szModule = "module_news.php";
include($szModule);
?>


Basically it captures the query string from the title, which contains the word 'Module' and a value to define on what module you wish to load. It'll than "include" that module, which will effectively run it.

So lets assume we're loading the news.

Your link (for your news) will look like this:
index.php?Module=news

If you want to load a different module (say you want to make a link to the articles section of your site) you just swap in a new value for Module.

Example:
<a href="www.mysite.com/index.php?Module=articles">Read My Articles!</a>

OKAY.

Now we've got module_news.php running, whats inside of that? The code to make the news run, of course!

Lets say the news has two templates used with it, the 'base page' (your basic layout) and a template specific article (just a basic table with a header, body for the article and footer for the date).

Those templates will look something like this:
Base Page:

<html>
<head><title>My Page : Viewing News</title></head>
<body>
<h1>Welcome to my Page!</h1>
In The News:
{NEWS_ARTICLES}
<hr>
Thanks for visiting!
</body>
</html>


Notice the {NEWS_ARTICLES}, that'll be where your articles end up going.

Heres what your individual article template might look like:

<b>{ARTICLE_HEADER}</b><br>
{ARTICLE_BODY}<br>
<u>{ARTICLE_FOOTER}</u>


Very simple.

So how do we make these templates 'run'? Through the module of course =)

The code for the module will look like this:


<?php
// Check for errors, all that other shit.


// Load the templates:

$BaseTemplate = file_get_contents("templates_news.txt");
$ArticleTemplate = file_get_contents("templates_article.txt");

$AllArticles = ""; // Blank, but won't be shortly.


// Connect to the database, etc.

// Run the query here, lets say SELECT * FROM `News`


// $Articles is now this particular row of the query result set.

while($Articles = mysql_fetch_array($QueryResult))
{
// Make our temp article the same as the template for our articles.

$TempArticle = $ArticleTemplate;

// Replace the placeholder information {ARTICLE_HEADER} etc, with real information.

$TempArticle = str_replace("
{ARTICLE_HEADER}", $Articles['DatabaseHeader'], $TempArticle);
$TempArticle = str_replace("
{ARTICLE_BODY}", $Articles['DatabaseBody'], $TempArticle);
$TempArticle = str_replace("
{ARTICLE_FOOTER}", $Articles['DatabaseFooter'], $TempArticle);

// Add this article to all of our articles (.= is for concatenation)

$AllArticles .= $TempArticles;
}

// Okay, we're done with the articles, lets replace the template with all the info and we're done.

$BaseTemplate = str_replace("
{NEWS_ARTICLES}", $AllArticles, $BaseTemplate);

// All done, echo this bad boy for display.

echo $BaseTemplate;

// Disconnect from the database.

?>


If you just want a static page (no database content), in the module just don't str_replace or anything like that, just file_get_contents the template and echo it right away.

And thats the basics of a CMS templated solution. The best part of this setup, is anyone not familiar with PHP but knows a decent amount of HTML can alter your templates. It also seperates layout from content, so changing your entire sites design is only a matter of changing a few templates.

Good luck, PHP rocks and I am its Messiah.

(This is without a doubt, my longest GameDev post ever)

[edited by - GroZZleR on May 5, 2004 11:57:14 AM]
Kahsm
Kahsm
Yup Yup.

Look good to me, I was about to reply with the same thing as Grozzler until I actually read his post. That's how I generally handle CMSs as well.





const char szName[] = "Kahsm";

[edited by - Kahsm on May 6, 2004 7:51:46 PM]
-------------------------------------------------------------------Life is short so go on and live it, cause the chicks dig it.- Kahsm
template
template
Don''t forget to do this before including a page:


str_replace("..", ".", $szModule);


That will prevent anyone from listing the files in the parent directory.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.