<?php
if ($_GET) {
    $number = '"' . $_GET["numer"] . '"';
    include("../../assets/script/other/connection.php");
    $sql = "SELECT * FROM `posty` WHERE id=$number LIMIT 1";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $ids = intval($row["id"]);
        $title = $row["tytul"];
        $content = $row["tresc"];
        $user_id = $row["user_id"];
        $data_create = $row["data_utworzenia"];
        $data_update = $row["data_edycji"];
        $autor = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM `users` WHERE `user_id` = $user_id"))["name"];
        $img = $row["img"];

        $frist = "SELECT * FROM `posty` LIMIT 1";
        $fristresult = mysqli_fetch_assoc(mysqli_query($con, $frist))["id"];

        $last = "SELECT * FROM `posty` ORDER BY `id` DESC LIMIT 1";
        $lastyresult = mysqli_fetch_assoc(mysqli_query($con, $last))["id"];

        //start next
        $idst = $ids;
        do {
            $idst++;
            $next = "SELECT * FROM `posty` WHERE id=$idst";
            $nextresult = mysqli_query($con, $next);
            if ($idst > intval($lastyresult)) {
                $idsnext = intval($fristresult);
                break;
            } else {
                $idsnext = $idst;
            }
        } while (mysqli_num_rows($nextresult) == 0);

        //end next

        //start prev

        $idst = $ids;

        do {
            $idst--;
            $prev = "SELECT * FROM `posty` WHERE id=$idst";
            $prevnyresult = mysqli_query($con, $prev);
            if ($idst <= 0) {
                $idsprv = intval($lastyresult);
                break;
            } else {
                $idsprv = $idst;
            }
        } while (mysqli_num_rows($prevnyresult) == 0);
        //end prev
    } else {
        header("Location: ../other/index.php");
    }
} else {
    header("Location: ../other/index.php");
}
