1
I’m trying to display images instead of numbers in the pagination.
I need help getting inside the for
these images that are coming from the variable $aImages
in the 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 that final result:
These are the tables in DB:
Table IMAGES 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
Table POSTS:
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 DB, the order prints correctly but each image prints repeatedly 15 times did not understand the reason, need each print only once.
I appreciate help
for() from the inside is printing the image of for each from the outside several times. Do you need it? If you need it, you need to rethink your paging. I think it is not the case to use foreach if it is to paginate. And another thing, $maximo has to catch the size of $aImages.
– Bacco
Thanks @Bacco o
for
is responsible for adding the pagination number$i
, I don’t know how to do it without it– Gisele