dpmfur2635 2018-09-09 22:15
浏览 80

服务器端网站上的分页无效

It's my index.php:

<?php  

$maxlinks = 4;
$paginaAtual = (isset($_GET['paginaAtual'])) ? (int)$_GET['paginaAtual'] : 1;
$maximo = 5;
$inicio = (($maximo * $paginaAtual) - $maximo);

$publicacoesUN = DBRead('publicacao', "ORDER BY id DESC LIMIT $inicio, $maximo");

$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

if ($post != '' || ($post == '' && $pagina != '')) {

    switch ($pagina):
    case 'home':
        $titulo = '';
        $shareTitulo = '';

        $descricao = '';
        $shareDescricao = '';

        $shareImg = '';
        $keywords = '';

        $ogUrl = '';
        $urlCanonico = '';
        break;

    case 'ultimasnoticias':
        $titulo = '';
        $shareTitulo = '';

        $descricao = '';
        $shareDescricao = '';

        $shareImg = '';
        $keywords = '';

        $ogUrl = '';
        $urlCanonico = '';
        break;

    default:
        $titulo = 'Home';
        $pagina = 'home';
    endswitch;

}

?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>

<nav>

    <ul>
        <li>
          <a href="?p=home">Página Inicial</a>
        </li>

        <li>
          <a href="?p=ultimasnoticias">Últimas Notícias</a>
        </li>
    </ul>

</nav>

    <?php
        if (empty($post)) {
        require_once 'page_' . $pagina . '.php';
        } else {
            require_once 'posts/' . $post . '.php';
        }
    ?>

</body>
</html>

And my ultimasnoticias.php:

<div class="container my-3">

    <div class="row">

        <?php foreach ($publicacoesUN as $UN): ?>

           <div class="col-12 col-md-6 col-lg-3 mb-3 mb-md-3">

                <div class="card">

                    <div class="img-container">

                      <a href="index.php?post=<?php echo $UN['title']?>"><img src="<?php echo $UN['capa']?>" alt="<?php echo $UN['alt']?>" class="card-img-top" id="imgUNcover"></a>

                    </div>

                    <div class="card-body">

                      <a href="index.php?post=<?php echo $UN['title']?>" class="card-title cardTitleLink"><h1 class="cardTitleUN"><?php echo $UN['title']?></h1></a>

                      <p class="card-text text-muted"><?php echo $UN['text']?></p>

                      <a href="index.php?post=<?php echo $UN['title']?>" class="btn btn-outline-danger btn-sm">Continue Lendo</a>

                    </div>

                </div>

           </div>

       <?php endforeach; ?>

    </div>

</div>

<?php

  $pdo = new PDO('mysql:host=localhost;dbname=publicacoes', 'root', '');

  $seleciona_2 = $pdo->prepare("SELECT * FROM `bn_publicacao`");
  $seleciona_2->execute();
  $total = $seleciona_2->rowCount();
  $total_paginas = ceil($total/$maximo);

  if($total > $maximo){

  echo '<a href="?paginaAtual=1">First page</a>';
  for ($i = $paginaAtual - $maxlinks; $i <= $paginaAtual -1; $i++) { 

    if ($i >= 1) {
      echo '<a href="?paginaAtual='.$i.'">'.$i.'</a>';
    }
  }
  echo '<span>'.$paginaAtual.'</span>';
  for ($i= $paginaAtual +1; $i <= $paginaAtual + $maxlinks; $i++) { 
    if ($i <= $total_paginas) {
      echo '<a href="?paginaAtual='.$i.'">'.$i.'</a>';
    }
  }
  echo '<a href="?paginaAtual='.$total_paginas.'">Last page</a>';

  }

?>

When i click on the link of the pagination like 1, 2, first page, last page...etc, it redirects to the home page. If i put the whole script from the file ultimasnoticias.php into the file index.php the pagination works.

This pagination script works for a static website, but it's not working on a server-side. How can i solve this?

  • 写回答

1条回答 默认 最新

  • duanqinqian5299 2018-09-09 22:28
    关注

    I recommend you use this class! It's excellent. It does it all for you!

    https://github.com/daveismyname/pagination

    An example of its use (MVC format):

    $pages = new Paginator('10','p');
    $pages->set_total( $this->support->countAllFaq() );
    $getAllFaq = $this->support->getAllFaq($pages->get_limit());
    if($getAllFaq == false) { $count = 0; } else { $count = count($getAllFaq); }
    $pageLinks = $pages->page_links();
    

    The database queries:

    public function countAllFaq()
    {
        $sql = 'SELECT f.name, f.content, f.date, f.status, f.cat_id, c.id, c.name AS catname FROM chewi_support_faq f LEFT JOIN chewi_support_categories c ON f.cat_id = c.id WHERE f.status = 1';   
        $results = $this->db->selectExtended($sql);
        $totalRows = count($results);
        if($results === FALSE){ $totalRows = '0'; }
        return $totalRows;
    }
    
    public function getAllFaq($limit)
    {
        $sql = 'SELECT f.id, f.name, f.content, f.date, f.status, f.cat_id, c.id AS cat_id, c.name AS catname FROM chewi_support_faq f LEFT JOIN chewi_support_categories c ON f.cat_id = c.id WHERE f.status = 1 ORDER BY f.cat_id DESC '.$limit;   
        $results = $this->db->selectExtended($sql);
        return $results;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