<?php include("_ayarlar.php"); ?>
<?php include("_fonksiyonlar.php"); ?>
<?php $title = "Tüm Haberler"; ?>
<?php include("_header.php"); ?>

<!-- Start posts-entry -->
<section class="section">
    <div class="container">
        <div class="row">
            <?php
            $sayfa = @$_GET["sayfa"];
            if ($sayfa == null) $sayfa = 1;
            $basla = ($sayfa - 1) * 9;
            $sql = "SELECT * FROM haber INNER JOIN kategori ON haber.kategori = kategori.kategori_id ORDER BY id DESC LIMIT $basla,9";
            $tablo = $vt->query($sql);
            while ($satir = $tablo->fetch_assoc()) { # Haber tablosunda kayıt olduğu sürece
            ?>
                <div class="col-lg-4 mb-4">
                    <div class="post-entry-alt">
                        <a href="haber.php?id=<?php echo $satir['id'] ?>&baslik=<?php echo baslik_donustur($satir['baslik']) ?>" class="img-link"><img src="kapak/<?php echo $satir['kapak'] ?>" alt="Image" class="img-fluid rounded" style="background-size: cover; height: 300px; object-fit: cover;"></a>
                        <div class="excerpt">
                            <h2><a href="haber.php?id=<?php echo $satir['id'] ?>&baslik=<?php echo baslik_donustur($satir['baslik']) ?>"><?php echo $satir['baslik'] ?></a></h2>
                            <div class="post-meta align-items-center text-left clearfix">
                            <span class="d-inline-block mt-1"><?php echo "<a href='kategori.php?id=$satir[kategori]&baslik=".baslik_donustur($satir['kategori_ad'])."'>".$satir['kategori_ad']."</a>" ?></span>
                                <span>&nbsp;-&nbsp; <?php echo tarih_donustur($satir['eklenme_tarihi']) ?></span>
                            </div>
                            <?php echo implode(' ', array_slice(explode(' ', $satir['icerik']), 0, 20)) ?>...
                            <p><a href="haber.php?id=<?php echo $satir['id'] ?>&baslik=<?php echo baslik_donustur($satir['baslik']) ?>" class="read-more">Okumaya Devam Et</a></p>
                        </div>
                    </div>
                </div>
            <?php
            }
            ?>
        </div>

    </div>
</section>
<nav aria-label="...">
    <ul class="pagination pagination-sm">
        <?php
            $sql = "SELECT * FROM haber";
            $tablo = $vt->query($sql);
            $haber_sayisi = $tablo->num_rows; # Toplam haber sayısı
            $buton_sayisi = ceil($haber_sayisi / 9);

            for ($i=1; $i<=$buton_sayisi ; $i++) { 
                echo "<li class='page-item'><a class='page-link' href='haberler.php?sayfa=$i'>$i</a></li>";
            }
        ?>
        

    </ul>
</nav>
<?php include("_footer.php"); ?>