幸福

如人饮水,冷暖自知

CodeIgniter实现分页传递多个参数

CodeIgniter默认分页只带一个参数,即当前页码,通过:$this->uri->segment(3)来获取。当需要

传递多个参数的时候,就会出错,解决方法如下即下列项目实例:

<div class="container">
<div class="line number1 index0 alt2"><code class="php variable">$id</code> <code class="php plain">= </code><code class="php functions">intval</code><code class="php plain">(</code><code class="php variable">$this</code><code class="php plain">-&gt;url[</code><code class="php string">'id'</code><code class="php plain">]);</code></div>
<div class="line number2 index1 alt1"><code class="php variable">$count</code> <code class="php plain">= </code><code class="php variable">$this</code><code class="php plain">-&gt;db-&gt;where(</code><code class="php keyword">array</code><code class="php plain">(</code><code class="php string">'type'</code><code class="php plain">=&gt;1,</code><code class="php string">'pid'</code><code class="php plain">=&gt;0,</code><code class="php string">'aid'</code><code class="php plain">=&gt;</code><code class="php variable">$id</code><code class="php plain">))-&gt;get(</code><code class="php string">'test'</code><code class="php plain">)-&gt;num_rows();</code></div>
<div class="line number3 index2 alt2"><code class="php variable">$this</code><code class="php plain">-&gt;load-&gt;library(</code><code class="php string">'pagination'</code><code class="php plain">);</code></div>
<div class="line number4 index3 alt1"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'use_page_numbers'</code><code class="php plain">] = TRUE;</code></div>
<div class="line number5 index4 alt2"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'base_url'</code><code class="php plain">] = base_url(</code><code class="php string">"article/article_view/id/{$id}"</code><code class="php plain">);  </code><code class="php comments">// 传递多个参数</code></div>
<div class="line number6 index5 alt1"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'total_rows'</code><code class="php plain">] = </code><code class="php variable">$count</code><code class="php plain">; </code></div>
<div class="line number7 index6 alt2"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'per_page'</code><code class="php plain">] = 15; </code></div>
<div class="line number8 index7 alt1"><code class="php comments">// 在分页外围添加一组标签</code></div>
<div class="line number9 index8 alt2"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'full_tag_open'</code><code class="php plain">] = </code><code class="php string">"&lt;div id='page'&gt;"</code><code class="php plain">;</code></div>
<div class="line number10 index9 alt1"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'full_tag_close'</code><code class="php plain">] = </code><code class="php string">'&lt;/div&gt;'</code><code class="php plain">;</code></div>
<div class="line number11 index10 alt2"><code class="php comments">// 自定义起始连接名称</code></div>
<div class="line number12 index11 alt1"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'first_link'</code><code class="php plain">] = </code><code class="php string">'首页'</code><code class="php plain">;</code></div>
<div class="line number13 index12 alt2"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'prev_link'</code><code class="php plain">] = </code><code class="php string">'上一页'</code><code class="php plain">;</code></div>
<div class="line number14 index13 alt1"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'next_link'</code><code class="php plain">] = </code><code class="php string">'下一页'</code><code class="php plain">;</code></div>
<div class="line number15 index14 alt2"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'last_link'</code><code class="php plain">] = </code><code class="php string">'末页'</code><code class="php plain">;</code></div>
<div class="line number16 index15 alt1"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'uri_segment'</code><code class="php plain">] = 5;         </code></div>
<div class="line number17 index16 alt2"><code class="php variable">$page</code><code class="php plain">=</code><code class="php variable">$this</code><code class="php plain">-&gt;uri-&gt;segment(5);</code></div>
<div class="line number18 index17 alt1"><code class="php keyword">if</code><code class="php plain">(</code><code class="php variable">$page</code> <code class="php plain">&gt; 0) </code><code class="php variable">$page</code> <code class="php plain">= </code><code class="php variable">$page</code><code class="php plain">-1;</code></div>
<div class="line number19 index18 alt2"><code class="php variable">$this</code><code class="php plain">-&gt;pagination-&gt;initialize(</code><code class="php variable">$config</code><code class="php plain">); </code></div>
<div class="line number20 index19 alt1"><code class="php variable">$n_result</code> <code class="php plain">= </code><code class="php variable">$this</code><code class="php plain">-&gt;db</code></div>
<div class="line number21 index20 alt2"><code class="php spaces">        </code><code class="php plain">-&gt;select(</code><code class="php string">"id#nikename#content#add_time#concat(path,'-',id) as bpath"</code><code class="php plain">)</code></div>
<div class="line number22 index21 alt1"><code class="php spaces">        </code><code class="php plain">-&gt;where(</code><code class="php keyword">array</code><code class="php plain">(</code><code class="php string">'type'</code><code class="php plain">=&gt;1,</code><code class="php string">'pid'</code><code class="php plain">=&gt;0,</code><code class="php string">'aid'</code><code class="php plain">=&gt;</code><code class="php variable">$id</code><code class="php plain">))</code></div>
<div class="line number23 index22 alt2"><code class="php spaces">        </code><code class="php plain">-&gt;order_by(</code><code class="php string">'bpath'</code><code class="php plain">)</code></div>
<div class="line number24 index23 alt1"><code class="php spaces">        </code><code class="php plain">-&gt;get(</code><code class="php string">'comments'</code><code class="php plain">,</code><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'per_page'</code><code class="php plain">],</code><code class="php variable">$page</code><code class="php plain">);</code></div>
</div>

说明:

<div class="container">
<div class="line number1 index0 alt2"><code class="php variable">$config</code><code class="php plain">[</code><code class="php string">'uri_segment'</code><code class="php plain">] = 5;</code></div>
</div>
是传递多个参数最重要的地方,即跳过“多个参数”(上面实例只添加了“id”一个参数),获取当前“page”页码。CI在获取到当前页码时,底部分页所指示的当前页码会是默认选中样式,这个需要通过
<div class="container">
<div class="line number1 index0 alt2"><code class="php variable">$page</code><code class="php plain">=</code><code class="php variable">$this</code><code class="php plain">-&gt;uri-&gt;segment(5);</code></div>
<div class="line number2 index1 alt1"><code class="php keyword">if</code><code class="php plain">(</code><code class="php variable">$page</code><code class="php plain">&gt; 0) </code><code class="php variable">$page</code><code class="php plain">= </code><code class="php variable">$page</code><code class="php plain">-1;</code></div>
</div>
<div class="line number2 index1 alt1">
<div class="post_content_t">

的控制来实现。

</div>
<div class="clear"></div>
</div>


去打赏

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注