doudu22272099831 2014-12-18 15:56
浏览 46
已采纳

php分页,使用sql server [关闭]

so, i am newbie in php development, i want to create a pagination with php pdo and sql server, first page works great, but other pages from pagination not work (sorry for my english, i am from brazil)

follows my code

<?php

require 'conn.php';
$pdo = dbConnect();

$limite = 10;

$pg = (isset($_GET['pg'])) ? (int)$_GET['pg'] : 1;

$inicio = ($pg * $limite) - $limite;


$sql = "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY ID_USUARIO) as row FROM tbl_usuario) a WHERE row between ".$inicio." and ".$limite.""; 

try {

        $query = $pdo->prepare($sql);
        $query->execute();

        } catch (PDOexception $error_sql){

                echo 'Erro ao retornar os Dados.'.$error_sql->getMessage();
}

while($linha = $query->fetch(PDO::FETCH_ASSOC)){ ?>

                <?php 
                echo $linha['NOME'].'</br>'; 

                ?> 
<?php }


$sql_Total = 'SELECT ID_USUARIO FROM tbl_usuario';

try {
        $query_Total = $pdo->prepare($sql_Total);
        $query_Total->execute();
        $query_result = $query_Total->fetchAll(PDO::FETCH_ASSOC);
        $query_count =  $query_Total->rowCount(PDO::FETCH_ASSOC);


        $qtdPag = ceil($query_count/$limite);

        } catch (PDOexception $error_Total){

                echo 'Erro ao retornar os Dados. '.$error_Total->getMessage();

        }


        echo "<div class='relax h30'></div>";
        echo '<a href="teste?pg=1">PRIMEIRA PÁGINA</a>&nbsp;';
        echo '<ul id="paginacao">';
    echo '<li><a class="anterior" href="teste?pg=1">Anterior</a></li>';

        if($qtdPag > 1 && $pg <= $qtdPag){

                for($i = 1; $i <= $qtdPag; $i++){

                        if($i == $pg){

                                echo "<li><a class='ativo'>".$i."</a></li>";

                        } else {

                                echo "<li><a href='teste?pg=$i'>".$i."</a></li>";

                        }

                }

        }

        echo "<li><a class='proxima' href='teste?pg=$qtdPag'>Próxima</a></li>";

?>
  • 写回答

1条回答 默认 最新

  • dongsi7759 2014-12-18 16:07
    关注

    You are looking for rows between $inicio and $limite. On page 1 that means we would be getting rows 0 to 10, which is correct. Page 2 however would be rows 10 to 10, which is none.

    You should create a new variable to determine the number of the last row, example:

    $lastRow = $inicio + $limite;
    

    And then just:

    WHERE row between ".$inicio." and ".$lastRow.";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置