php微信公众号开发之翻页查询

时间:2021-05-26

本文为大家分享了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"; // 连主库 $db = mysql_connect($host,$user,$pwd); if(!$db){ die("Connect Server Failed: " . mysql_error($db)); } if (!mysql_select_db($dbname)) { die("Select Database Failed: " . mysql_error($db)); } mysql_query("set names utf-8",$db); /** * 接下来就可以使用其它标准php mysql函数操作进行数据库操作 */

index.php整体代码如下:

<?php/** * wechat php test *///define your tokendefine("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; } }}?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章