PHP分页问题

如何解决PHP分页问题

| 因此出于某种原因,我试图找出为什么我的分页无法正常工作的原因。我将其从第1页移至第2页,但由于某种原因它不会转到第3页。我检查了看数据库中的查询是否正确,因此不确定我要去哪里。
$per_page = \'4\';

$tenure_sql =  \'SELECT COUNT(id) as count
                FROM people.bywu
                WHERE type <> 0
                AND status = \"approved\"\';

$tenure_query = mysql_query( $tenure_sql,DB );

$tenure_count = mysql_fetch_object( $tenure_query );
$tenure_count = $tenure_count -> count;
$tenure_pages = ceil( $tenure_count / $per_page );

<div class=\"pagination\" id=\"tenure_pages\">
<a href=\"\" class=\"lt grayed\">&lt;</a>
Stories <span id=\"tenure_low\" class=\"current_low\"><?= $tenure_count ? \'1\':\'0\' ?></span>-<span id=\"tenure_high\" class=\"current_high\"><?= $tenure_count > 4 ? $per_page : $tenure_count ?></span> of <span class=\"total\"><?= $tenure_count ?></span>
<a href=\"\" class=\"gt<?= $tenure_count < 5 ? \' grayed\':\'\' ?>\">&gt;</a>
<span class=\"pages\" style=\"display:none;\"><?= $tenure_pages ?></span>
<?
    for( $i = 1; $i < $tenure_pages + 1; $i++ )
    {
    echo \'<a href=\"\">\' . $i . \'</a> \';
    } // for
?>
    

解决方法

放弃了kohana分页类,如果您了解有关php类的任何基础知识,我认为您会发现它很有用。 用法:
$pager = Pagination::factory(array(\'current_page\' => $_GET[\'page\'],\'total_items\' => $total_items,\'items_per_page\' => 20));

if ($pager->next_page) { /* etc.......*/ }

<?php
class Pagination {


    protected $config = array(
            \'current_page\'      => 1,\'total_items\'       => 0,\'items_per_page\'    => 10
    );

    // Current page number
    protected $current_page;

    // Total item count
    protected $total_items;

    // How many items to show per page
    protected $items_per_page;

    // Total page count
    protected $total_pages;

    // Item offset for the first item displayed on the current page
    protected $current_first_item;

    // Item offset for the last item displayed on the current page
    protected $current_last_item;

    // Previous page number; FALSE if the current page is the first one
    protected $previous_page;

    // Next page number; FALSE if the current page is the last one
    protected $next_page;

    // First page number; FALSE if the current page is the first one
    protected $first_page;

    // Last page number; FALSE if the current page is the last one
    protected $last_page;

    // Query offset
    protected $offset;

    /**
     * Creates a new Pagination object.
     *
     * @param   array  configuration
     * @return  Pagination
     */
    public static function factory(array $config = array())
    {
        return new Pagination($config);
    }

    /**
     * Creates a new Pagination object.
     *
     * @param   array  configuration
     * @return  void
     */
    public function __construct(array $config = array())
    {


        // Pagination setup
        $this->setup($config);

    }



    /**
     * Loads configuration settings into the object and (re)calculates pagination if needed.
     * Allows you to update config settings after a Pagination object has been constructed.
     *
     * @param   array   configuration
     * @return  object  Pagination
     */
    public function setup(array $config = array())
    {


        // Only (re)calculate pagination when needed
        if ($this->current_page === NULL
            OR isset($config[\'current_page\'])
            OR isset($config[\'total_items\'])
            OR isset($config[\'items_per_page\']))
        {

            // Calculate and clean all pagination variables
            $this->current_page = (int) $this->config[\'current_page\'];
            $this->total_items        = (int) max(0,$this->config[\'total_items\']);
            $this->items_per_page     = (int) max(1,$this->config[\'items_per_page\']);
            $this->total_pages        = (int) ceil($this->total_items / $this->items_per_page);
            $this->current_page       = (int) min(max(1,$this->current_page),max(1,$this->total_pages));
            $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1,$this->total_items);
            $this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1,$this->total_items);
            $this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
            $this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
            $this->first_page         = ($this->current_page === 1) ? FALSE : 1;
            $this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
            $this->offset             = (int) (($this->current_page - 1) * $this->items_per_page);
        }

        // Chainable method
        return $this;
    }


    /**
     * Returns a Pagination property.
     *
     * @param   string  property name
     * @return  mixed   Pagination property; NULL if not found
     */
    public function __get($key)
    {
        return isset($this->$key) ? $this->$key : NULL;
    }

    /**
     * Updates a single config setting,and recalculates pagination if needed.
     *
     * @param   string  config key
     * @param   mixed   config value
     * @return  void
     */
    public function __set($key,$value)
    {
        $this->setup(array($key => $value));
    }

} // End Pagination

 ?>
    ,除非从数据库中获得的
count()
9
或更大,否则您将永远不会看到
3
   ceil(0/4) -> 0
   ...
   ceil(8/4) -> 2
   ceil(9/4) -> 3
那么...数据库中有多少篇文章符合您查询的条件? 您没有在代码中显示出您当前在“当前页面”中的位置,因此代码如何知道您当前在哪个页面上?
$total_pages = 8;
$current_page = $_GET[\'curPage\'];

for ($i = 1; $i <= $total_pages; $i++) {
   $class = ($i == $current_page) ? \' class=\"current\"\' : \'\';
   echo <<<EOL
<a href=\"page.php?curPage=$i\"$class>$i</a>

EOL;
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-