Home > Article > Backend Development > Pagination class, php paging class_PHP tutorial
page.class.php
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span> <span> 3</span> <span>* PHP分页类 </span><span> 4</span> <span>* @package Page </span><span> 5</span> <span>* @Created 2013-03-27 </span><span> 6</span> <span>* @Modify 2013-03-27 </span><span> 7</span> <span>* @link http://www.60ie.net </span><span> 8</span> <span>* Example: </span><span> 9</span> <span>$myPage=new Pager(1300,intval($CurrentPage)); </span><span> 10</span> <span>$pageStr= $myPage->GetPagerContent(); </span><span> 11</span> <span>echo $pageStr; </span><span> 12</span> <span>*/</span> <span> 13</span> <span>class</span><span> Pager { </span><span> 14</span> <span>private</span> <span>$pageSize</span> = 10<span>; </span><span> 15</span> <span>private</span> <span>$pageIndex</span>; <span>//</span><span>页码</span> <span> 16</span> <span>private</span> <span>$totalNum</span>; <span>//</span><span>数据总数</span> <span> 17</span> <span> 18</span> <span>private</span> <span>$totalPagesCount</span>; <span>//</span><span>总页数</span> <span> 19</span> <span> 20</span> <span>private</span> <span>$pageUrl</span>; <span>// </span><span> 21</span> <span>private</span> <span>static</span> <span>$_instance</span><span>; </span><span> 22</span> <span> 23</span> <span>public</span> <span>function</span> __construct(<span>$p_totalNum</span>, <span>$p_pageIndex</span>, <span>$p_pageSize</span> = 10,<span>$p_initNum</span>=3,<span>$p_initMaxNum</span>=5<span>) { </span><span> 24</span> <span>if</span> (! <span>isset</span> ( <span>$p_totalNum</span> ) || !<span>isset</span>(<span>$p_pageIndex</span><span>)) { </span><span> 25</span> <span>die</span> ( "pager initial error"<span> ); </span><span> 26</span> <span>} </span><span> 27</span> <span> 28</span> <span>$this</span>->totalNum = <span>$p_totalNum</span>; <span>//</span><span>数据总数</span> <span> 29</span> <span>$this</span>->pageIndex = <span>$p_pageIndex</span><span>; </span><span> 30</span> <span>$this</span>->pageSize = <span>$p_pageSize</span><span>; </span><span> 31</span> <span>$this</span>->initNum=<span>$p_initNum</span><span>; </span><span> 32</span> <span>$this</span>->initMaxNum=<span>$p_initMaxNum</span><span>; </span><span> 33</span> <span>$this</span>->totalPagesCount= <span>ceil</span>(<span>$p_totalNum</span> / <span>$p_pageSize</span><span>); </span><span> 34</span> <span>$this</span>->pageUrl=<span>$this</span>-><span>_getPageUrl(); </span><span> 35</span> <span> 36</span> <span>$this</span>-><span>_initPagerLegal(); </span><span> 37</span> <span>} </span><span> 38</span> <span> 39</span> <span> 40</span> <span>/*</span><span>* </span><span> 41</span> <span>* 获取去除page部分的当前URL字符串 </span><span> 42</span> <span>* </span><span> 43</span> <span>* @return String URL字符串 </span><span> 44</span> <span>*/</span> <span> 45</span> <span>private</span> <span>function</span><span> _getPageUrl() { </span><span> 46</span> <span>$CurrentUrl</span> = <span>$_SERVER</span>["REQUEST_URI"]; <span>//</span><span>获取URI</span> <span> 47</span> <span>$arrUrl</span> = <span>parse_url</span>(<span>$CurrentUrl</span>); <span>//</span><span>解析URI,返回一关联数组</span> <span> 48</span> <span>$urlQuery</span> = <span>$arrUrl</span>["query"]; <span>//</span><span>获取数组键为query的值 </span><span> 49</span> <span>//将URI中的“?page=页码”置换为“?page”</span> <span> 50</span> <span>if</span>(<span>$urlQuery</span><span>){ </span><span> 51</span> <span>//</span><span>$urlQuery = preg_replace("/(^|&)page=".$this->pageIndex."/", "", $urlQuery);</span> <span> 52</span> <span>$urlQuery</span> = ""<span>; </span><span> 53</span> <span>$CurrentUrl</span> = <span>str_replace</span>(<span>$arrUrl</span>["query"], <span>$urlQuery</span>, <span>$CurrentUrl</span><span>); </span><span> 54</span> <span>echo</span> <span>$CurrentUrl</span><span>; </span><span> 55</span> <span>if</span>(<span>$urlQuery</span><span>){ </span><span> 56</span> <span>$CurrentUrl</span>.="&page"<span>; </span><span> 57</span> }<span>else</span><span>{ </span><span> 58</span> <span>$CurrentUrl</span>.="page"<span>; </span><span> 59</span> <span>} </span><span> 60</span> } <span>else</span><span> { </span><span> 61</span> <span>$CurrentUrl</span>.="?page"<span>; </span><span> 62</span> <span>} </span><span> 63</span> <span> 64</span> <span>return</span> <span>$CurrentUrl</span><span>; </span><span> 65</span> <span> 66</span> <span>} </span><span> 67</span> <span>/*</span> <span> 68</span> <span>*设置页面参数合法性 </span><span> 69</span> <span>*@return void </span><span> 70</span> <span>*/</span> <span> 71</span> <span>private</span> <span>function</span><span> _initPagerLegal() </span><span> 72</span> <span>{ </span><span> 73</span> <span>//</span><span>页码不合法时,修正页码</span> <span> 74</span> <span>if</span>((!<span>is_numeric</span>(<span>$this</span>->pageIndex)) || <span>$this</span>->pageIndex<1<span>) </span><span> 75</span> <span>{ </span><span> 76</span> <span>$this</span>->pageIndex=1<span>; </span><span> 77</span> }<span>elseif</span>(<span>$this</span>->pageIndex > <span>$this</span>-><span>totalPagesCount) </span><span> 78</span> <span>{ </span><span> 79</span> <span>$this</span>->pageIndex=<span>$this</span>-><span>totalPagesCount; </span><span> 80</span> <span>} </span><span> 81</span> <span>} </span><span> 82</span> <span> 83</span> <span>public</span> <span>function</span><span> GetPagerContent() { </span><span> 84</span> <span>$str</span> = "<div class=\"Pagination\">"<span>; </span><span> 85</span> <span>//</span><span>首页 上一页 </span> <span> 86</span> <span>/*</span><span>if($this->pageIndex==1) </span><span> 87</span> <span>{ </span><span> 88</span> <span>$str .="<a href='javascript:void(0)' class='tips' title='首页'>首页</a> "."\n"; </span><span> 89</span> <span>$str .="<a href='javascript:void(0)' class='tips' title='上一页'>上一页</a> "."\n"."\n"; </span><span> 90</span> <span>}else </span><span> 91</span> <span>{ </span><span> 92</span> <span>$str .="<a href='{$this->pageUrl}=1' class='tips' title='首页'>首页</a> "."\n"; </span><span> 93</span> <span>$str .="<a href='{$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一页'>上一页</a> "."\n"."\n"; </span><span> 94</span> <span>} </span><span> 95</span> <span>*/</span> <span> 96</span> <span>if</span>(<span>$this</span>->pageIndex!=1<span>){ </span><span> 97</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}=1' class='tips' title='首页'>首页</a> "."\n"<span>; </span><span> 98</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}=".(<span>$this</span>->pageIndex-1)."' class='tips' title='上一页'>上一页</a> "."\n"."\n"<span>; </span><span> 99</span> <span>} </span><span>100</span> <span>/*</span> <span>101</span> <span>102</span> <span>除首末后 页面分页逻辑 </span><span>103</span> <span>104</span> <span>*/</span> <span>105</span> <span>//</span><span>10页(含)以下 </span> <span>106</span> <span>$currnt</span>=""<span>; </span><span>107</span> <span>if</span>(<span>$this</span>->totalPagesCount<=10<span>) </span><span>108</span> <span>{ </span><span>109</span> <span>110</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=<span>$this</span>->totalPagesCount;<span>$i</span>++<span>) </span><span>111</span> <span>112</span> <span>{ </span><span>113</span> <span>if</span>(<span>$i</span>==<span>$this</span>-><span>pageIndex) </span><span>114</span> { <span>$currnt</span>=" class='current'"<span>;} </span><span>115</span> <span>else</span> <span>116</span> { <span>$currnt</span>=""<span>; } </span><span>117</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>} ' {<span>$currnt</span>}><span>$i</span></a>"."\n"<span> ; </span><span>118</span> <span>} </span><span>119</span> }<span>else</span> <span>//</span><span>10页以上 </span> <span>120</span> { <span>if</span>(<span>$this</span>->pageIndex<3) <span>//</span><span>当前页小于3 </span> <span>121</span> <span>{ </span><span>122</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=3;<span>$i</span>++<span>) </span><span>123</span> <span>{ </span><span>124</span> <span>if</span>(<span>$i</span>==<span>$this</span>-><span>pageIndex) </span><span>125</span> { <span>$currnt</span>=" class='current'"<span>;} </span><span>126</span> <span>else</span> <span>127</span> { <span>$currnt</span>=""<span>; } </span><span>128</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>} ' {<span>$currnt</span>}><span>$i</span></a>"."\n"<span> ; </span><span>129</span> <span>} </span><span>130</span> <span>131</span> <span>$str</span>.="<span class=\"dot\">……</span>"."\n"<span>; </span><span>132</span> <span>133</span> <span>for</span>(<span>$i</span>=<span>$this</span>->totalPagesCount-3+1;<span>$i</span><=<span>$this</span>->totalPagesCount;<span>$i</span>++)<span>//</span><span>功能1 </span> <span>134</span> <span>{ </span><span>135</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>}' ><span>$i</span></a>"."\n"<span> ; </span><span>136</span> <span>137</span> <span>} </span><span>138</span> }<span>elseif</span>(<span>$this</span>->pageIndex<=5) <span>//</span><span> 5 >= 当前页 >= 3 </span> <span>139</span> <span>{ </span><span>140</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=(<span>$this</span>->pageIndex+1);<span>$i</span>++<span>) </span><span>141</span> <span>{ </span><span>142</span> <span>if</span>(<span>$i</span>==<span>$this</span>-><span>pageIndex) </span><span>143</span> { <span>$currnt</span>=" class='current'"<span>;} </span><span>144</span> <span>else</span> <span>145</span> { <span>$currnt</span>=""<span>; } </span><span>146</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>} ' {<span>$currnt</span>}><span>$i</span></a>"."\n"<span> ; </span><span>147</span> <span>148</span> <span>} </span><span>149</span> <span>$str</span>.="<span class=\"dot\">……</span>"."\n"<span>; </span><span>150</span> <span>151</span> <span>for</span>(<span>$i</span>=<span>$this</span>->totalPagesCount-3+1;<span>$i</span><=<span>$this</span>->totalPagesCount;<span>$i</span>++)<span>//</span><span>功能1 </span> <span>152</span> <span>{ </span><span>153</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>}' ><span>$i</span></a>"."\n"<span> ; </span><span>154</span> <span>155</span> <span>} </span><span>156</span> <span>157</span> }<span>elseif</span>(5<<span>$this</span>->pageIndex && <span>$this</span>->pageIndex<=<span>$this</span>->totalPagesCount-5 ) <span>//</span><span>当前页大于5,同时小于总页数-5</span> <span>158</span> <span>159</span> <span>{ </span><span>160</span> <span>161</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=3;<span>$i</span>++<span>) </span><span>162</span> <span>{ </span><span>163</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>}' ><span>$i</span></a>"."\n"<span> ; </span><span>164</span> <span>} </span><span>165</span> <span>$str</span>.="<span class=\"dot\">……</span>"<span>; </span><span>166</span> <span>for</span>(<span>$i</span>=<span>$this</span>->pageIndex-1 ;<span>$i</span><=<span>$this</span>->pageIndex+1 && <span>$i</span><=<span>$this</span>->totalPagesCount-5+1;<span>$i</span>++<span>) </span><span>167</span> <span>{ </span><span>168</span> <span>if</span>(<span>$i</span>==<span>$this</span>-><span>pageIndex) </span><span>169</span> { <span>$currnt</span>=" class='current'"<span>;} </span><span>170</span> <span>else</span> <span>171</span> { <span>$currnt</span>=""<span>; } </span><span>172</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>} ' {<span>$currnt</span>}><span>$i</span></a>"."\n"<span> ; </span><span>173</span> <span>} </span><span>174</span> <span>$str</span>.="<span class=\"dot\">……</span>"<span>; </span><span>175</span> <span>176</span> <span>for</span>(<span>$i</span>=<span>$this</span>->totalPagesCount-3+1;<span>$i</span><=<span>$this</span>->totalPagesCount;<span>$i</span>++<span>) </span><span>177</span> <span>{ </span><span>178</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>}' ><span>$i</span></a>"."\n"<span> ; </span><span>179</span> <span>180</span> <span>} </span><span>181</span> }<span>else</span> <span>182</span> <span>{ </span><span>183</span> <span>184</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=3;<span>$i</span>++<span>) </span><span>185</span> <span>{ </span><span>186</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>}' ><span>$i</span></a>"."\n"<span> ; </span><span>187</span> <span>} </span><span>188</span> <span>$str</span>.="<span class=\"dot\">……</span>"."\n"<span>; </span><span>189</span> <span>190</span> <span>for</span>(<span>$i</span>=<span>$this</span>->totalPagesCount-5;<span>$i</span><=<span>$this</span>->totalPagesCount;<span>$i</span>++)<span>//</span><span>功能1 </span> <span>191</span> <span>{ </span><span>192</span> <span>if</span>(<span>$i</span>==<span>$this</span>-><span>pageIndex) </span><span>193</span> { <span>$currnt</span>=" class='current'"<span>;} </span><span>194</span> <span>else</span> <span>195</span> { <span>$currnt</span>=""<span>; } </span><span>196</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$i</span>} ' {<span>$currnt</span>}><span>$i</span></a>"."\n"<span> ; </span><span>197</span> <span>198</span> <span>} </span><span>199</span> <span>} </span><span>200</span> <span>201</span> <span>} </span><span>202</span> <span>203</span> <span>204</span> <span>205</span> <span>206</span> <span>/*</span> <span>207</span> <span>208</span> <span>除首末后 页面分页逻辑结束 </span><span>209</span> <span>210</span> <span>*/</span> <span>211</span> <span>212</span> <span>//</span><span>下一页 末页 </span> <span>213</span> <span>/*</span><span>if($this->pageIndex==$this->totalPagesCount) </span><span>214</span> <span>{ </span><span>215</span> <span>$str .="\n"."<a href='javascript:void(0)' class='tips' title='下一页'>下一页</a>"."\n" ; </span><span>216</span> <span>$str .="<a href='javascript:void(0)' class='tips' title='末页'>末页</a>"."\n"; </span><span>217</span> <span>}else </span><span>218</span> <span>{ </span><span>219</span> <span>$str .="\n"."<a href='{$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一页'>下一页</a> "."\n"; </span><span>220</span> <span>$str .="<a href='{$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末页'>末页</a> "."\n" ; </span><span>221</span> <span>} </span><span>222</span> <span>*/</span> <span>223</span> <span>if</span>(<span>$this</span>->pageIndex!=<span>$this</span>-><span>totalPagesCount){ </span><span>224</span> <span>$str</span> .="\n"."<a href='{<span>$this</span>->pageUrl}=".(<span>$this</span>->pageIndex+1)."' class='tips' title='下一页'>下一页</a> "."\n"<span>; </span><span>225</span> <span>$str</span> .="<a href='{<span>$this</span>->pageUrl}={<span>$this</span>->totalPagesCount}' class='tips' title='末页'>末页</a> "."\n"<span> ; </span><span>226</span> <span>} </span><span>227</span> <span>$str</span> .= "</div>"<span>; </span><span>228</span> <span>return</span> <span>$str</span><span>; </span><span>229</span> <span>} </span><span>230</span> <span>231</span> <span>232</span> <span>233</span> <span>234</span> <span>/*</span><span>* </span><span>235</span> <span>* 获得实例 </span><span>236</span> <span>* @return </span><span>237</span> <span>*/</span> <span>238</span> <span>//</span><span> static public function getInstance() { </span><span>239</span> <span>// if (is_null ( self::$_instance )) { </span><span>240</span> <span>// self::$_instance = new pager (); </span><span>241</span> <span>// } </span><span>242</span> <span>// return self::$_instance; </span><span>243</span> <span>// }</span> <span>244</span> <span>245</span> <span>246</span> <span>} </span><span>247</span> ?>
Implementation class page.php
<span> 1</span> <head> <span> 2</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <span> 3</span> <title>----分页演示-----</title> <span> 4</span> <link href="pager.css" type="text/css" rel="stylesheet" /> <span> 5</span> </head> <span> 6</span> <body> <span> 7</span> <?<span>php </span><span> 8</span> <span>include</span> "pager.class.php"<span>; </span><span> 9</span> <span>$CurrentPage</span>=<span>isset</span>(<span>$_GET</span>['page'])?<span>$_GET</span>['page']:1<span>; </span><span>10</span> <span>$myPage</span>=<span>new</span> pager(1300,<span>intval</span>(<span>$CurrentPage</span><span>)); </span><span>11</span> <span>$pageStr</span>= <span>$myPage</span>-><span>GetPagerContent(); </span><span>12</span> <span>echo</span> <span>$pageStr</span><span>; </span><span>13</span> ?> <span>14</span> </body> <span>15</span> </html>
Style settings
<span> 1</span> <span>body,html</span>{<span> padding</span>:<span>0px</span>;<span> margin</span>:<span>0px</span>;<span> color</span>:<span>#333333</span>;<span> font-family</span>:<span>"宋体",Arial,Lucida,Verdana,Helvetica,sans-serif</span>;<span> font-size</span>:<span>12px</span>;<span> line-height</span>:<span>150%</span>;} <span> 2</span> <span> 3</span> <span>h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label</span>{<span>margin</span>:<span>0</span>;<span> padding</span>:<span>0</span>;<span> border</span>:<span>none</span>;<span> list-style-type</span>:<span>none</span>;} <span> 4</span> <span> 5</span> <span>/*</span><span>*前台分页样式*</span><span>*/</span> <span> 6</span> <span> 7</span> <span>.Pagination </span>{<span>margin</span>:<span>10px 0 0</span>;<span>padding</span>:<span>5px 0</span>;<span>text-align</span>:<span>rightright</span>;<span> height</span>:<span>20px</span>;<span> line-height</span>:<span>20px</span>;<span> font-family</span>:<span>Arial, Helvetica, sans-serif,"宋体"</span>;} <span> 8</span> <span> 9</span> <span>.Pagination a </span>{<span>margin-left</span>:<span>2px</span>;<span>padding</span>:<span>2px 7px 2px</span>;} <span>10</span> <span>11</span> <span>.Pagination .dot</span>{<span> border</span>:<span>medium none</span>;<span> padding</span>:<span>4px 8px</span>} <span>12</span> <span>13</span> <span>.Pagination a:link, .Pagination a:visited </span>{<span>border</span>:<span>1px solid #dedede</span>;<span>color</span>:<span>#696969</span>;<span>text-decoration</span>:<span>none</span>;} <span>14</span> <span>15</span> <span>.Pagination a:hover, .Pagination a:active, .Pagination a.current:link, .Pagination a.current:visited </span>{<span>border</span>:<span>1px solid #dedede</span>;<span>color</span>:<span>#fff</span>;<span> background-color</span>:<span>#ff6600</span>;<span> background-image</span>:<span>none</span>;<span> border</span>:<span>#ff6600 solid 1px</span>;} <span>16</span> <span>17</span> <span>.Pagination .selectBar</span>{<span> border</span>:<span>#dedede solid 1px</span>;<span> font-size</span>:<span>12px</span>;<span> width</span>:<span>95px</span>;<span> height</span>:<span>21px</span>;<span> line-height</span>:<span>21px</span>;<span> margin-left</span>:<span>10px</span>;<span> display</span>:<span>inline</span>} <span>18</span> <span>19</span> <span>.Pagination a.tips</span>{<span>_padding</span>:<span>4px 7px 1px</span>;}
Achieve results