{"id":199361,"date":"2025-05-03T08:24:30","date_gmt":"2025-05-03T00:24:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199361\/"},"modified":"2025-05-03T08:24:30","modified_gmt":"2025-05-03T00:24:30","slug":"%e4%bc%98%e5%8c%96%e9%87%8d%e5%86%99ecshop%e5%88%86%e9%a1%b5get_pager%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199361\/","title":{"rendered":"\u4f18\u5316\u91cd\u5199ecshop\u5206\u9875get_pager\u51fd\u6570"},"content":{"rendered":"<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> \u5728ecshop\u5206\u9875\u7684\u65f6\u5019\u8981\u6309\u7167\u81ea\u5df1\u7684\u9700\u6c42\u5206\u9875\uff0c\u6240\u6709\u5728ecshop\u539f\u6709\u7684get_pager\u5206\u9875\u51fd\u6570\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u4fee\u6539\uff1a<\/p>\n<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> <span style=\"padding: 0px;margin: 0px;line-height: 1.6em\">function my_get_pager($url,$param,$record_count,$page = 1,$size = 10){<\/span><\/p>\n<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> &nbsp;&nbsp; &nbsp;$size =intval($size);\/\/\u6bcf\u9875\u663e\u793a\u51e0\u6761\u8bb0\u5f55<br \/> &nbsp;&nbsp; &nbsp;if($size&lt;1){ \/\/\u5982\u679c\u6bcf\u9875\u663e\u793a\u7684\u8bb0\u5f55\u5c0f\u4e8e1\u7684\u8bdd<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$size = 10;\/\/\u5c06\u6bcf\u9875\u663e\u793a\u8bb0\u5f55\u6761\u6570\u8bbe\u7f6e\u4e3a10<br \/> &nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;$page = intval($page);\/\/\/\u5f53\u524d\u9875\u7801<br \/> &nbsp;&nbsp; &nbsp;if ($page &lt; 1) \/\/\/\u5982\u679c\u5f53\u524d\u9875\u7801\u5c0f\u4e8e1\u7684\u8bdd<br \/> &nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$page = 1;\/\/\/\u5c06\u5f53\u524d\u9875\u7801\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a1<br \/> &nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;$record_count = intval($record_count);\/\/\/\u8bb0\u5f55\u603b\u6570\u91cf<br \/> &nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;$page_count = $record_count &gt; 0 ? intval(ceil($record_count \/ $size)) : 1; \/\/\/\u603b\u9875\u6570<br \/> &nbsp;&nbsp; &nbsp;if ($page &gt; $page_count) \/\/\/\u5982\u679c\u5f53\u524d\u9875\u7801\u5927\u4e8e\u603b\u9875\u6570<br \/> &nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$page = $page_count;\/\/\/\u5219\u5c06\u603b\u9875\u6570\u8d4b\u503c\u7ed9\u5f53\u524d\u9875\u7801<br \/> &nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;<\/p>\n<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> &nbsp;<\/p>\n<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> &nbsp;&nbsp; &nbsp;$page_prev &nbsp;= ($page &gt; 1) ? $page \u2013 1 : 1; \/\/\/\u524d\u4e00\u9875<br \/> &nbsp;&nbsp; &nbsp;$page_next &nbsp;= ($page &lt; $page_count) ? $page + 1 : $page_count; \/\/\/\u540e\u4e00\u9875<\/p>\n<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> &nbsp;&nbsp; &nbsp; \/* \u5c06\u53c2\u6570\u5408\u6210url\u5b57\u4e32 *\/<br \/> &nbsp;&nbsp; &nbsp;$param_url = &#8216;?&#8217;; &nbsp;\/\/\/\u53c2\u6570\u7ec4\u6210\u7684url\u5b57\u7b26\u4e32 \u5982\uff1a&#8221;?act=list&#8221;<br \/> &nbsp;&nbsp; &nbsp;foreach ($param AS $key =&gt; $value) \/\/\/\u952e\u503c\u5bf9\u6570\u7ec4\u53c2\u6570<br \/> &nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$param_url .= $key . &#8216;=&#8217; . $value . &#8216;&amp;&#8217;; \/\/\/&#8221;?\u53f7\u540e\u9762\u7684\u53c2\u6570&#8221;<br \/> &nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;url&#8217;] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $url; \/\/\/\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u662f\u4e00\u4e2a\u6587\u4ef6\u540d \u5982\uff1aget_comment.php<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;start&#8217;] &nbsp; &nbsp; &nbsp; &nbsp;= ($page -1) * $size; \/\/\/\u67e5\u8be2\u65f6\u7684\u8d77\u59cb\u4f4d\u7f6e<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page&#8217;] &nbsp; &nbsp; &nbsp; &nbsp; = $page; \/\/\/\u5f53\u524d\u9875<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;size&#8217;] &nbsp; &nbsp; &nbsp; &nbsp; = $size; \/\/\/\u6bcf\u9875\u663e\u793a\u7684\u8bb0\u5f55\u6761\u6570<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;record_count&#8217;] = $record_count; &nbsp;\/\/\/\u8bb0\u5f55\u603b\u6570<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page_count&#8217;] &nbsp; = $page_count; \/\/\/\u603b\u9875\u6570&nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;<br \/> &nbsp; &nbsp; &nbsp; &nbsp; $_pagenum = 10; &nbsp; &nbsp; \/\/ \u663e\u793a\u7684\u9875\u7801<br \/> &nbsp; &nbsp; &nbsp; &nbsp; $_offset = 2; &nbsp; &nbsp; &nbsp; \/\/ \u5f53\u524d\u9875\u504f\u79fb\u503c<br \/> &nbsp; &nbsp; &nbsp; &nbsp; $_from = $_to = 0; &nbsp;\/\/ \u5f00\u59cb\u9875, \u7ed3\u675f\u9875&nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;<br \/> &nbsp;&nbsp; &nbsp;if($_pagenum &gt; $page_count) \/\/\/\u5982\u679c\u663e\u793a\u7684\u9875\u7801 \u5927\u4e8e \u603b\u9875\u6570\uff0c\u5982\uff1a\u663e\u793a10\u9875 \u603b\u9875\u6570\u4e3a8 \u5219\u663e\u793a1-8<br \/> &nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_from = 1; \/\/\/\u4ece1\u5f00\u59cb<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_to = $page_count; \/\/\/\u5230 \u603b\u9875\u6570 \u4e3a\u6b62<br \/> &nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;else \/\/\/\u5982\u679c \u663e\u793a\u7684\u9875\u7801 \u5c0f\u4e8e \u603b\u9875\u7801\uff0c\u5982\uff1a\u663e\u793a10\u9875 \u603b\u9875\u6570\u4e3a15<br \/> &nbsp;&nbsp; &nbsp;{\/\/\/(1)\u5047\u5982\u5f53\u524d\u9875\u4e3a2,\u5219$_from\u4e3a0\uff0c$_to\u4e3a9 &nbsp; (2)\u5047\u5982\u5f53\u524d\u9875\u4e3a6\uff0c\u5219$_from\u4e3a4,$_to\u4e3a13 &nbsp;<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_from = $page \u2013 $_offset; &nbsp; &nbsp;;\/\/\/(1)$_from\u4e3a0 (2)$_from\u4e3a4 \u5f53\u524d\u9875-\u5f53\u524d\u9875\u504f\u79fb\u91cf\uff0c\u5982\uff1a 1-10\u30012-11\u30013-12\u30014-13\u30015-14\u30016-15\u3001\u4e0d\u4f1a\u51fa\u73b07-16\u30018-17\uff0c\u56e0\u4e3a\u603b\u9875\u6570\u662f15 <span style=\"color:#ffffff\">\/\/www.ldhost.cn<\/span><br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_to = $_from + $_pagenum \u2013 1; \/\/\/(1)$_to\u4e3a9 (2)$_to\u4e3a13<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if($_from &lt; 1)<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_to = $page + 1 \u2013 $_from; \/\/\/(1)\u5219$_to\u4e3a3<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_from = 1; \/\/\/(1)$_from\u91cd\u65b0\u590d\u5236\u4e3a1<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if($_to \u2013 $_from &lt; $_pagenum) \/\/(1)3-1 \u5c0f\u4e8e \u8981\u663e\u793a\u7684\u9875\u7801\u6570<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_to = $_pagenum; \/\/\/(1)\u5219$_to\u91cd\u65b0\u590d\u5236\u4e3a10<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif($_to &gt; $page_count) \/\/(2)\u5982\u679c13 \u5927\u4e8e 10\u7684\u8bdd &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u5982\u679c14 \u5927\u4e8e 10\u7684\u8bdd<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_from = $page_count \u2013 $_pagenum + 1; \/\/\/(2)$_from\u4e3a\u8d77\u59cb\u9875\u6570\uff1a15-10+1=6<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_to = $page_count; \/\/(2)$_to\u4e3a\u603b\u9875\u6570\uff1a15<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;$url_format = $url . $param_url . &#8216;page=&#8217;;<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page_first&#8217;] = ($page \u2013 $_offset &gt; 1 &amp;&amp; $_pagenum &lt; $page_count) ? $url_format . 1 : &#8221;;<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page_prev&#8217;] &nbsp;= ($page &gt; 1) ? $url_format . $page_prev : &#8221;;<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page_next&#8217;] &nbsp;= ($page &lt; $page_count) ? $url_format . $page_next : &#8221;;<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page_last&#8217;] &nbsp;= ($_to &lt; $page_count) ? $url_format . $page_count : &#8221;;<br \/> &nbsp;&nbsp; &nbsp;$pager[&#8216;page_number&#8217;] = array();<br \/> &nbsp;&nbsp; &nbsp;for ($i=$_from;$i&lt;=$_to;++$i)<br \/> &nbsp;&nbsp; &nbsp;{<br \/> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$pager[&#8216;page_number&#8217;][$i] = $url_format . $i;<br \/> &nbsp;&nbsp; &nbsp;}<br \/> &nbsp;&nbsp; &nbsp;return $pager;<br \/> &nbsp;};<\/p>\n<p style=\"padding: 0px;margin: 0.7em 0px;line-height: 1.8em;font-family: verdana, tahoma, arial, sans-serif;font-size: 14px\"> \u8fd4\u56de\u7684$pager \u6570\u7ec4\u91cc\u9762\u5305\u542b\u4e86\u7b2c\u4e00\u9875\/\u672b\u9875 \u3001\u4e0a\u4e00\u9875\/\u4e0b\u4e00\u9875 \u5df2\u7ecf\u5206\u9875\u663e\u793a\u7684\u7801\u6570\u53ca\u5730\u5740\uff0c\u8d4b\u503c\u5230\u524d\u7aef\u9875\u9762\u5373\u53ef\u5b9e\u4e60\u81ea\u5b9a\u4e49\u5206\u9875\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728ecshop\u5206\u9875\u7684\u65f6\u5019\u8981\u6309\u7167\u81ea&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-199361","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199361"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199361\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}