I'm trying to display images, rather than numbers, in pagination.
I need help putting into for
these images that are coming from the variable $aImages
in foreach
,
$maximo = 1;
$pagina = isset($_GET['pagina']) ? ($_GET['pagina']) : '1';
$pagina2 = $pagina + 1;
$inicio = $pagina - 1;
$inicio = $maximo * $inicio;
//CONSULTA PARA CONTAR TODOS REGISTROS
$strCount = "SELECT COUNT(*) AS 'total_images' FROM posts, images
WHERE posts.id_posts = images.posts_ID
AND slug='".$_GET['slug']."'";
$varstrCount = $crud->verdados($strCount);
$total = 0;
if(count($varstrCount)){
foreach ($varstrCount as $row) {
$total = $row["total_imagens"];
}
}
//CONSULTA PARA LIMITAR OS REGISTROS NA PAGINAÇÃO
$resultado = "SELECT * FROM posts, images
WHERE posts.id_post = images.posts_ID
AND slug='".$_GET['slug']."' ORDER BY id_posts LIMIT $inicio,$maximo";
//EXECUTA A CONSULTA
$varresultado = $crud->verdados($resultado);
$max_links = 10;
$previous = $pagina - 1;
$next = $pagina + 1;
$pgs = ceil($total / $maximo);
//CONSULTA PARA AGRUPAR IMAGENS RELACIONADAS AO ID
$stmt = $DB_con->prepare("SELECT
posts.*,
COUNT(0) total,
GROUP_CONCAT(dir_image SEPARATOR '|') images
FROM
posts
INNER JOIN images ON (posts.id_post = images.post_id )
WHERE
slug=:slug
GROUP BY
id_post
");
//EXECUTA A CONSULTA
$stmt->execute(array(":slug"=>$_GET['slug']));
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$aImages = explode('|', $row['images']);
for($i=$pagina-$max_links; $i <= $pgs; $i++) {
foreach ($aImages as $sImage) {
echo "<a href='".BASE_URL.$slug."-".($i)."'><img src='".BASE_URL.$sImage."' ></a>"; ;
}
}
}
The idea is to have this end result:
These are the tables in the DB:
IMAGES table looks like this:
id_images | dir_image | post_id
1 image1.jpg 1
2 image2.jpg 1
3 image3.jpg 1
4 image4.jpg 1
5 image5.jpg 2
6 image6.jpg 2
7 image7.jpg 2
8 image8.jpg 2
POSTS table:
id_post | slug | title
1 title_post Title Post
2 title_post_2 Title Post 2
As an example the variable $aImages
has 5 images coming from the DB, the order prints correctly but each image prints 15 times repeatedly. I did not understand the reason, I need each one to print only once.
Thank you for your help