May 13, 2007 4:39:38 GMT -5
This tutorial is for SI only! May not be posted anywhere else, and only by me!Hello,
I'm starting a new tutorial in a few parts as it will take some coding and testing. I'll post them as quick as I can but please be patient for the next parts.
I'm writing them out of my head, so if anything errors please PM me!What will you learn to make in this tutorial?
- The MySQL tables
- Database Connection
- Grabbing Articles
Ok, now let's start of with our MySQL code. We want to store the news articles, and the comments. The comments will be grabbed by the ID of the news article.
Code (MySQL):
CREATE TABLE `comments` (
`id` int(11) not null auto_increment,
`art_id` int(11) not null,
`ip` varchar(15) not null,
`name` varchar(25) not null,
`email` varchar(255) not null,
`message` text not null,
PRIMARY_KEY( id )
);
CREATE TABLE `articles` (
`id` int(11) not null auto_increment,
`date` int(11) not null,
`name` varchar(25) not null,
`title` varchar(75) not null,
`message` text not null,
PRIMARY_KEY( id )
);
So now we have our MySQL ready.
Let's make a new file:
config.inc.phpThis file will hold our database connection and anything wich you need in all the files.
First we connect to our database and select the right database:
Code (PHP):
<?php
$conn = mysql_connect( "localhost", "username", "password" );
mysql_select_db( "database", $conn );
?>
The connection is done. Now we want our articles on the home, so we create a new file:
index.phpThe first thing we do is include our config!
Code (PHP):
<?php
include 'config.inc.php';
?>
Now you should have an empty page. Now it's time to grab the articles.. Bu we can't just grab and through it on the site. We need to check if there are any messages and we need to add security.
To make it easy to edit later, we make a variable wich holds the number of articles you want to grab on the home. I'm setting it to 5.
Code (PHP):
<?php
include 'config.inc.php';
$articles = 5;
?>
Now it's time to get the latest articles. With the mysql_query we are going to select the articles, with a limit of $articles. And we want the newest one on top, so we set an order on DESC.
Code (PHP):
<?php
include 'config.inc.php';
$articles = 5;
$get = mysql_query( "SELECT * FROM `articles`ORDER BY id DESC LIMIT " . $articles );
?>
When that's done, we are going to look if there are any articles. If not, then return a message saying there is no news. We'll use the mysql_num_rows function for this.
Code (PHP):
<?php
include 'config.inc.php';
$articles = 5;
$get = mysql_query( "SELECT * FROM `articles` ORDER BY id DESC LIMIT " . $articles );
if( mysql_num_rows( $get ) == 0 )
{
echo 'There is no news.';
} else {
echo 'There is some news!';
}
?>
Now, where it says 'There is some news!' we are going to through the selected news articles in an array and loop through them. For this we'll use a while loop and a mysql_fetch_assoc.
To test it we will echo the title of the news articles.
Code (PHP):
<?php
include 'config.inc.php';
$articles = 5;
$get = mysql_query( "SELECT * FROM `articles` ORDER BY id DESC LIMIT " . $articles );
if( mysql_num_rows( $get ) == 0 )
{
echo 'There is no news.';
} else {
while( mysql_fetch_assoc( $get ) )
{
echo $get[ 'title' ] . "<br />\n";
}
}
?>
Let's say you've got 3 articles, then the index would now look like:
News Article Name 3
News Article Name 2
News Article Name 1
Let's add the name and message to it. I'm thoughing the message through htmlspecialchars() and nl2br(). htmlspecialchars will disallow HTML codes, and nl2br will replace enters with <br />(html enters).
Code (PHP):
<?php
include 'config.inc.php';
$articles = 5;
$get = mysql_query( "SELECT * FROM `articles` ORDER BY id DESC LIMIT " . $articles );
if( mysql_num_rows( $get ) == 0 )
{
echo 'There is no news.';
} else {
while( mysql_fetch_assoc( $get ) )
{
echo "<strong>" . $get[ 'title' ] . "</strong> - by " . $get[ 'name' ] . "<br />
" . htmlspecialchars( nl2br( $get[ 'message' ] ) ) . "<br /><br />\n";
}
}
?>
Now it will look like:
Title - by Name
This is
a message
Title - by Name
This is
a message
Now, for the date we have to use the date() because the stored date is a timestamp made with time().
Code (PHP):
<?php
include 'config.inc.php';
$articles = 5;
$get = mysql_query( "SELECT * FROM `articles` ORDER BY id DESC LIMIT " . $articles );
if( mysql_num_rows( $get ) == 0 )
{
echo 'There is no news.';
} else {
while( mysql_fetch_assoc( $get ) )
{
echo "<strong>" . $get[ 'title' ] . "</strong> - by " . $get[ 'name' ] . "<br />
" . htmlspecialchars( nl2br( $get[ 'message' ] ) ) . "<br />
<i>Posted on: " . date( 'd F Y \a\t h:i:s A', $get[ 'date' ] ) . "</i><br /><br />\n";
}
}
?>
Now one article looks like:
Title - by Name
This is
a message
Posted on: 13 May 2007 at 11:39:41 AMThis was part 1, and I hope it helped. Part 2 coming soon!
Greetings,
Mike.