<h1>Новости</h1>
<a href="/admin/?p=news&amp;new">Добавить</a><br /><br />
<?php
if(isset($_GET["new"])){
	if(isset($_POST["new"])){
		db_connect();
		db_query("INSERT INTO `".DBpref."news` (`title`, `text`, `dat`) VALUES ('".db_string($_POST["title"])."', '".db_string($_POST["text"])."', '".date("Y-m-d H:i:s")."')");
		db_close();
		die("<h2>Готово</h2>");
	}
echo '<h2>Добавить</h2>';
echo '
<form action="" method="post">
<table width="100%" border="1" cellspacing="0" cellpadding="0" class="table1">
  <tr>
    <td width="180px" align="right">Заголовок</td>
    <td><input name="title" type="text" size="50" maxlength="255" /></td>
  </tr>
  <tr>
    <td width="180px" align="right">Текст</td>
    <td><textarea name="text" cols="50" rows="6"></textarea></td>
  </tr>
  <tr>
    <td width="180px" align="right"></td>
    <td><input name="new" type="submit" value="Добавить" /></td>
  </tr>
</table>
</form>
';
exit();
}

if(isset($_GET["id"])){
	db_connect();
		
		if(isset($_POST["del"])){
			db_query("DELETE FROM `".DBpref."news` WHERE `id` = ".intval($_GET["id"]));
			die("<h2>Готово</h2>");
		}
	
		if(isset($_POST["edit"])){
			db_query("UPDATE `".DBpref."news` SET `title` = '".db_string($_POST["title"])."', `text` = '".db_string($_POST["text"])."' WHERE `id` = ".intval($_GET["id"]));
			die("<h2>Готово/h2>");
		}
		
	$res = db_query("SELECT * FROM `".DBpref."news` WHERE `id` = ".intval($_GET["id"]));
	db_close();
$row = db_assoc($res);
echo '
<form action="" method="post">
<table width="100%" border="1" cellspacing="0" cellpadding="0" class="table1">
  <tr>
    <td width="180px" align="right">Заголовок</td>
    <td><input name="title" type="text" value="'.$row["title"].'" size="50" maxlength="255" /></td>
  </tr>
  <tr>
    <td width="180px" align="right">Текст</td>
    <td><textarea name="text" cols="50" rows="6">'.$row["text"].'</textarea></td>
  </tr>
  <tr>
    <td width="180px" align="right"></td>
    <td><input name="edit" type="submit" value="Готово" /> <input name="del" type="submit" value="Удалить" /></td>
  </tr>
</table>
</form>
';
exit();
}
db_connect();
$res = db_query("SELECT * FROM `".DBpref."news` ORDER BY `id` DESC");
db_close();

echo '
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Текст</td>
    <td>Дата</td>
	<td></td>
  </tr>
';

while($row = db_assoc($res)){
echo '
  <tr>
    <td>'.$row["title"].'</td>
    <td>'.$row["dat"].'</td>
	<td><a href="?p=news&id='.$row["id"].'">Редактировать</a></td>
  </tr>
';
}

echo '
</table>
';
?>