如何配置分页codeigniter?

2023-11-23

我尝试使用CodeIgniter进行分页,根据Codeigniter的手册,它应该很简单,即使在示例中是这样的

« 第一个 最后一个 »

$config['total_rows'] = $this->searchdesc_model->queryallrows();
$config['per_page'] = '10';
$config['uri_segment'] =4;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<p>';
$config['last_tag_close'] = '</p>'

$this->load->library('Company_Creation');

在视图中我只这样称呼它 pagination->create_links(); ?> (或者当我从控制器调用它时,我通过视图发送它,但我仍然只得到这个

1 2 3 >

没有办法让它看起来像这个例子,可能听起来很愚蠢,但是,任何人都可以帮助我吗?或者有类似的问题?

Thanks

EDIT 1

$config['total_rows'] = $this->searchdesc_model->queryallrows();
$config['per_page'] = '5';
$config['uri_segment'] =4;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['first_link'] = ' First';
$config['last_link'] = ' Last';
$config['last_tag_open'] = '<p>';
$config['last_tag_close'] = '</p>';
$config['next_link'] = '';
$config['next_tag_open'] = '<p id="nextbutton" style="padding-left:5px;">';
$config['next_tag_close'] = '</p>';
$config['prev_link'] = '';
$config['prev_tag_open'] = '<p id="prevbutton" style="padding-right:5px;">';
$config['prev_tag_close'] = '</p>';
$config['num_links']=4;
$data['retorno'] = $this->searchdesc_model->queryalldb($config['per_page'],$this->uri->segment(4,0));
$config['total_rows']=1000;
$this->pagination->initialize($config);

我根据我收到的一些建议这样做了,就像你说的,当有很多数据时它效果很好,我仍然喜欢一直显示第一个和下一个按钮,我在查询后设置了total_rows(我用正确的行数调用它) ),我之前也尝试过,结果是一样的,我也只需要显示 4 个数字,我正在使用 numb_links ...仍然不起作用(我不知道为什么 Ci 文档说应该起作用..)有什么想法吗?

Thanks!


生成示例所示的内容实际上非常简单。您只需要扩展分页库来适应这一点。我能够做到这一点。无论您显示多少页,它仍然显示第一个、最后一个、后退箭头和前进箭头。

如果您希望始终显示 5 个页面,其中包含前进和后退的内容,则需要有那么多结果来填充该页面。然后你设置num_links到第三页之前和之后您想要的内容。所以它将是 2。如果您在第一页,我的更改将使其在适用时显示 4 页。见下图。白色是当前页面。绿色表示页面可用。

enter image description here enter image description here

希望我已经正确解释了所有内容并且这对您有用。让我知道。

控制器

  $this->pagingConfig = array();
  $this->pagingConfig['base_url'] = 'URL';
  $this->pagingConfig['total_rows'] = 0;//TOTAL ROWS
  $this->pagingConfig['cur_page'] = 0;//CURRENT PAGE NUMBER
  $this->pagingConfig['per_page'] = 0;//YOUR RESULTS PER PAGE
  $this->pagingConfig['num_links'] = 2;//NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY
  $this->pagingConfig['first_link'] = "&lt;&lt; First";
  $this->pagingConfig['last_link'] = "Last &gt;&gt;";
  $this->pagingConfig['full_tag_open'] = "<div class='pagination'>";
  $this->pagingConfig['full_tag_close'] = "</div>";
  $this->pagingConfig['last_tag_open'] = "";
  $this->pagingConfig['first_tag_close'] = "";
  $this->pagingConfig['anchor_class'] = "page";
  $this->pagination->initialize($this->pagingConfig);
  $strPaging = $this->pagination->create_links();

扩展分页库调用

