PHP中用数组实现分页的方法PHP中的分页写法大多数是用LIMIT关键字进行实现,可是如果有应用的过种中也有用LIMIT不太方便的时候,至于什么情况,这里不再做列举,我就碰到了这样的情况,所以万般无奈之下,想到了一个很笨的方法,通过数据库一次查询,然后用数组来实现分页,转载这个方法,请注明来源www.chhua.com,以下是代码:

1
2
3
4
5
6
7
8
<span style="color: #000000;font-weight: bold">function</span> getPageArr<span style="color: #009900">(</span><span style="color: #000088">$pageArr</span><span style="color: #339933">,</span><span style="color: #000088">$starNum</span><span style="color: #339933">,</span><span style="color: #000088">$pageNum</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//获取分页数组</span>
		<span style="color: #666666;font-style: italic">//参数刘明:数组,开始位置,每页数量</span>
		<span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$i</span><span style="color: #339933">=</span><span style="color: #000088">$starNum</span><span style="color: #339933">;</span><span style="color: #000088">$i</span><span style="color: #339933"><</span><span style="color: #000088">$starNum</span><span style="color: #339933">+</span><span style="color: #000088">$pageNum</span><span style="color: #339933">;</span><span style="color: #000088">$i</span><span style="color: #339933">++</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$i</span><span style="color: #339933">>=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/count" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">count</span></a><span style="color: #009900">(</span><span style="color: #000088">$pageArr</span><span style="color: #009900">)</span> or <a rel="noopener noreferrer nofollow" href="http://www.php.net/count" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">count</span></a><span style="color: #009900">(</span><span style="color: #000088">$pageArr</span><span style="color: #009900">)</span><span style="color: #339933">==</span><span style="color: #cc66cc">0</span><span style="color: #009900">)</span> <span style="color: #b1b100">break</span><span style="color: #339933">;</span>
			<span style="color: #000088">$newArr</span><span style="color: #009900">[</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$pageArr</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$newArr</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>