Система за новини с mySQL
Автор: StormBreaker
Какво правят хората като си нямат работа……… Laughing Veryhappy Надявам се да няма проблеми с това. Ако има моля да не ме банвате веднага, ами да пишете едно ЛС и аз ще го махна! Winking
config.php
<?php
$user="root";
$password="*******";
$database="**********";
$rowperpage = 10;
?>bbcode.php
<?php
function bbcode($text){
$text=preg_replace("/\[b\](.+?)\[\/b\]/s", '<b>$1</b>',$text);
$text=preg_replace("/\[i\](.+?)\[\/i\]/s", '<i>$1</i>',$text);
$text=preg_replace("/\[u\](.+?)\[\/u\]/s", '<u>$1</u>',$text);
$text=preg_replace("/\[quote\](.+?)\[\/quote\]/s", '<table cellspacing="1" width="100%" border="0"><tr><td>Quote:</td></tr><tr><td width="100%" style="border:solid 1px black">$1</td></tr></table>',$text);
$text=preg_replace("/\[quote\=(.+?)](.+?)\[\/quote\]/s", '<table cellspacing="1" width="100%" border="0"><tr><td>$1 каза:</td></tr><tr><td width="100%" style="border:solid 1px black">$2</td></tr></table>',$text);
$text=preg_replace("/\[url\](.+?)\[\/url\]/s", '<a href="$1">$1</a>',$text);
$text=preg_replace("/\[url\=(.+?)\](.+?)\[\/url\]/s", '<a href="$1">$2</a>',$text);
$text=preg_replace("/\[img\](.+?)\[\/img\]/s", '<img src="$1" alt="User submitted image" title="User submitted image"/>',$text);
$text=preg_replace("/\[color\=(.+?)\](.+?)\[\/color\]/s", '<span style="color:$1">$2</span>',$text);
$text=preg_replace("/\[size\=(.+?)\](.+?)\[\/size\]/s", '<span style="font-size:$1">$2</span>',$text);
$text=preg_replace("/\[code\](.+?)\[\/code\]/s",'<table cellspacing="1" width="100%" border="0"><tr><td>Code:</td></tr><tr><td width="100%" style="border:solid 1px black">$1</td></tr></table>',$text);
$text = nl2br($text);
return $text;
}
?>createdb.php
<?php
include 'config.php';
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE news (id int(6) NOT NULL auto_increment,title varchar(20) NOT NULL,subtitle varchar(140) NOT NULL,text varchar(1000) NOT NULL,date varchar(20) NOT NULL,category varchar(20) NOT NULL,writer varchar(20) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id)) ENGINE=MyISAM DEFAULT CHARSET=cp1251;";
mysql_query($query);
mysql_close();
?>news.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1251" />
<title>News listing!</title>
<?
include 'config.php';
include 'bbcode.php';
mysql_connect(localhost,$user,$password);
$chars = "SET CHARACTER SET cp1251"; mysql_query($chars);
@mysql_select_db($database) or die( "Unable to select database");
$newspage = $_GET['newspage'];
$id = $_GET['id'];
$delid = $_GET['delid'];
if (empty($newspage))
$newspage = 0;
if (!(empty($delid)) || (is_int($delid))){
$query="delete from news where id='$delid'";
$result=mysql_query($query);
}
if (!(empty($id)) || (is_int($id))){
$query = "SELECT * FROM news where id='$id'";
$result = mysql_query($query);
$title = mysql_result($result, 0, "title");
$text = mysql_result($result, 0, "text");
$date = mysql_result($result, 0, "date");
$category = mysql_result($result, 0, "category");
$title = bbcode($title);
$title = stripslashes($title);
$text = bbcode($text);
$text = stripslashes($text);
?>
<center>
<table width="600" border="0">
<tr>
<td style="font-size:13px; font-style:oblique;" width="20%" align="left"><? echo $category; ?></td>
<td style="font-size:19px; font-family:Georgia, 'Times New Roman', Times, serif" width="50%" align="center"><b><? echo $title; ?></b></td>
<td style="font-size:13px; font-style:oblique;" width="20%" align="right"><? echo $date; ?></td>
</tr>
<tr>
<td align="left" style="font-size:14px;" colspan="3"><br /><? echo $text; ?></td>
</tr>
<tr valign="bottom"><td><a href = "javascript:history.back()"> << Назад </a></td></tr>
</table>
</center>
<?
}
else {
$query = "SELECT * FROM news";
$result = mysql_query($query);
$num = mysql_num_rows($result);
?>
<center>
<table width="504" border="0">
<tr><td><h2><center>News</center></h2></td></tr>
<?php
$plus = $newspage + 1;
$minus = $newspage - 1;
$min = $newspage * $rowperpage;
$max = ($newspage * $rowperpage) + $rowperpage;
$maxpage = $num / $rowperpage;
for ($i = $num - 1 - $min; $i>=0 && $i >= $num - $max; $i--){
$id = mysql_result($result, $i, "id");
$title = mysql_result($result, $i, "title");
$subtitle = mysql_result($result, $i, "subtitle");
$date = mysql_result($result, $i, "date");
$category = mysql_result($result, $i, "category");
$title = bbcode($title);
$title = stripslashes($title);
$subtitle = bbcode($subtitle);
$subtitle = stripslashes($subtitle);
echo '<tr>
<td>
<table width="500" border="0">
<tr>
<td style="font-size:13px; font-style:oblique;" width="20%" align="left">'.$category.'</td>
<td style="font-family:Georgia, \'Times New Roman\', Times, serif" width="50%" align="center"><b>'.$title.'</b></td>
<td style="font-size:13px; font-style:oblique;" width="20%" align="right">'.$date.'</td>
</tr>
<tr>
<td align="left" style="font-size:14px; max-height:50" colspan="3">'.$subtitle.'<br />
<br /></td></tr>
<tr>
<td colspan="2">
<span class="style2">
<b>
<a href="?page=news&id='.$id.'">>> Прочети още</a>
</b>
</span>
</td>
<td align="right">
<span class="style2">
<b>
<a href="?page=news&newspage='.$newspage.'&delid='.$id.'">>> Изтрий</a>
</b>
</span>
</tr><hr /></table>
</td></tr>
';
unset($id);
}
?>
</table>
<?
// navigation buttons
if (!($newspage <= 0)){
echo "<a href=\"?page=news&newspage=".$minus."\"><< предишна | </a>";
}
$b = 1;
for ($a = 0; $a < $maxpage; $a++){
if ($a == $newspage)
echo "<a href=\"?page=news&newspage=".$a."\"><b> ".$b." </b></a>";
else
echo "<a href=\"?page=news&newspage=".$a."\"> ".$b." </a>";
$b++;
}
if (($newspage + 1) < $maxpage)
echo "<a href=\"?page=news&newspage=".$plus."\"> | следваща >></a>";
}
?>
</center>addnew.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1251" />
<title>Add News!</title>
<style type="text/css">
<!--
.style1 {font-size: 9px}
-->
</style>
<SCRIPT LANGUAGE="JavaScript">
// Параметрите на функцията са: field - поредицата поле, count - оставаши полета и max - максималния брой от символи
function CountLeft(field, count, max) {
// ако желаете по голяма дължина в полето max го променете.
if (field.value.length > max)
field.value = field.value.substring(0, max);
else
// Пресмята оставащите символи
count.value = max - field.value.length;
}
function foo(textarea,limit){
var val=textarea.value.replace(/\r/g,'').split('\n');
if(val.length>limit){
alert('Не можете да въведете повече от '+limit+' реда!');
textarea.value=val.slice(0,-1).join('\n')
}
}
</script>
<center>
<? if (!($_POST['submit'])){ ?>
<form action="" class="form" method="post"><b>Заглавие: </b><br />
<select name="category" size="1">
<option value="global">Global</option>
<option value="stargatesg1">Stargate SG-1</option>
<option value="atlantis">Stargate Atlantis</option>
<option value="heroes">Heroes</option>
</select>
<input type="text" maxlength="20" name="title"><br /><br />
<b>Относно (максимум 3 реда):</b><br />
<input readonly type="text" name="left" size=5 maxlength=3 value="140">
<br />
<textarea name="subtitle" rows="3" style="border:dashed; border-width:thin; width:450;" onKeyDown="CountLeft(this.form.subtitle,this.form.left,140);" onKeyUp="CountLeft(this.form.subtitle,this.form.left,140);foo(this,3)"></textarea><br />
<br />
<b>Текст: Може да се използва BBCode</b><br />
<textarea name="text" rows="20" cols="15" style="border:dashed; border-width:thin; font-size: 18px; width: 1000;"></textarea>
<br />
<input type="submit" name="submit">
</form>
<hr>
<span class="style1">Created by Stormbreaker for TheLostCity site!</span>
<?php
}
if ($_POST['submit']){
$text = $_POST['text'];
$title = $_POST['title'];
$subtitle = $_POST['subtitle'];
$category = $_POST['category'];
if ((empty($text)) or (empty($title)) or (empty($subtitle))){
echo "<h2>Моля, попълнете всички полета!</h2>";
exit;
}
$text = htmlspecialchars($text);
$text = addslashes($text);
$title = htmlspecialchars($title);
$title = addslashes($title);
$subtitle = htmlspecialchars($subtitle);
$subtitle = addslashes($subtitle);
$date = date("G:i d.m.Y");
$writer = "admin";
include 'config.php';
mysql_connect(localhost,$user,$password);
$chars="SET CHARACTER SET cp1251";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($chars);
$query = "INSERT INTO news VALUES ('','$title','$subtitle','$text','$date','$category','$writer')";
mysql_query($query);
mysql_close();
echo "<h2>Новината е добавена успешно!</h2>";
}
?>
</center>

