07-28-2008, 09:40 PM
So you want a news system? This is a tutorial on how to make one. No ready-made script but a great (and handy) explanation.
1. Requirements
Basic knowledge of PHP and MySQL, a text editor (e.g. Notepad) and a browser.
2. Database Structure
The database will have to look something like :
table 'news'
- id INT AUTO_INCREMENT,
- author VARCHAR(xxx),
- title VARCHAR(xxx),
- message TEXT,
- date INT,
- PRIMARY KEY(id)
Notice that date is INT. That's because we'll be storing the value of php function time() which gives you a very big nummer that can be converted to dates.
2. Insert Script
The form to insert news should be only accesible by you (and/or assistents). That's why it's nice to include a small check, such as a password check. If you have a complete usersystem, you could include a usercheck to see if the user is e.g. an admin/moderator.
$password should be your desired password.
1. Requirements
Basic knowledge of PHP and MySQL, a text editor (e.g. Notepad) and a browser.
2. Database Structure
The database will have to look something like :
table 'news'
- id INT AUTO_INCREMENT,
- author VARCHAR(xxx),
- title VARCHAR(xxx),
- message TEXT,
- date INT,
- PRIMARY KEY(id)
Notice that date is INT. That's because we'll be storing the value of php function time() which gives you a very big nummer that can be converted to dates.
2. Insert Script
The form to insert news should be only accesible by you (and/or assistents). That's why it's nice to include a small check, such as a password check. If you have a complete usersystem, you could include a usercheck to see if the user is e.g. an admin/moderator.
$password should be your desired password.
Code:
<?php
$password = 'azerty';
if(!empty($_POST['news_author']) || !empty($_POST['news_title']) || !empty($_POST['news_message']) || !empty($_POST['news_password']))
{
if(!empty($_POST['news_author']) && !empty($_POST['news_title']) && !empty($_POST['news_message']) && !empty($_POST['news_password']))
{
// You should check the password to see if it's correct.
if($_POST['news_password'] == $password)
{
// We won't check for SQL Injection. That's something you have to do on your own. And it's not that important because the user already needed a correct password.
$date = time();
$query = "INSERT INTO news(id, author, title, message, date) VALUES('', '".$_POST['news_author']."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."'";
// Insert the query into the database as you like.
}
else
{
echo '<p>incorect password</p>';
}
}
else
{
echo '<p>please fill in all fields</p>';
}
}
<form action='insertnews.php' method='post'>
<table>
<tr>
<td>Your name</td>
<td><input type='text' name='news_author' /></td>
</tr>
<tr>
<td>Title</td>
<td><input type='text' name='news_title' /></td>
</tr>
<tr>
<td>Message</td>
<td><input type='text' name='news_message' /></td>
</tr>
<tr>
<td>Password</td>
<td><input type='password' name='news_password' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
</form>
3. Show Script
Now you have to show the data.
Code:
<?php
$shownews_query = "SELECT * FROM news ORDER BY date DESC";
$shownews_row = mysql_fetch_array($shownews_query);
?>
<table>
<tr>
<td>Author</td>
<td><?php echo $shownews_row['author']; ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo $shownews_row['title']; ?></td>
</tr>
<tr>
<td colspan='2'><?php echo $shownews_row['message']; ?></td>
</table>
And so on, on, on, on and on....
So now you have seen the basics of how to make a news system. You can add as many features as you like, such as categories, showing the last 10 news items, ...
