FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[PhP] News Posting Tutorial (code, actually ^^')





Souleater
Hello everyone,

This tutrorial may help many new webmasters who want to make their index page more Dynamic Smile .... actually it's a bit of code not a tutorial but comments can help you understand a bit more of what the script is ... I request that a minimum php knowledge should be acquired by the person using my script Smile.
You should have a Mysql database

General Info [Features of the script]:
- Name of the Author
- Mailto the Poster
- Date of the posted News
- Title of the posted news (silly but it's a feature though :p)
- Avatar of the poster
- And the news itself Razz

All of that will be on your index page while you have finished all your configs Smile

Before you start your scripting you should know that the code is formed by 4 main pages => config.php // show_news.php // list_news.php // write_news.php ... and a Mysql database ...

here is the code to paste on your database :

Quote:

CREATE TABLE news
(
id int(11) not null auto_increment;
name varchar(255) not null;
title varchar(255) not null;
mail varchar(255) not null;
avatar varchar(255) not null;
message text not null;
time bigint(20) not null;
);

P.S : just passing by so i am not sure the sql request is a good one, just post after seeing any prob ^^

Or just use phpmyadmin it'll be easier => /!\ id should be in auto increment /!\

Well let's start by ... let me see ^^

config.php

Quote:

<?
mysql_connect("yoursqlhost", "yourusername", "yourpassword"); // Fill the bold text with your configs
mysql_select_db("yourdatabasename");
?>


That's it for config ^^

Now the show_news.php file, which is the easiest

show_news.php
Quote:

<?
require('config.php');

$res = mysql_query('SELECT * FROM news ORDER BY id DESC LIMIT 0, 5');
while ($data = mysql_fetch_array($res))
{
?>

<div>
<table width="490" border="0">
<tr>
<td width="292"><b><? echo $data['title']; ?> </b></td>
<td width="146" align="right"><b><a href="mailto:<? echo $data['mail']; ?>"><? echo $data['name']; ?></a></b> <i> le <? echo date('d/m/Y', $data['time']); ?></i></td>
</tr>
</table>
<table width="490">
<tr>
<td width="118" align="center" valign="top"><img src="<? echo addslashes($data['avatar']); ?>"></td>
<td width="464">
<?
$contenu = nl2br(stripslashes($data['message']));
echo $message;
?>
</td>
</tr>
</table><br>
</div>
<?
} // don't touch thiiis xD
?>


Copy paste copy paste ^^ but try to look good in the code so as you can understand how all it works Wink

let's go on to list_news.php

list_news.php
Quote:

<?
require('config.php');

if (isset($_POST['name']) AND isset($_POST['mail']) AND isset($_POST['avatar']) AND isset($_POST['title']) AND isset($_POST['message']))
{
$name = addslashes($_POST['name']);
$mail = addslashes($_POST['mail']);
$avatar = addslashes($_POST['avatar']);
$titre = addslashes($_POST['titre']);
$contenu = addslashes($_POST['contenu']);
// We try to find out if we were going to modify the news ...
if ($_POST['id_news'] == 0)
{
// No modifs ?? lets put some data on the tables :p
mysql_query("INSERT INTO news VALUES('', '" . $name . "', '" . $title . "', '" . $message . "', '" . time() . "', '" . $mail . "', '" . $avatar . "')");
}
else
{
// If we want to modify, let's just update all the data
mysql_query("UPDATE news SET name='" . $name . "', title='" . $title . "', message='" . $message . "', mail='" . $mail . "', avatar='" . $avatar . "' WHERE id=" . $_POST['id_news']);
}
}

/*
Verify if ever we want to delete the news
*/
if (isset($_GET['delete_news']))
{
// let's delete data Very Happy
mysql_query('DELETE FROM news WHERE id=' . $_GET['delete_news']);
}
?>

<table width="487">
<tr>
<th>Modify</th>
<th>Delete</th>
<th>Title</th>
<th>Date</th>
<th>Poster</th>
</tr>

<?
$res = mysql_query('SELECT * FROM news ORDER BY id DESC');
while ($data = mysql_fetch_array($res)) // let's start a loooooop
{
?>

<tr>
<td align="center"><? echo '<a href="write_news.php?modify_news=' . $data['id'] . '">'; ?>Modify</a></td>
<td align="center"><? echo '<a href="list_news.php?delete_news=' . $data['id'] . '">'; ?>Delete</a></td>
<td align="center"><? echo stripslashes($data['title']); ?></td>
<td align="center"><? echo date('d/m/Y', $data['time']); ?></td>
<td align="center"><? echo stripslashes($data['pseudo']); ?></td>
</tr>

<?
} // don't touuuch Neutral end of the loop :p
?>


pfioouuu yahoo you've just finished to code the listing of the news :p
let's rock on !!!

write_news.php
Quote:

<?
require('config.php');

if (isset($_GET['modify_news']))
{
// let's take out some data from the table
$res = mysql_query('SELECT * FROM news WHERE id=' . $_GET['modify_news']);
$data = mysql_fetch_array($res);

// On place le titre et le contenu dans des variables simples
$name = $data['name'];
$mail = $data['mail'];
$avatar = $data['avatar'];
$title = $data['title'];
$message = $data['message'];
$id_news = $data['id'];
}
else
{
// Just let this like that, it means that we are filling in a blank form
$name = '';
$mail = '';
$avatar = '';
$title = '';
$message = '';
$id_news = 0;
}
?>

<form action="list_news.php" method="post">
<table width="422">
<tr>
<td width="100">Name :</td><td width="200"> <input type="text" size="30" name="name" value="<? echo $name; ?>" /></td>
</tr>
<tr>
<td width="100">Mail :</td><td width="200"><input type="text" size="30" name="mail" value="<? echo $mail; ?>" /></td>
</tr>
<tr>
<td width="100">Avatar's Url :</td><td width="200"><input type="text" size="30" name="avatar" value="<? echo $avatar; ?>" /></td>
</tr>
<tr>
<td width="100">Title :</td><td width="200"><input type="text" size="30" name="title" value="<? echo $title; ?>" /></td>
</tr>
</table>

<p>
Message :<br /> <br>
<textarea name="message" cols="80" rows="10">
<? echo $message; ?>
</textarea>
<br />
<br>
<input type="hidden" name="id_news" value="<? echo $id_news; ?>" />
<input type="submit" value="Post !" />



hahahaha Congrats we've finished !!! create a folder on an ftp named "admin" upload
the files in it => go to : yourdomain/admin/write_news.php ... write a news post it .... look into show_news.php your news is there ^^ with all
the features explained above :p

one last thing so as you can have you news on you index page; insert this code where you want that the news should apparate :

<? include('admin/show_news.php'); ?>

that's all, hope you've liked my code :p

You may create an admin control panel just a normal page that links to the different other pages ^^ ... ahh i've almost forgot you can protect your folder "admin" by an ".htaccess" and".htpasswd" ... i'll try to have a tutorial for it i just haven't got time right now sorry Sad

Ciao all,
Souleater

P.S: If ever you want to re-post my script just make sure you put this versionning infos on top of it :
//**********************************
// /-/ Script By Siteduzero
// /-/ largely modified by souleater
//**********************************
// For http://Frihost.com/ all right reserved
//
//**********************************
//*******Script versioning*************
// /-/ News posting script V 1.0
// Original script by siteduzero staff,
// too shallow for me Neutral
// /-/ News posting script V 1.2
// Largely modified By Souleater
// Added features :
// - Translation French => English
// - Author name
// - Mailto Author
// - Avatar
// - Modified show_news.php design
//**********************************
Related topics
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.