本文為大家分享了php微信公眾號開發之翻頁查詢的具體代碼,供大家參考,具體內容如下
注意:公眾號列表最多只能列出8列,超出會報錯
- 分頁原理
- limit 開始位置 , 條數
- (當前頁數 - 1) x 每頁條數 , 每頁條數
- limit ($Page - 1) * $PageSize , $PageSize
- 0 為開始位置
- mysql_num_rows 條數
- require() 與 require_once() 開始加載,錯誤停止
- include() 與 include_once() 使用加載,錯誤跳過
- ceil 進一 向上取整
原理:
- 總共10條,每頁9條
- n條
- sum 總共幾頁 ceil ( n / 9 )
- 開始條數 : (當前頁數 - 1 ) x 每頁條數
- (key - 1)* 9
注釋:+1 是為了說明有多少頁
>10 10
核心代碼如下:
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $type = $postObj->MsgType; $customevent = $postObj->Event; $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content>%s</Content> <FuncFlag>0</FuncFlag> </xml>"; switch ($type) { case "event"; if ($customevent=="subscribe") {$contentStr = "感謝你的關注\n欄目正在搭建,敬請期待\n回復1看視頻教程";} break; case "image"; $contentStr = "你的圖片很棒!"; break; case "location"; $contentStr = "你的緯度是{$latitude},經度是{$longitude},我已經鎖定!"; break; case "link" ; $contentStr = "你的鏈接有病毒吧!"; break; case "text"; include("coon.php"); $num = "SELECT * FROM `kecheng` "; $que=mysql_query($num); $no=mysql_num_rows($que);//獲得條數 $sumpage=ceil($no/7); $page=(intval($keyword)-1)*7; $total=$no-$page+1; if($total>8) {$total=8;} $sql = "SELECT * FROM `kecheng` ORDER BY `id` DESC LIMIT {$page},7"; $query=mysql_query($sql); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>$total</ArticleCount> <Articles> <item> <Title><![CDATA[總共{$sumpage}頁,輸入頁數翻頁]]></Title> <Description><![CDATA[]]></Description> <PicUrl>http://autoguitar.duapp.com/1.jpg</PicUrl> <Url><![CDATA[]]></Url> </item>"; while($rs=mysql_fetch_array($query)){ $newsTpl.="<item> <Title>$rs[content]</Title> <Description><![CDATA[]]></Description> <PicUrl>http://dq095.applinzi.com/2.jpg</PicUrl> <Url><![CDATA[]]></Url> </item>"; } $newsTpl.="</Articles> <FuncFlag>0</FuncFlag> </xml>"; $myresultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time); echo $myresultStr; break; default; $contentStr ="此項功能尚未開發"; } $msgType="text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;
coon.php連接數據庫代碼如下:
<?php //用 戶 名 : $user //密 碼 : $pwd //主庫域名 : $host //從庫域名 : SAE_MYSQL_HOST_S //端 口 : $port //數據庫名 : $dbname $dbname = "app_dq095"; $host = "w.rdc.sae.sina.com.cn"; $port = "3306"; $user = "4k514n103z"; $pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203"; /*接著調用mysql_connect()連接服務器*/ // 連主庫 $db = mysql_connect($host,$user,$pwd); if(!$db){ die("Connect Server Failed: " . mysql_error($db)); } /*連接成功后立即調用mysql_select_db()選中需要連接的數據庫*/ if (!mysql_select_db($dbname)) { die("Select Database Failed: " . mysql_error($db)); } mysql_query("set names utf-8",$db); /*至此連接已完全建立,就可對當前數據庫進行相應的操作了*/ /*!!!注意,無法再通過本次連接調用mysql_select_db來切換到其它數據庫了!!!*/ /* 需要再連接其它數據庫,請再使用mysql_connect+mysql_select_db啟動另一個連接*/ /** * 接下來就可以使用其它標準php mysql函數操作進行數據庫操作 */
index.php整體代碼如下:
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $type = $postObj->MsgType; $customevent = $postObj->Event; $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content>%s</Content> <FuncFlag>0</FuncFlag> </xml>"; switch ($type) { case "event"; if ($customevent=="subscribe") {$contentStr = "感謝你的關注\n欄目正在搭建,敬請期待\n回復1看視頻教程";} break; case "image"; $contentStr = "你的圖片很棒!"; break; case "location"; $contentStr = "你的緯度是{$latitude},經度是{$longitude},我已經鎖定!"; break; case "link" ; $contentStr = "你的鏈接有病毒吧!"; break; case "text"; include("coon.php"); $num = "SELECT * FROM `kecheng` "; $que=mysql_query($num); $no=mysql_num_rows($que);//獲得條數 $sumpage=ceil($no/7); $page=(intval($keyword)-1)*7; $total=$no-$page+1; if($total>8) {$total=8;} $sql = "SELECT * FROM `kecheng` ORDER BY `id` DESC LIMIT {$page},7"; $query=mysql_query($sql); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>$total</ArticleCount> <Articles> <item> <Title><![CDATA[總共{$sumpage}頁,輸入頁數翻頁]]></Title> <Description><![CDATA[]]></Description> <PicUrl>http://autoguitar.duapp.com/1.jpg</PicUrl> <Url><![CDATA[]]></Url> </item>"; while($rs=mysql_fetch_array($query)){ $newsTpl.="<item> <Title>$rs[content]</Title> <Description><![CDATA[]]></Description> <PicUrl>http://dq095.applinzi.com/2.jpg</PicUrl> <Url><![CDATA[]]></Url> </item>"; } $newsTpl.="</Articles> <FuncFlag>0</FuncFlag> </xml>"; $myresultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time); echo $myresultStr; break; default; $contentStr ="此項功能尚未開發"; } $msgType="text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。