  <?php
    $sql = "SELECT * FROM `posty` ORDER BY id DESC LIMIT 12";
    $header =  "12 Najnowszych postów ";
    if (isset($_GET["ile"])) {
        if ($_GET["ile"] == "all") {
            $sql = "SELECT * FROM `posty` ORDER BY id DESC";
            $header =  "Wszystkie posty";
        } else if ($_GET["ile"] != 12 && $_GET["ile"] >= 12) {
            $ile =  $_GET["ile"];
            $sql = "SELECT * FROM `posty` ORDER BY id DESC LIMIT $ile";
        }
    }
    if ((isset($_GET["zawiera"]) && $_GET["zawiera"] != null)) {
        $like = $_GET["zawiera"];
        $sql = "SELECT * FROM `posty` WHERE `tytul` LIKE '%$like%' OR `tresc` LIKE '%$like%' ORDER BY `id` DESC ";
        $header = "posty które zawierają wyraz $like";
    }

    $resolut = mysqli_query($con, $sql);
    $i = 0;
    $post_list = array();
    while ($row = mysqli_fetch_assoc($resolut)) {
        $post["id"] = $row["id"];
        $post["title"] = $row["tytul"];
        $post["img"] = $row["img"];
        $user_id = $row["user_id"];
        $post["autor"] = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM `users` WHERE `user_id` = $user_id"))["name"];;
        $post["intro_max"] = substr($row["tresc"], 0, 500);
        if (strlen($post["title"]) >= 29) {
            $post["intro_min"] = substr($row["tresc"], 0, 99 - strlen($post["title"])) .  " ...";
        } else {
            $post["intro_min"] = substr($row["tresc"], 0, 99) . " ...";
        }
        $post_list[] = $post;
    }
    $ile = mysqli_num_rows($resolut);
    if (!isset($GET["ile"])) {
        $header =  "$ile Najnowszych postów";
    }
