paging the records of a foreach in PHP parameterized by a select

0

I am making a query to a table where I take a parameter from a SELECT and send the result to an array to show it on the screen. Given the number of records, I want to page it. When I paginate if there is 100 record that I divide to 20 per page, it shows the first 20 but then shows 75 results in general without the criteria sent from the SELECT being met. Please, if someone can help me, I would appreciate it.

This is my code:

   <?php
         if (isset($_GET["id_gal"])){

            $reg_x_pag = 20;

            $sql_page="SELECT * FROM PRODUCTOS WHERE id_gal = $id_ga";

            $resultado_page=$base->prepare($sql_page);

            $resultado_page->execute(array());                        

            $nro_reg=$resultado_page->rowCount();

            $nro_pag = ceil($nro_reg/$reg_x_pag);

            if (!isset($_GET['page'])) {
                $page = 1;
            } else {
                $page = $_GET['page'];
            }
            $prime_pag = ($page-1)*$reg_x_pag;

            $registro1=$base->query('SELECT * FROM PRODUCTOS WHERE id_gal = ' . $id_ga . ' ORDER BY fecha DESC LIMIT ' . $prime_pag . ',' .  $reg_x_pag)->fetchAll(PDO::FETCH_OBJ);
          ?>            
        <div class="fr-row-padding">      
            <?php foreach($registro1 as $art_1): ?> 
            <div class="fr-col l3 m6 fr-margin-bottom">
              <div class="fr-display-container">
                <div class="fr-center fr-color fr-height5 fr-padding"><?php echo $art_1->nombre?></div>
                <a href="#" class="circ  fa fa-search-plus dos"></a>
                <img class="zoom" src="/images/articulos/<?php echo $art_1->img ?>" alt="Capricho&acute;s" style="width:100%;">
                <div class="fr-color2 fr-padding">
                    <p>
                        <span class="fr-xlarge fr-padding-right">&euro;<?php echo $art_1->precio?></span>
                        <?php 
                        if ($precioA!=0){?>
                        <span class="fr-xlarge tachartexto">&euro;<?php echo $art_1->precioA?></span>
                        <?php }?>
                        <a href="V1.8/artV1.8sel.php?Id=<?php echo $art_1->id_pro?>" class="fr-right fr-xlarge">Ver...</a>
                    </p>   
                </div>                          
              </div>

            </div>
            <?php endforeach; ?>
            <div class="fr-col s12 fr-margin-bottom fr-center paginacion">
            <?php     
            for ($page=1;$page<=$nro_pag;$page++) {
                        echo '<a href="catV1.8pro.php?page='. $page .'">' . $page . '</a> ';
                    }
            ?>
            </div>                        
        </div>
            <? 
            }else{
            ?>
            <div class="fr-row-padding" >

            <?php 
            $registro=$base->query("SELECT * FROM PRODUCTOS WHERE ver_pag = 'on' AND precio <> '0' ORDER BY RAND() LIMIT 0,20")->fetchAll(PDO::FETCH_OBJ);                        
            foreach($registro as $art): ?>
            <?php $titulo=$art->nombre;
                  $precio=$art->precio;
                  $precioA=$art->precioA;
                  $img=$art->img;
                  $diapo_a=$art->diapo1;
                  $diapo_b=$art->diapo2;
                  $descripcion=$art->descripcion;
                  $id_pro=$art->id_pro;
            ?>
            <div class="fr-col l3 m6 fr-margin-bottom">
              <div class="fr-display-container">
                <div class="fr-center fr-color fr-height5 fr-padding"><?=$titulo?></div>
                <a href="#" class="circ  fa fa-search-plus dos"></a>
                <img class="zoom" src="/images/articulos/<?=$img?>" alt="Capricho&acute;s" style="width:100%;">
                <div class="fr-color2 fr-padding">
                    <p>
                        <span class="fr-xlarge fr-padding-right">&euro;<?=$precio?></span>
                        <?php 
                        if ($precioA!=0){?>
                        <span class="fr-xlarge tachartexto">&euro;<?=$precioA?></span>
                        <?php }?>
                        <a href="V1.8/artV1.8sel.php?Id=<?php echo $id_pro?>" class="fr-right fr-xlarge">Ver...</a>
                    </p>   
                </div>
              </div>
            </div>
            <?php endforeach; ?>
        </div>
            <?
            }
            ?>

    </div>  
    
asked by Faratir 27.05.2018 в 14:48
source

0 answers