Szczerze? cieżko cokolwiek rozczytać z twojego kodu. Co w ogóle robi funkcja t1? Nazwij ją normalnie.
Jakiś mój stary przykładowy kod:
/**
* @param int $offset
* @param int $length
*
* @return \Generator
*/
function readImages(int $offset = 0, int $length = 5): \Generator
{
$stmt = connect()->query("SELECT * FROM gallery ORDER BY created_at,id DESC LIMIT {$offset}, {$length}",
PDO::FETCH_ASSOC);
foreach ($stmt as $record) {
yield $record;
}
}
/**
* @return int
*/
function countImages(): int
{
return connect()->query('SELECT count(*) FROM gallery')->fetch(PDO::FETCH_NUM)[0] ?? 0;
}
//pagination:
$page = (int) ($_GET['page'] ?? 0);
$length = 5;
$offset = $length * $page;
$total = countImages();
//parametry do templatki:
$params = [
'images' => readImages($offset, $length),
'pagination' => [
'prev' => $page - 1 < 0 ? false : $page - 1,
'next' => $total < $offset + $length * 2 ? false : $page + 1,
],
];
<!-- pagination w templatce -->
<section class="pagination">
<?php if ($pagination['prev'] !== false): ?>
<a href="?page=<?= $pagination['prev'] ?>">Previous</a>
<?php endif; ?>
<?php if ($pagination['next'] !== false): ?>
<a href="?page=<?= $pagination['next'] ?>">Next</a>
<?php endif; ?>
</section>
<!-- template wyswietlania postow/galerii/produktow -->
<ul>
<?php foreach ($images as $image): ?>
<li>
<article id="<?= $image['id'] ?>">
<img width="300px" src="<?= $image['path'] ?>" alt="image <?= $image['id'] ?>">
</article>
</li>
<?php endforeach; ?>
</ul>
A tu jakiś stary projekt paginacji dla SlimPHP: https://github.com/xandros15/SlimPagination