<?php  header('Content-type: text/html; charset=utf-8'); ?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<center><table width=400><tr><td><span class="header1">Lägg till nyhet</span><br>
<?php
	extract($_REQUEST);

	include("../scripts/sqlinfo.inc");
	mysql_connect($host, $user, $pass);
	mysql_selectdb("pyrot");
	
	if ($del) {
		mysql_query("delete from news where col_id=$del");
		$del=0;
	}
	
	if (!$submit) {
		if (!$nr) {
	?>
	<form method="post" action="add_news.php">
	<table border=0>
	<tr><td>Rubrik:</td><td><input type="text" name="rubrik"></td></tr>
	<tr><td valign="top">Text:</td><td><textarea name="info" rows=7 cols=30></textarea>
	<input type="hidden" name="datum" value="0">
	<tr><td> </td><td><input type="submit" name="submit" value="Spara"></td></tr>
	</table>
	</form>
	<?php
		} else {
			$result=mysql_query("select * from news where col_id=$nr");
			if ($r=mysql_fetch_array($result)) {
				printf("<form method=\"post\" action=\"add_news.php?nr=%s\">
					<table border=0>
					<tr><td>Rubrik:</td><td><input type=\"text\" name=\"rubrik\" value=\"%s\"></td></tr>
					<tr><td>Datum (ееее-MM-DD hh:mm:ss>:</td><td><input type=\"text\" name=\"datum\" value=\"%s\"></td></tr>
					<tr><td valign=\"top\">Text:</td><td><textarea name=\"info\" rows=7 cols=30>%s</textarea>
					<tr><td> </td><td><input type=\"submit\" name=\"submit\" value=\"Spara\"></td></tr>
					</table>
					</form>"
					,$nr, $r["rubrik"], $r["datum"], $r["info"]);
			}
		}

		$result=mysql_query("SELECT * from news order by datum desc, col_id desc");
		if ($r=mysql_fetch_array($result)) {
			printf("<center><table width=400><tr><td colspan=5><span class=\"header1\">Nyheter</span>");
			do {
				printf("<tr><td><b><a href=\"add_news.php?nr=%s\">[Ändra]</a><a href=\"add_news.php?del=%s\">[Ta bort]</a>
					%s, %s</b></td></tr><tr><td>%s<hr></td></tr>",$r["col_id"], $r["col_id"], $r["datum"], $r["rubrik"], $r["info"]
					);
			} while($r=mysql_fetch_array($result));
		} else printf("Databasen är tom!");
	} else {
		if ($nr) {
			$query = "";
			sprintf($query, "update news set rubrik = '%s', datum = '%s', info = '%s' where col_id = %s",
				$rubrik, $datum, $info, $nr
				);
			$res = mysql_query($query);
		} else {
			$res = mysql_query("insert into news values(null, UTC_TIMESTAMP(), \"$rubrik\", \"$info\")");
		}


		if($res=1)
			printf("Nyhet inlagt...");
		else
			printf("Något gick snett...");
	}

?>
</td></tr></table></center>
</body>
</html>