function create_links()
{
  // EDIT: ADDED THIS BECAUSE COULDN'T SEEM TO SET THIS ANYWHERE ELSE
  if ($this->anchor_class != '')
  {
     $this->anchor_class = 'class="'.$this->anchor_class.'" ';
  }

  // If our item count or per-page total is zero there is no need to continue.
  if ($this->total_rows == 0 OR $this->per_page == 0)
  {
     return '';
  }

  // Calculate the total number of pages
  $num_pages = ceil($this->total_rows / $this->per_page);

  // Is there only one page? Hm... nothing more to do here then.
  if ($num_pages == 1)
  {
     return '';
  }

  // Determine the current page number.
  $CI =& get_instance();

  if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  {
     if ($CI->input->get($this->query_string_segment) != 0)
     {
        $this->cur_page = $CI->input->get($this->query_string_segment);

        // Prep the current page - no funny business!
        $this->cur_page = (int) $this->cur_page;
     }
  }
  else
  {
     if ($CI->uri->segment($this->uri_segment) != 0)
     {
        $this->cur_page = $CI->uri->segment($this->uri_segment);

        // Prep the current page - no funny business!
        $this->cur_page = (int) $this->cur_page;
     }
  }

  $this->num_links = (int)$this->num_links;

  if ($this->num_links < 1)
  {
     show_error('Your number of links must be a positive number.');
  }

  if ( ! is_numeric($this->cur_page))
  {
     $this->cur_page = 1;
  }

  // Is the page number beyond the result range?
  // If so we show the last page
  if ($this->cur_page > $this->total_rows)
  {
     $this->cur_page = ($num_pages - 1);
  }

  // EDIT: DON'T NEED THIS THE WAY I'VE CHANGED IT
  // $uri_page_number = $this->cur_page;
  // $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);

  // EDIT: START OF MODIFIED START AND END TO WORK HOW I WANT
  $totalLinks = ($this->num_links*2)+1;
  if($totalLinks > ($this->total_rows/$this->per_page))
  {
     $totalLinks = ceil($this->total_rows/$this->per_page);
  }
  //first page
  if($this->cur_page == 1)
  {
     $start = 1;
     $end = $start + $totalLinks - 1;
  }
  //middle pages
  elseif($this->cur_page + $this->num_links <= $num_pages && $this->cur_page - $this->num_links > 0)
  {
     $start = $this->cur_page - $this->num_links;
     $end = $this->cur_page + $this->num_links;
  }
  //last couple of pages
  elseif(($this->cur_page + $totalLinks) > $num_pages)
  {
     $start = $num_pages - $totalLinks + 1;
     $end = $num_pages;
     //check to see if this is in the first half of links so it doesn't jump the paging
     if($this->cur_page <= $this->num_links)
     {
        $start = 1;
        $end = $start + $totalLinks - 1;
     }
  }
  //first couple of pages
  elseif(($this->cur_page - $totalLinks) < 1)
  {
     $start = 1;
     $end = $start + $totalLinks - 1;
  }
  // EDIT: END OF MODIFIED START AND END TO WORK HOW I WANT

  // EDIT: CODEIGNITERS BASE PAGING SETUP SEE ABOVE FOR MY CHANGES
  // $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  // $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;

  // Is pagination being used over GET or POST?  If get, add a per_page query
  // string. If post, add a trailing slash to the base URL if needed
  if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  {
     $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
  }
  else
  {
     $this->base_url = rtrim($this->base_url, '/') .'/';
  }

  // And here we go...
  $output = '';

  // Render the "First" link
  // EDIT: CHANGED TO ALWAYS SHOW FIRST LINK AT LEAST
  if  ($this->first_link !== FALSE AND $this->cur_page != 1)
  {
     $first_url = ($this->first_url == '') ? $this->base_url."1" : $this->first_url;
     $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  }
  else
  {
     $output .= $this->cur_tag_open.$this->first_link.$this->cur_tag_close;
  }

  // Render the "previous" link
  // EDIT: CHANGED TO ALWAYS SHOW PREVIOUS LINK AT LEAST
  if  ($this->prev_link !== FALSE AND $this->cur_page != 1)
  {
     $i = $this->cur_page-1;

     if ($i == 0 && $this->first_url != '')
     {
        $output .= $this->prev_tag_open.'<a  '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
     }
     else
     {
        $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
        $output .= $this->prev_tag_open.'<a  '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
     }

  }
  else
  {
     $output .= $this->cur_tag_open.$this->prev_link.$this->cur_tag_close;
  }

  // EDIT: CHANGED THIS TO ALWAYS SHOW ALL LINKS WANTED EVEN IF ON FIRST PAGE
  // Render the pages
  if ($this->display_pages !== FALSE)
  {
     // Write the digit links
     for ($loop = $start; $loop <= $end; $loop++)
     {
        // EDIT: DON'T NEED THIS THE WAY I'VE CHANGED IT
        // $i = ($loop * $this->per_page) - $this->per_page;

        if ($loop >= 0)
        {
           if ($this->cur_page == $loop)
           {
              $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
           }
           else
           {
              $n = ($loop == 0) ? '0' : $loop;

              if ($n == '' && $this->first_url != '')
              {
                 $output .= $this->num_tag_open.'<a  '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
              }
              else
              {
                 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

                 $output .= $this->num_tag_open.'<a  '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
              }
           }
        }
     }
  }

  // Render the "next" link
  // EDIT: CHANGED TO ALWAYS SHOW NEXT LINK AT LEAST
  if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
  {
     $output .= $this->next_tag_open.'<a  '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page+1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  }
  else
  {
     $output .= $this->cur_tag_open.$this->next_link.$this->cur_tag_close;
  }

  // Render the "Last" link
  // EDIT: CHANGED TO ALWAYS SHOW LAST LINK AT LEAST
  if ($this->last_link !== FALSE AND $this->cur_page != $num_pages)
  {
     $i = (($num_pages));
     $output .= $this->last_tag_open.'<a  '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  }
  else
  {
     $output .= $this->cur_tag_open.$this->last_link.$this->cur_tag_close;
  }

  // Kill double slashes.  Note: Sometimes we can end up with a double slash
  // in the penultimate link so we'll kill all double slashes.
  $output = preg_replace("#([^:])//+#", "\\1/", $output);

  // Add the wrapper HTML if exists
  $output = $this->full_tag_open.$output.$this->full_tag_close;

  return $output;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何配置分页codeigniter? 的相关文章

  • 为什么这评估为 true

    为什么这评估结果为真
  • 使用 MYSQL 将 h:mm pm/am 时间格式插入数据库

    我正在尝试将以 h mm am pm 格式写入的时间插入到存储为标准 DATETIME 格式 hh mm ss 的数据库中 但我不知道如何将发布的时间转换为标准格式所以数据库会接受它 这是我到目前为止一直在尝试的 title POST in
  • 使用 ImageMagick (PHP) 将 2 个图像并排合并为 1 个图像

    我认为这是一件容易的事 我有 2 张图片 JPG 我希望它们合并成一张图片 其中 2 张图片并排 所以我有图片 A 和图片 B 我想要图片 AB 并排 两个图像具有相同的宽度和高度 在本例中 宽度 200px 高度 300px 但是第二个图
  • 禁用 WooCommerce 手动/编辑订单的电子邮件通知

    需要 WooCommerce 专业知识 我需要禁用手动创建的订单的电子邮件通知 我必须使用处理状态 由于处理订单状态的自定义挂钩 我无法创建自定义状态 理想情况下 手动订单页面中可以勾选一个复选框 勾选后 它将禁止在每种状态下向客户发送电子
  • 谷歌如何通过图像进行搜索?

    最近 谷歌推出了图片搜索的新功能 即通过图片搜索 我们可以通过在谷歌搜索框中上传图片来搜索其他图片 这怎么可能 http images google com http images google com Look at WP 基于内容的图像
  • 如何让Apache服务index.php而不是index.html?

    如果我将以下行放入index html文件 使 Apache 包含index php file 参观index html页面向我显示了这个 这是为什么 为什么它实际上不包含 PHP 文件 正如其他人指出的那样 您很可能没有 html设置为处
  • 尝试使用 php 发送 POST 请求,无论我做什么,我都会收到“HTTP ERROR 500”

    为了发出 HTTP 请求 有人建议我尝试使用 PHP 并给了我一段代码 url https example com dashboard api data array to gt PHONE NUMBER from gt SENDER ID
  • Mono Android 中的搜索对话框

    我正在尝试根据此处的文档在 Mono Android 应用程序中实现搜索对话框 http developer android com guide topics search search dialog html http developer
  • 使用 PHP PayPal REST API 退款?

    我正在开发一个集成到 PayPal 的 REST API 中的 PHP 应用程序 我正确处理了事务并将事务 ID 保存到 MySQL 数据库中 我现在正在尝试退款 但无法让它停止给出 传入 JSON 请求未映射到 API 请求 错误 有人对
  • 如何在HTML中的PHP中注释掉HTML和PHP?

    这是我想注释掉的一行代码 h1 class post title a href title a h1 一种流行的注释方法是分别注释 html 和 php 有一个更好的方法吗
  • 获取字符串中的最后一个整数

    我需要隔离包含多个整数的字符串中最新出现的整数 我怎样才能得到23代替1 for lastnum1 text 1 out of 23 lastnum1 this gt getEval eregi replace out of text 你可
  • Facebook 应用程序无法获取会话

    我正在 Heroku 上为 Facebook 开发一个非常基本的 PHP 应用程序 它显示非常基本的用户信息 如姓名 个人资料图片 但该应用程序在 getToken 方法中停止 我在登录我的个人资料后尝试了该应用程序 但仍然出现相同的消息
  • Mysqli 更新抛出 Call to a member function bind_param() 错误[重复]

    这个问题在这里已经有答案了 我有一个 70 80 字段表单 需要插入到表中 因此我首先根据表单中的输入名称在数据库中创建了一个表 而不是手动创建一个巨大的插入语句 这是我使用的代码创建 更改表 function createTable ar
  • 如何在 Zend MVC 中实现 SSL

    我之前已经通过使用特定的安全文件夹 例如服务器上的 https 文件夹与 http 文件夹 实现了安全页面 我已经开始使用 Zend Framework 并希望应用程序的某些部分 例如登录 使用 https 我在谷歌上搜索过 甚至在这里搜索
  • 如何通过php获取网页的Open Graph协议?

    PHP 有一个简单的命令来获取网页的元标记 get meta tags 但这仅适用于具有名称属性的元标记 然而 开放图谱协议如今变得越来越流行 从网页获取 opg 值的最简单方法是什么 例如 我看到的基本方法是通过 cURL 获取页面并使用
  • 使用 json_encode() 函数在 PHP 数组中生成 JSON 键值对

    我正在尝试以特定语法获取 JSON 输出 这是我的代码 ss array 1 jpg 2 jpg dates array eu gt 59 99 us gt 39 99 array1 array name gt game1 publishe
  • 如何在 phalcon 框架中同时连接多个数据库在模型类中同时使用两个而不仅仅是一个

    在我的代码中我有两个数据库ABC and XYZ 我想在同一模型中使用两个数据库 而不是 phalcon 中的解决方案是什么 如何为此实现多个数据库连接 one
  • WordPress 自定义帖子类型未显示在搜索结果中

    我在 WordPress 中遇到自定义帖子类型 测验 和搜索的问题 自定义帖子类型未显示在我的搜索结果页面中 我的搜索结果中仅显示默认的帖子内容 以下是我使用的代码 函数 php函数create posttype register post
  • 从所有会话中注销

    我有一个注销选项 这是我的代码 session start session destroy setcookie key time 60 60 24 setcookie username time 60 60 24 我想添加另一个选项来注销所
  • 如何使用 php 在 sql 查询中转义引号?

    我有一个疑问 sql SELECT CustomerID FROM tblCustomer WHERE EmailAddress addslashes POST username AND Password addslashes POST p

随机推荐

  • 如何使UIImagePickerController StatusBar lightContent风格?

    当我呈现 UIImagePickerController 时状态栏文本颜色仍然是黑色 如何制作这样的 只需三步 1 Add UINavigationControllerDelegate UIImagePickerControllerDele
  • n 球坐标系到笛卡尔坐标系

    Is there any efficient way of changing between Cartesian coordinate system and n spherical one The transformation is as
  • 如何设置 GraphQL 查询,以便需要一个或另一个参数,但不能同时需要两者

    我刚刚开始掌握 GraphQL 我设置了以下查询 type UserType args id name id type new GraphQLNonNull GraphQLID email name email type new Graph
  • 为什么我无法更改 Rhino Mocks 存根对象中的返回值?

    如果这是一个愚蠢的问题 请原谅我 但我在嘲笑方面还很陌生 并且正在努力解决这个问题 我有一些单元测试 使用内置的 Visual Studio 2010 Professional 测试功能 它们使用方法所需的存根 我创建了一个存根 并为几个属
  • 嵌套字典到嵌套转发器 asp.net c#

    我正在制作一个 asp page 它将显示有关公司资产的分层信息 为了获取数据 我使用了 lambda 表达式 FASAssetInfoDataContext fasInfo new FASAssetInfoDataContext var
  • Imagemagick仅使用一个核心

    我正在运行一个 8 核的 Ubuntu 服务器 然而 imagemagick 始终只使用 1 个单核 跑步identify version返回 Version ImageMagick 6 6 9 7 2012 08 17 Q16 http
  • 从 OWL 本体到 Neo4j 图形数据库的映射

    我正在与 OWL 合作 创建了一个非常大的本体 我使用曼彻斯特大学开发的OWL API http owlapi sourceforge net 问题是 只有使用 OWL API 本体才会加载到内存中 有两种有价值的解决方案可以将数据从本体传
  • 滚动视图没有完全向下滚动

    我正在构建一个类似聊天的应用程序 它使用滚动视图在屏幕上显示用户输入的文本 我正在做的是随着更多文本附加到屏幕上 自动向下滚动滚动视图 我在用着 ScrollView my scrollview ScrollView findViewByI
  • Android自定义按钮,里面有imageview和textview?

    我正在寻找创建一个自定义按钮 理想情况下 该按钮左侧有一个图像 右侧有一个文本视图 我将如何实现这个目标 最快的方法 创建可点击的视图 其中包含 ImageView 和 TextView 并以可绘制按钮作为背景
  • MSVC - 停止标题中的警告

    我正在将 MSVC 与 CMaked 项目一起使用 因此 我在 MSVC 上启用了许多为 gcc 和 clang 启用的标志 然而 Wall 警告级别让我有些痛苦 它警告我包含头文件中的各种内容 例如 stdio h 和 boost 头文件
  • jquery - 检测div的底部是否接触浏览器窗口的底部?

    给定页面上的 div 如何检测div何时滚动到浏览器窗口底部的位置 与浏览器窗口底部齐平 我认为上面的答案不起作用 因为 offset top 是 div 和文档顶部之间的空间 并且不是可变的 这对我有用 var a mydiv offse
  • matplotlib:使图例键成为方形

    我正在使用 matplotlib 并且希望在制作条形图时将图例中的键更改为正方形而不是矩形 有没有办法指定这一点 我现在拥有的 我想要的是 Thanks 如果您想要一个非常快速和肮脏的解决方案来获得近似平方 可能需要根据您的绘图进行一些微调
  • 如何在Java中执行无符号到有符号的转换?

    假设我从输入设备读取了这些字节 6F D4 06 40 该数字是毫弧秒格式的经度读数 最高位 0x80000000 基本上始终为零 并且在本问题中被忽略 我可以轻松地将字节转换为unsigned整数 1876166208 但是如何将该无符号
  • 作为好友的模板参数

    在 C 03 中 以下内容是非法的 尽管某些编译器支持它 template
  • 如何判断给定的URL链接是视频还是图片?

    我正在尝试获取用户输入的给定 URL 并确定该 URL 是否指向图像或视频 示例用例 当用户粘贴 YouTube 视频的 URL 时 保存时页面将自动显示嵌入式 YouTube 播放器 当用户在 Flickr 中发布图片的 URL 时 在保
  • 从服务器获取数据后如何将数据存储在房间数据库中

    我在我的 android 应用程序中使用 Retrofit2 和 Rxjava2 作为网络库 使用 NodeJS 和 MongoDB 作为后端服务 我想从服务器获取数据并将数据存储在房间数据库中 以便用户再次打开应用程序时它会从房间获取数据
  • 模型视图投影矩阵的用途

    我们使用模型视图投影矩阵的目的是什么 为什么着色器需要模型视图投影矩阵 模型 视图和投影矩阵是三个独立的矩阵 模型从对象的局部坐标空间映射到世界空间 从世界空间到相机空间的视图 从相机到屏幕的投影 如果您组合了所有三个 则可以使用一个结果从
  • wait((int *)0) 的含义

    一个使用这样的等待函数的程序是 include
  • android Zoom-to-Fit All Markers on Google Map v2 [关闭]

    很难说出这里问的是什么 这个问题模棱两可 含糊不清 不完整 过于宽泛或言辞激烈 无法以目前的形式合理回答 如需帮助澄清此问题以便重新打开 访问帮助中心 如何放大 缩小地图视图以覆盖所有标记 我正在审查 sdk 中给出的 gmap V2 示例
  • 如何配置分页codeigniter?

    我尝试使用CodeIgniter进行分页 根据Codeigniter的手册 它应该很简单 即使在示例中是这样的 第一个 最后一个 config total rows this gt searchdesc model gt queryallr