<?php
include('header.php');

echo "<br />";

include('config.php');

$con = mysqli_connect($host, $user, $pass, $base);
if (mysqli_connect_errno()) die (mysqli_connect_error());

if (!$con->set_charset("utf8")) {
	printf("Error loading character set utf8: %s\n", $mysqli->error);
}

$result = mysqli_query("SELECT count(*) as total from aktualnosci");
$data = mysqli_fetch_assoc($result);
$all = $data['total'];

if (!isset($_GET['page']) || !is_int($_GET['page'])) {
    $page = 0;
} else {
    $page = $_GET['page'];
    if ($page > ($all/5)) {
        $page = $all/5;
    }
}

if (!isset($_GET['id'])) {
    $query = "SELECT *,unix_timestamp(data) FROM aktualnosci ORDER BY id DESC";
    $result = mysqli_query($con, $query);
    while ($result && $row = mysqli_fetch_array($result)) {

        $mysqldate = date("d m Y", $row['unix_timestamp(data)']);

        echo $mysqldate . "<br />";
        echo "<h1>" . $row['tytul'] . "</h1>";

        $string = $row['wiadomosc'];
        $string = (strlen($string) > 512) ? substr($string,0,512).'...' : $string;

        echo $string . "<br />";

        echo "<a href=\"./aktualnosci.php?id=" . $row['id'] . "\">Więcej</a><br /><br />";
    }

} else {
    $id = $_GET['id'];

    $query = "SELECT *,unix_timestamp(data) FROM aktualnosci WHERE id=" . $id;
    $result = mysqli_query($con, $query);
    while ($result && $row = mysqli_fetch_array($result)) {

        $mysqldate = date("d m Y", $row['unix_timestamp(data)']);

        echo $mysqldate . "<br />";
        echo "<h1>" . $row['tytul'] . "</h1>";

        $string = $row['wiadomosc'];

        echo $string . "<br />";

    }


}

include('footer.php');
?>
