Yes you can, however for performance reasons (it will take a few microseconds to load the HTML) it would be quicker to store a reference to a cached file with the html and including that instead.
2/3) Nailed it However you can also use htaccess mod_rewrite to make the urls’ prettier.
A breif code example would be:
// index.php?id=10101
// Query database for the ID, put results into $result
// Query would look something like: SELECT * FROM `ww_pages` WHERE `page_id` = ID;
if(isset($result)){ // lets assume if nothing is in the database, nothing is returned.
include($results['html_cached']);
}else{
// 404 error
}
# .htaccess
RewriteEngine on
RewriteRule ^page/([^/\\.]+)/?$ index.php?id=$1 [L]
I am not understanding what you mean about the caching part.
My articles are pretty formatted/marked up, yet they are simple HTML.
2/3) Nailed it However you can also use htaccess mod_rewrite to make the urls’ prettier.
A brief code example would be:
// index.php?id=10101
// Query database for the ID, put results into $result
if(isset($result)){ // lets assume if nothing is in the database, nothing is returned.
include($results['html_cached']);
}else{
// 404 error
}
What is $result??
What is $results[‘html_cached’]??
# .htaccess
RewriteEngine on
RewriteRule ^page/([^/\\.]+)/?$ index.php?id=$1 [L]
Can you explain what this says in English and how it works?
One strategy used is to store the article as html in your database, so then it can be pulled back for editing, in say a WYSIWYG editor.
When an ‘edit and save’ cycle is performed, you rewrite a straight html file probably with a common header and footer surrounding the article body itself.
This would be termed the cached copy. It is pre-prepared and ready to be served - without necessarily going to the database to get content.
eg
header.html
<html><body><menu></menu>
footer.html
</body></html>
So when an article, lets say articleID=23 is saved, you [fphp]file_get_contents/fphp the header, get the article body itself - perhaps along with storing it in your database, and [fphp]file_get_contents/fphp the footer and store them all using some kind of file write function as:
index.php (pseudo code)
grab incoming $article_text
grab incoming $article_title
store $article_text in the database
$header = file_get_contents('header.html')
$footer = file_get_contents('footer.html')
write to $article_title . '.html' the string:
$header . £article_text . $footer
Clearly index.php will be more complicated than that, but essentially this is what it has to do.
So you can go on just picking up the html file from the public side of your website, which will be slightly quicker than hitting your database. Sometimes this strategy is termed ‘baking’ in that it presents a pre-baked loaf - all ready to go.
It may not be the best solution all the time, for example what if you need to change the menu? You will have to re-bake all of your files…
So you store the main article as HTML in your database, but then export that out, combine it with Header and Footer HTML, and then “bake” a finished article file called <<ArticleTitle>>.html??
You already have $article_text and $article_title as variables.
You perform two operations on them, store in database, and then when successful, go on to bake in a html file with surrounding code. No need to get it from the database again, you already have the text in index.php
This is a simplified answer to your question “what is meant by creating a ‘cache’?”.
Think of it like this if you like:
$header = "<html><body><menu></menu>";
$footer = "</body></html>";
$article_title = "<h1>My Story Today</h1>";
$article_text = "<p>blah etc</p>";
$file_name = 'my-story-today.html'; // ***
// store text and title in your database:
// of course you have to escape the values, but essentially:
"insert into articles (title, text) values ('$article_title', '$article_text');"
// if that operation returns positive ...
file_put_contents($file_name, $header . '<h1>' .$article_title .'</h1>'
. $article_text . $footer)
*** this is the trick turn My Story Today into “my-story-today” (sometimes termed “slugging” - which you can search this forum for) which can be used to a) create the predictable and what must be a unique html file name and b) become the SEO friendly link - so to the second part of your question, how you turn:
/articles/my-story-today
into :
/articles/index.php?articleID=23
OR EVEN
/articles/index.php?article=my-story-today (why do you need an ID at all? use the slug as the unique primary key)
There are lots of other issues which you will have to address, what happens if the article is edited and the title changes? How will I enforce uniqueness in the file name? (or slug)
Accept that this may not be the best solution for you, but it is a strategy you could pursue. At least you have a couple of terms to search the wider web for now.
BTW there is absolutely nothing wrong with just having a public index.php file which does this: index.php?articleID=23
<?php
$id = (int)$_GET['articleID'];
// get the title and the text from the database
"select title, text from articles where articleID = $id"
echo $header;
echo '<h1>' . $row['title']. '</h1>';
echo $row['text'];
echo $footer;
Thats what most seem to do, and its probably the best way to start - then add caching if you are inclined/feel the need. This way if something changes in your menu in the header file, your website is updated immediately.
Not enough to worry about unless you are serving millions of pages, or you are doing multiple sql requests per page (grabbing and building complex menu trees for example) or you have some evidence that this is causing undue stress on your server.
This type of caching (to file system) is only one of many caching techniques.
[google]caching strategies in PHP[/google]
I’m confused about the order of things…
I thought you queried the article from the database by using a URL like:
So while I appreciate your suggestion, it sounds like I need to K.I.S.S. and just access content from my database and save the caching for when I give Amazon.com a run for the money?!
remains in the web browser address bar for the user (and search engines)
So, dumb question,…
In the past I created a physical file called “all-about-creating-dynamic-content.php”. And when I had to reference that file for a link or whatever, I just went into NetBeans (or my file system), copied the file name, and pasted it into a link, include, whatever.
Because the file name was self-documenting, it was very easy to do.
Now if I no longer have physical files, and if every article is ultimately referenced by id=1, id=2, …, id=62194802, then it’s going to be a real pain remembering that “all-about-creating-dynamic-content.php” is id=429876210.
Follow me?
I think this may be a large part of my confusion?! :-/
Lastly, to your earlier point…
Behind the scenes, Apache then re-routes the request to
articles
======
"all-about-creating-dynamic-content"
"All about creating Dynamic Content"
"blah de blah, de blarrrr"
"Technology"
2011-07-06 12:15:00
I would not advocate the use of false keys like id = 123 because:
a) you cannot understand what it happening in your db when looking at all these numeric keys
b) you dont need them
c) its easier to then use Apaches mod_rewrite as some kind of page-controller without all the messing around of un-mapping the ids to titles, as you have quite rightly pointed out
BUT there are drawbacks and I have mentioned them higher up on this thread. If you have further questions about them, then fire away.
articles
======
"all-about-creating-dynamic-content"
"All about creating Dynamic Content"
"blah de blah, de blarrrr"
"Technology"
2011-07-06 12:15:00
Well, I have that, but having a database background, I still through in an “id” as the PK, and will just put a unique index on “urlTitle” (aka “slug”).
I would not advocate the use of false keys like id = 123 because:
a) you cannot understand what it happening in your db when looking at all these numeric keys
b) you dont need them
c) its easier to then use Apaches mod_rewrite as some kind of page-controller without all the messing around of un-mapping the ids to titles, as you have quite rightly pointed out
Okay.
BUT there are drawbacks and I have mentioned them higher up on this thread. If you have further questions about them, then fire away.
I’m sorry, what drawbacks are you referring to?
Here is what I have so far…
I created a page called “article.php” that is my article template, and it is located in my WEB_ROOT.
I also still have a page called “article_index.php” which looks similar to the home page on Ars Technica.
Lose the single quotes. You just said that article.php is at your webroot, so just go to the home directory of the website with ‘/’
<a href="/article.php?id=mail-meters-are-a-good-investment">Mail Meters are a Good Investment</a>
A problem you will hit if you let others contribute to your site or you want your Titles to be constructed with proper English is:
Mail Meters, really a Good Investment?
But some chars are illegal or get turned into the likes of %20 as urls, so you have to be able to reliably slugify Mail Meters, really a Good Investment? to mail-meters-really-a-good-investment
What about articles with the same title? That is a likely scenario so you would also need a published date or something. To make thing less complicated I would always include the ID and place the title in the URL merely as filler data.
/article.php?id=89765&title=doesn’t matter
Then use a rewrite like:
/article/{title}/{id}
This all really a mute point considering everything else that has to be done before this even really becomes relevant.
How so?! How can you display a .php page without a .php file extension??
Keep Ids if you want, the cost is miniscule.
I’ll need it for my Comments module…
Lose the single quotes. You just said that article.php is at your webroot, so just go to the home directory of the website with ‘/’
<a href="/article.php?id=mail-meters-are-a-good-investment">Mail Meters are a Good Investment</a>
Guess I could. I just use my constant to make it easier and safer to switch from Dev to Production.
A problem you will hit if you let others contribute to your site or you want your Titles to be constructed with proper English is:
Mail Meters, really a Good Investment?
But some chars are illegal or get turned into the likes of %20 as urls, so you have to be able to reliably slugify Mail Meters, really a Good Investment? to mail-meters-really-a-good-investment
No other contributors. And I’m still a pretty manual kind of gal.
I will be composing the articles myself and manually typing in what I think is the best “urlTitle” >< “Title”.
Many newspapers use the date as a unique identifier.
/2011/07/my-article
Anyhow, seeing as moot points are being raised:
a) if you are the only one submitting articles, you can easily ensure uniqueness yourself
b) I already articulated that in your case you should use IDs in my reply at #7 above, see how you get on with it, it will work fine.
My advocacy of using slugs is borne from designing multi-user systems where I find ridiculous the constant unmapping of id=1234 to ‘My Title’, and my file cacheing works incredibly efficiently as a result.
I have a site where there are literally 1000s of cached pages and I go directly from SEO friendly URLS to cached page content.
My POV served only to illustrate a possible relationship between caching, mod_rewrite and dynamic content - and at least I aired my points.
The best book I read on all the different types of cacheing was Schlossnagle’s Advanced PHP Programming - but it appears pretty old now.