1

I've been writing a small blogging system to advance my knowledge of PHP, and I'm having fun writing it. I've been using one page, coupled with jquery's pajinate plugin to display all my blog entries through one page. However, my question here is can I generate a page which displays a blog article with the id of 2?

What I want to do is generate the article, but dump it to a page (content/article-name-here.php). Is there any way of automating the process?

(Sorry if it's not clear)

7
  • 2
    Do you actually want a file to be generated, or just the appearance of a file? The latter is the preferred method and one of the awesome capabilities of PHP coupled with mod_rewrite. Commented Feb 15, 2012 at 17:33
  • Recommendations. 1) Don't aim to have ".php" in the title. Go for extensionless URLs. mod_rewrite is a really easy tool that will let you do this. 2) Rather than using the id, have a unique index on a slug on the database, i.e. 'article-name-here' Commented Feb 15, 2012 at 17:42
  • I have a simple rewrite system in place already :D Commented Feb 15, 2012 at 17:52
  • @Sohnee just for got record, mod_rewrite can handle extensions too, so you can make a URL conform to this.php, that.html, something.jsp or nothing.aspx, and redirect to whatever you want. Commented Feb 15, 2012 at 18:12
  • @ChrisSobolewski - my point was to avoid extensions. Why does a visitor have to care whether the site is .php, .html, .anything. Extensionless URLs are far preferable. Commented Feb 16, 2012 at 9:06

1 Answer 1

2

Here's a basic SQL query:

SELECT * FROM blogEntries WHERE id=2

(Note: Never use SELECT * unless you're actually using all the data)

I have done the same project as you, creating a simple blogging engine, and I use the same page for both the general list and the single article.

Using a URL such as example.com/?blogId=2 you can modify your query:

$SQL = 'SELECT * FROM blogEntries';
//if $_GET['id'] has a value, append it to query
if(isset($_GET['id'])){
    $SQL .= 'WHERE id=' . $_GET['blogId'];
};
//Execute SQL Query
//process your data as you want it to appear

If you want custom URLs for each blog entry, you will want to look in to Mod_Rewrite: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Sign up to request clarification or add additional context in comments.

2 Comments

You shouldn't use SELECT * even if you are using all the fields (or so I've heard).
The argument against SELECT * if you are NOT using all the fields is performance based. The argument against using it if you ARE going to use all the fields is based on maintaining the code, AFAIK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.