Елементарна news системка

Автор: Er4o

Здравейте написах една елементарна news системка с админ ареа 🙂
Ето я и нея:

  1. Правите папка images/ и вътре слагате
  2. Правите файл config.php с код:
<?
$host = 'localhost'; // mysql host
$user = 'root'; // mysql user
$mysql_pass = ''; // mysql pass
$db = ''; // database name

$website = "Your "; // website name

$adminname = "admin"; // admin username
$adminpass = "admin"; // admin password
?>

Правите файл connect.php и в него слагате

<?php
include("config.php");
mysql_connect("$host", "$user", "$mysql_pass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());
?>

Нов файл index.php с код:

<?
header("Location: news.php");
?>

Сега и news.php с код:

<?
include("config.php");
include("connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<? echo("<title> $website Website - News</title>"); ?>
<link rel='stylesheet' href='style.css' type='text/css' />
</head>

<form method="POST" target="<? echo $PHP_SELF; ?>">
<body>
<table align="center" width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table align="left" width="20%" class="own" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center" class="menu">Main Menu</div>
<a href="news.php"><img src="images/bullet.gif" width="11" height="11" style="border: 0px;" /> News</a><br />
<a href="admin.php"><img src="images/bullet.gif" width="11" height="11" style="border: 0px;" /> Admin</a><br />
</td>
</tr>
<table align="center" width="75%" class="own" cellpadding="0" cellspacing="0">
<tr>
<td>

<?
$result = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 5") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo("
<div align='left' class='menu'>News: " . $row['title'] . " (" . $row['id'] . ")</div>
<div style='border: 1px solid #FFF; width: 750px; height: 100px; overflow: auto; margin-bottom: 5px;'>
" . $row['text'] . "
</div>
<div align='right' class='menu'>Posted by: " . $row['nick'] . " At: " . $row['date'] . "</div>
");
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>

</html>

Файл admin.php и в него слагате:

<?
include("config.php");
include("connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<? echo("<title> $website Website - Admin area</title>"); ?>
<link rel='stylesheet' href='style.css' type='text/css' />
</head>

<form method="POST" target="<? echo $PHP_SELF; ?>">
<body>
<table align="center" width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table align="left" width="20%" class="own" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center" class="menu">Main Menu</div>
<a href="news.php"><img src="images/bullet.gif" width="11" height="11" style="border: 0px;" /> News</a><br />
<a href="admin.php"><img src="images/bullet.gif" width="11" height="11" style="border: 0px;" /> Admin</a><br />
</td>
</tr>
<table align="center" width="75%" class="own" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center" class="menu">Admin area</div>
<table align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
Username:
</td>
<td width="60%">
<input class="edit" name="username" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td width="60%">
<input type="password" class="edit" name="password" />
</td>
</tr>
<tr>
<td>
Your name:
</td>
<td width="60%">
<input class="edit" name="nick" />
</td>
</tr>
<tr>
<td>
News title:
</td>
<td width="60%">
<input class="edit" name="title" />
</td>
</tr>
<tr>
<td>
The news:
</td>
<td width="60%">
<textarea name="news" class="edit" rows="10" cols="30"></textarea>
</td>
</tr>
<tr>
<td>

</td>
<td width="60%">
<input class="button" type="submit" name="submit" value="Post news" />
<input class="button" type="submit" name="delete" value="Delete all news" />
</td>
</tr>
</table>
<?
$button = $_POST['submit'];
$delete = $_POST['delete'];
if ($button) {
$username = $_POST['username'];
$password = $_POST['password'];
$nick = htmlspecialchars($_POST['nick']);
$title = htmlspecialchars($_POST['title']);
$news = htmlspecialchars($_POST['news']);
$date = date("D M j G:i:s");
if ($username != $adminname) { echo '<div align="center"><b>Wrong Username.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
if ($password != $adminpass) { echo '<div align="center"><b>Wrong Password.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
if (!$nick) { echo '<div align="center"><b>Please enter your nick.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
if (!$title) { echo '<div align="center"><b>Please enter the news title.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
if (!$news) { echo '<div align="center"><b>Please enter the main news.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
mysql_query("INSERT INTO news (nick, title, date, text) VALUES('$nick', '$title', '$date', '$news') ") or die(mysql_error());
echo '<div align="center"><b>News post to database.</b></div>';
}
if ($delete) {
$username = $_POST['username'];
$password = $_POST['password'];
if ($username != $adminname) { echo '<div align="center"><b>Wrong Username.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
if ($password != $adminpass) { echo '<div align="center"><b>Wrong Password.</b></div>'; echo '<div align="center" class="menu"> </div>'; exit; }
mysql_query("TRUNCATE TABLE `news`") or die(mysql_error());
echo '<div align="center"><b>All news from database was deleted.</b></div>';
}
?>
<div align="center" class="menu"> </div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>

</html>

Следва и install.php:

<?
include("config.php");
include("connect.php");
mysql_query("
CREATE TABLE `news` (
`id` int(50) NOT NULL auto_increment,
`nick` varchar(30) NOT NULL default '',
`title` varchar(100) NOT NULL default '',
`date` varchar(100) NOT NULL default '',
`text` TEXT NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM ;
") or die(mysql_error());
echo("Databases create successfully! Please now delete your install.php");
mysql_query("INSERT INTO news (nick, title, date, text) VALUES('Test', 'Title', 'Date', 'The news') ") or die(mysql_error());
?>

Накрая следва style.css и в него слагате

body {
background-color: #343434;
scrollbar-arrow-color: #FF3333;
scrollbar-track-color: #FFF;
scrollbar-face-color: #ADAEAF;
scrollbar-darkshadow-color: #FFF;
scrollbar-shadow-color: #FFF;
}
.own {
border: 1px solid #FFF;
padding: 3px;
color: #FFF;
font: 14px tahoma, verdana, arial, sans-serif;
}
.menu {
margin-bottom: 5px;
background-color: #FF3333;
}
a {
font: 12px tahoma, verdana, arial, sans-serif;
color: #33FF33;
text-decoration: none;
font-weight: normal;
}
a:hover {
font: 12px tahoma, verdana, arial, sans-serif;
color: #FFF;
text-decoration: underline;
}
.edit {
border: 1px solid #FFF;
color: #FFF;
background-color: #343434;
margin-bottom: 5px;
}
.button {
border: 1px solid #FFF;
color: #FFF;
background-color: #343434;
}

Това е! Извинете ме за грешките нов съм.

Leave a Reply