Forum Moderators: coopster
I'm intending to use a template for the site, such as using a .inc header and footer.
Before looking in depth at php and mysql I was under the impression that for each page I would have variables such as $title $description $author $articledate and $article which would pull data from the corresponding info stored in the mysql database.
Is this the best way to approach such a website? I'm just questioning it because I can't find specific information on the processes needed to set up an article based dynamic website.
(You would put the following code snippets in your template.)
So, say someone goes to "happypage.htm".
<?php
$thispageURL = $_SERVER['REQUEST_URI'];
?>
Now $thispageURL contains this:
"http://www.mydomain.com/happypage.htm"
Then you grab all your page data from the database, using that URL in the "WHERE" clause. Basically you're grabbing all the data associated with "happypage.htm".
$query="SELECT * FROM pageinfotable WHERE pageURL=".$thispageURL." LIMIT 1";
$result=mysql_query($query);
You don't need to assign each field to a variable. Do this:
if (mysql_num_rows($result)){
$pageinfo=mysql_fetch_array($result);
}else{
// deal with this as a 404 error
}
Now your data is stored conveniently in an array called $pageinfo, and you can use it wherever you need to.
<title><?php print($pageinfo['title'])?></title>
Have you heard of SMARTY [smarty.php.net] templates? I don't use them much myself, but I know other developers who swear by them, & use them for everything.
I just need something cleared up with respect to the urls/file names which i will use.
Firstly I'm assuming in your example that you've used mod_rewrite to process .htm files as php files and that's why you've used happypage.htm
When I add the relevent text to the database do I need some kind of primary key or is this something to do with the url?
Also if I'm using text names for the files, is there a way of stopping the duplication of file names? I assume one was of doing this is to use values instead, but with search engines now taking into account the file names, maybe getting rid of text is not a good option.
Hope someone can help me again and i hope i'm not talking too much rubbish!
Cheers
In my database, I do have a primary key which is a standard autoincrementing integer. I don't use the URL as a primary key, because... well just because. In my CMS, it's possible to have more than one node on the navigation tree mapped to the same URL. it's unusual, but true.
Here is a stripped-down version of my table:
- pageID int(11) Primary auto_increment
- pagename varchar(50)
- pageURL varchar(250)
- pageParent int(11)
// pageParent is used for defining a navigation hierarchy
- pageTitle varchar(150)
- pageMetaKeys longtext
- pageMetaDescription varchar(200)
- pageContentHTML longtext
My table has extra fields defining things like banner ads, user ratings, Google PR, featured products, and many other things that are chosen on a page-by-page basis.
Also if I'm using text names for the files, is there a way of stopping the duplication of file names?
It depends what you're doing. Don't duplicate file names, if you can help it. My site has a few dozen "index.htm" files, but they all have different paths, eg.:
dir1/index.htm,
mystuff/index.htm,
my/stuff/index.htm.
I'm sticking with a strategy where each page actually has a file on the server and a unique URL. Google likes it.
You could serve an entire site like this:
index.htm?page=12
index.htm?page=425
index.htm?page=4
index.htm?page=8827
index.htm?page=324
index.htm?page=102
But I personally prefer meaningful, unique file names for each page.
Sticky me and I'll send you a link to a demo of the CMS I built using these methods.
Cheers,
httpwebwitch