<?php
if ($user_data["rola"] == "admin") { // user name is hear
    $qeralluser = "SELECT * FROM `users`";
    $alluser = mysqli_query($con, $qeralluser);

    if ($_GET) { // get url check? user select
        $username = $_GET["name"];
        if ($username == "forum") { // all user post
            $sql = "SELECT * FROM `posty` ORDER BY id DESC ";
        } else { // check exist user
            while ($userexist = mysqli_fetch_assoc($alluser)) {
                if ($userexist["name"] == $_GET["name"]) {
                    $username = "'" . $_GET["name"] . "'";
                    $sql = "SELECT * FROM `posty` WHERE `user_id`=(SELECT `user_id` FROM `users` WHERE `name`=$username) ORDER BY id DESC ";
                    $username = $_GET["name"];
                    $noexist = 0;
                    break;
                } else {
                    $noexist = 1;
                }
            }
            if ($noexist == 1) {
                header("Location: postviewsman.php");
            }
        }
    } else { // not get url select post user
        $id_us = $user_data["user_id"];
        $sql = "SELECT * FROM `posty` WHERE user_id = $id_us ORDER BY id DESC";
        $username = $user_data["name"];
    }
} else { // not admin select post login user
    $id_us = $user_data["user_id"];
    $sql = "SELECT * FROM `posty` WHERE user_id = $id_us ORDER BY id DESC";
}


// how much post

$much = mysqli_query($con, $sql);
$i = 0;

if (mysqli_num_rows($much) == 0) {
    $data = "brak postów";
    $no_post = 1;
} else {
    $data = mysqli_num_rows($much);
    $no_post = 0;
}


// list user for admin

if ($user_data["rola"] == "admin") {
    $options = "";
    if (!$_GET) {
        $_GET["name"] = "";
    }
    if ($username == $user_data["name"]) { // first option user
        $options .=  "<option value=" . '"' . $username . '"' . ">CIEBIE</option>";
    } else {
        $options .= "<option value=" . '"' . $username . '">' . $username . "</option>";
    }

    // select all user
    $sql = "SELECT `name` FROM `users`";
    $resolut = mysqli_query($con, $sql);
    if ($_GET['name']) {
    }
    while ($row = mysqli_fetch_assoc($resolut)) {  //option usser
        $rowname = $row['name'];
        // if name select is selct or name is name user and name selct us name user or name is user name and not get 
        if ($rowname == $_GET["name"] || ($rowname == $user_data["name"] && $_GET["name"] == $user_data["name"]) || ($rowname == $user_data["name"] && !$_GET["name"])) {
            continue;
        } else if ($rowname == $user_data["name"]) {
            $options .= "<option value=" . '"' . $rowname . '"' . ">CIEBIE</option>";
        } else {
            $options .= "<option" . " value=" . '"' . $rowname . '"' . ">$rowname</option>";
        }
    }
    if ("forum" != $_GET["name"]) { // if not all post option all
        $options .= "<option" . " value=" . '"' . "forum" . '"' . ">forum</option>";
    }
}


// select post

if ($no_post != 1) {
    $post_data = array();
    while ($row = mysqli_fetch_assoc($much)) {
        $post['id'] = $row["id"];
        $post['title'] = $row["tytul"];
        $post['content_max'] = $row["tresc"];
        $post['img'] = $row["img"];
        if (strlen($row["tytul"]) >= 29) {
            $post['content'] = substr($row["tresc"], 0, 99 - strlen($row["tytul"]));
        } else {
            $post['content'] = substr($row["tresc"], 0, 99);
        }

        $post_data[] = $post;
    }
}
mysqli_close($con);
