<?php
include("headers.php")
?>

<head>
<title>Administration av Degerfors Amatörfiskeklubbs hemsida</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">

div.post {
border: 1px solid black;
}

div.post .summary {
font-style: italic;
}

</style>
</head>
<body>

<?php
$conn=mysql_connect("localhost", "Konto", "lösenord");

//den ska vara en variabel konstigt nog
$rs=mysql_select_db("degerfors_afk_n", $conn)
																			 or die ("<p>Problem med ansluningen till databasen</p>");


// efter submit
if ($edit_news && $action=="add") {

  $sql="insert into news (date, name, header, summary, content) values (\"$date\", \"$name\", \"$header\", \"$summary\", \"$content\")";
  
  $rs=mysql_query($sql, $conn);

}

//om man har valt modify
if($edit_news && $action=="modify") {

	$sql ="update news set date=\"$date\" where id=\"$newsid\"\n";

	$sql .="update news set name=\"$name\" where id=\"$newsid\"\n";

	$sql .="update news set header=\"$header\" where id=\"$newsid\"\n";

	$sql .="update news set summary=\"$summary\" where id=\"$newsid\"\n";

	$sql .="update news set content=\"$content\" where id=\"$newsid\"\n";
	$rs=mysql_query($sql, $conn);
}

if(!$edit_news && $action=="modify") {
	$sql="select * from news where id=$newsid";
	$rs=mysql_query($sql, $conn);
	while($row=mysql_fetch_array($rs)) {
		$tmp_date=$row["date"];
		$tmp_name=$row["name"];
		$tmp_header=$row["header"];
		$tmp_summary=$row["summary"];
		$tmp_content=$row["content"];
	}
}

//om man har valt att ta bort en nyhet
if ($action=="delete") {
  $sql="delete from news where id=$newsid";
  
  $rs=mysql_query($sql, $conn);
}

//vilka vill man visa
if($posts_to_show) {
$limit=$posts_to_show;
}
else {
$limit="0,9";
}

//visa de senaste tio (eller vad man kan tänkas välja med min specialfunktion)
$sql="select * from news order by date desc limit $limit";
$rs=mysql_query($sql, $conn);
	while($row=mysql_fetch_array($rs)) {
		echo("<div class=\"post\"><h3>".$row["date"]."</h3>");
		echo("<h2>".$row["header"]."</h2>");
		echo("<p class=\"summary\">".$row["summary"]."</p>");
		echo("<p>".$row["content"]."</p>");
		echo("
		<a href=\"$PHP_SELF?newsid=$tmp_id&amp;action=delete\">Ta bort</a>
		<a href=\"$PHP_SELF?newsid=$tmp_id&amp;action=modify\">Redigera</a></div>
		");
	}


//formulär för att lägga till nya

echo("
<form action=\"\" method=\"post\">
<p>
<input type=\"text\" name=\"date\" id=\"date\" value=\"
");
if ($tmp_date) {
  echo($tmp_date);
}
else {
  echo date("Y-m-d H:i");
}

echo("
\" />
<label for=\"date\">Tid (enligt formatet: 2004-08-30 20:05</label>
</p>

<p>
<input type=\"text\" name=\"name\" id=\"name\" value=\"$tmp_name\" />
<label for=\"name\">Namn (ett kort namn för nyheten)</label>
</p>

<p>
<input type=\"text\" name=\"header\" id=\"header\" value=\"$tmp_header\" />
<label for=\"header\">Rubrik (rubriken som visas tillsammans med nyheten)</label>
</p>

<p>
<input type=\"text\" name=\"summary\" id=\"summary\" value=\"$tmp_summary\" />
<label for=\"summary\">Sammanfattning av nyheten.</label>
</p>

<p>
<label for=\"content\">Text</label>
<textarea name=\"content\" id=\"content\" cols=\"70\" rows=\"10\">$tmp_text</textarea>
</p>

<div>
<input type=\"hidden\" name=\"action\" value=\"add\" />
<input type=\"hidden\" name=\"newsid\" value=\"$newsid\" />
<input type=\"hidden\" name=\"action\" value=\"$action\" />
</div>

<p>
<button type=\"submit\" name=\"edit_news\" value=\"skicka\">Skicka</button>
</p>
</form>
");

?>

<p>Visa senaste:<br />
<a href="<?php echo($PHP_SELF)?>?posts_to_show=0,9">1-10</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=10,19">11-20</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=20,29">21-30</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=30,39">31-40</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=40,49">41-50</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=50,59">51-60</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=60,69">61-70</a>
<a href="<?php echo($PHP_SELF)?>?posts_to_show=70,79">71-80</a>
</p>
</body>
</html>
