Foreach - Paging with images

1

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

    
asked by anonymous 25.03.2016 / 19:42

1 answer

2
  

Note: This response has been posted to a previous issue of the question.

Here is a very simple and didactic example of pagination, to serve as an example:

$imagens = '/1.jpg|/2.jpg|/3.jpg|/4.jpg|/5.jpg|/6.jpg|/7.jpg|/8.jpg';
$aImages = explode( '|',  $imagens );

$porPagina = 3;
$total = count( $aImages );
$paginas = ceil( $total / $porPagina ); 

for( $pagina = 0; $pagina < $paginas; ++$pagina ) {
   echo 'Pagina ' . ( $pagina + 1 ) . '<br>' . PHP_EOL;

   $offset = $pagina * $porPagina;
   for( $item = $offset ; $item < $total && $item < $offset + $porPagina; ++$item ) {
       echo '<img src="'.BASE_URL.$aImages[$item].'" ></a>' . PHP_EOL;
   }
}

See working at IDEONE .

Of course in the case, you will have to adapt to the actual situation of your code. If it is to show only one page at a time, just delete the for( $pagina from outside, and set the variable externally.

    
25.03.2016 / 20:02