i have been teaching myself php and mysql and i am in the middle of coding a blog from scratch to know how the code all works and fits together. but i have not been able to figure out how to code a single post page to show one post at a time. i can list all posts on a single page but how do i create links on the main all-entries page to individual entries? for example, i have six posts on my main page, how do i code it so i can click on any post so it will take me to a page where only that post is displayed?
i am really enjoying learning php and mysql but i have hit a wall on the above issue. if anyone can offer a suggestion on this i would greatly appreciate it.
Though I’d keep the site layout as its own template, then ShowArticles (listing all articles) and ViewArticle (displaying a specific article) are basically ‘slotted’ into a position in that layout. This means that changing the one main layout file will change the whole site, whereas using fresh code in every file will require all of the files to be modified.
thanks much, anthony! do i then have to make a change to my html template that i’m using to display all my posts and/or create a new template to display the single posts?
i’m sorry if this may seem like a stupid question. i’ve only been doing this a few weeks.
You need to pass something uniquely specifiying the article you require in the database, usually this is the article.id.
/article.php?id=45
This would display the article with an id of 45, notice the address bar for this forum, right now? Look familiar?
Here’s a quick walk-through.
<?php
/**
* make sure we have an article id passed
*/
if(true === empty($_GET['id']) || 0 === (int)$_GET['id']){
echo 'Sorry, invalid aricle id specified.';
exit;
}
/**
* find the article
*/
$result = mysql_query(sprintf('SELECT * FROM article WHERE id = %d', $_GET['id']));
/**
* did we find it?
*/
if(0 === mysql_num_rows($result)){
echo 'Sorry, the aricle id you specified is non-existant.';
exit;
}
/**
* get the article details
*/
$article = mysql_fetch_assoc($result);
/**
* display 'em
*/
echo $article['title'];
echo $article['content'];