Thread: Blogs...?
View Single Post
Old 03-03-2008, 03:43 PM   #7 (permalink)
abiko
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

You're welcome. *blushes*
As for Aarons answer :)

For a simple app like a blog - you have a users table and a blog article table.
Now user logging in/out is another story, but how to select..
First - the simple way.
The table -

Code:
CREATE TABLE `blog_articles` (
  `id` tinyint(4) NOT NULL auto_increment,
  `title` varchar(48) character set utf8 collate utf8_bin NOT NULL,
  `content` text character set utf8 collate utf8_bin NOT NULL,
  `date` varchar(10) character set utf8 collate utf8_bin NOT NULL,
  `author` tinyint(2) NOT NULL,
  `views` tinyint(5) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8_bin AUTO_INCREMENT=1 ;

So you insert the data to your table, and selecting out is simple.

Code:
SELECT id, title, content, views FROM blog_articles ORDER BY id DESC LIMIT 0,30
This will select 30 rows, starting with id 0 fetch 30 records.
Now - for pagation - you want to select news from the id 6 and 10 of them - the LIMIT statement would look like this - LIMIT 6, 10.

Now let's say you want to select the author for your articles.
There are several ways to do this - one of them is LEFT JOIN ..
Code:
SELECT a.id, a.title, a.content, a.views, u.username
FROM blog_articles as a
LEFT JOIN blog_users AS u on a.author = u.user_id
ORDER BY a.id DESC
LIMIT 0,30
Now this query tells the database to select content from the blog_articles table, as in the first query, and then join the blog_users table - where are all the user data and to select the username where users id is as in articles author.

Now we have touched the interesting topic here - the relations between two tables. To connect two tables they need to have an ID that joines them. In this case blog_articles has the author field - in which is the id of an user from blog_users table.
This could be extended to the part where you want to have categories for your articles.
When you select data you select as in the query above and add another "operator" the category id - that is the blog_categories table.

Also you want to get a one especially great article from the database.
All you need is it's id.
Code:
SELECT * FROM blog_articles where id = 'articlesID' LIMIT 1
Now this is the simple blog database scheme (simple as in vanilla :) ).
I have a feeling that you are a beginner that wants to learn how to work with databases and my suggestions are -
- read some tutorials on that subject - creating your own blog or simple shoutbox example - that is a great foundation to start on.
- read the MySQL (SQLite) docs about certain operators - SELECT, JOIN, LIMIT, ORDER etc. Get to know your database - it will do you good.
Advanced:
- select how you want to access your database - mysql, mysqli [MySQL related] or PDO.

Also when submitting data from the form, and a textarea don't forget to "filter" out your input (HTMLPurifier is a good start ) - removes any suspicious elements and protects you from XSS

I haven't touched the php part of connecting to a database etc presuming you know that.
Please let me know if I can help you with something :)
__________________
Back from sysadmins to the programmers.

Last edited by abiko : 03-03-2008 at 04:12 PM.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
The Following User Says Thank You to abiko For This Useful Post:
SOCK (03-03-2008)