php微信公眾號開發(fā)之翻頁查詢
更新時間:2018年10月20日 10:50:49 作者:dq_095
這篇文章主要為大家詳細介紹了php微信公眾號開發(fā)之翻頁查詢功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了php微信公眾號開發(fā)之翻頁查詢的具體代碼,供大家參考,具體內(nèi)容如下
注意:公眾號列表最多只能列出8列,超出會報錯
- 分頁原理
- limit 開始位置 , 條數(shù)
- (當前頁數(shù) - 1) x 每頁條數(shù) , 每頁條數(shù)
- limit ($Page - 1) * $PageSize , $PageSize
- 0 為開始位置
- mysql_num_rows 條數(shù)
- require() 與 require_once() 開始加載,錯誤停止
- include() 與 include_once() 使用加載,錯誤跳過
- ceil 進一 向上取整
原理:
- 總共10條,每頁9條
- n條
- sum 總共幾頁 ceil ( n / 9 )
- 開始條數(shù) : (當前頁數(shù) - 1 ) x 每頁條數(shù)
- (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},經(jīng)度是{$longitude},我已經(jīng)鎖定!";
break;
case "link" ;
$contentStr = "你的鏈接有病毒吧!";
break;
case "text";
include("coon.php");
$num = "SELECT * FROM `kecheng` ";
$que=mysql_query($num);
$no=mysql_num_rows($que);//獲得條數(shù)
$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}頁,輸入頁數(shù)翻頁]]></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 ="此項功能尚未開發(fā)";
}
$msgType="text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
coon.php連接數(shù)據(jù)庫代碼如下:
<?php
//用 戶 名 : $user
//密 碼 : $pwd
//主庫域名 : $host
//從庫域名 : SAE_MYSQL_HOST_S
//端 口 : $port
//數(shù)據(jù)庫名 : $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/*接著調(diào)用mysql_connect()連接服務器*/
// 連主庫
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/*連接成功后立即調(diào)用mysql_select_db()選中需要連接的數(shù)據(jù)庫*/
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/*至此連接已完全建立,就可對當前數(shù)據(jù)庫進行相應的操作了*/
/*!??!注意,無法再通過本次連接調(diào)用mysql_select_db來切換到其它數(shù)據(jù)庫了?。?!*/
/* 需要再連接其它數(shù)據(jù)庫,請再使用mysql_connect+mysql_select_db啟動另一個連接*/
/**
* 接下來就可以使用其它標準php mysql函數(shù)操作進行數(shù)據(jù)庫操作
*/
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},經(jīng)度是{$longitude},我已經(jīng)鎖定!";
break;
case "link" ;
$contentStr = "你的鏈接有病毒吧!";
break;
case "text";
include("coon.php");
$num = "SELECT * FROM `kecheng` ";
$que=mysql_query($num);
$no=mysql_num_rows($que);//獲得條數(shù)
$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}頁,輸入頁數(shù)翻頁]]></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 ="此項功能尚未開發(fā)";
}
$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;
}
}
}
?>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
PHP url的pathinfo模式加載不同控制器的簡單實現(xiàn)
下面小編就為大家?guī)硪黄狿HP url的pathinfo模式加載不同控制器的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
前篇文章,我們復習了php的一些常用的正則表達式,本文,我們來重點介紹下php正則表達式的常用函數(shù),兩者相結(jié)合才可以完美使用哦?。?/div> 2014-08-08
WordPress中使主題支持小工具以及添加插件啟用函數(shù)
這篇文章主要介紹了WordPress中使主題支持widget以及添加插件啟用函數(shù)的方法,使WP可以使用小工具widget與通過register_activation_hook()來添加啟用插件的函數(shù),需要的朋友可以參考下2015-12-12
Thinkphp頁面跳轉(zhuǎn)設置跳轉(zhuǎn)等待時間的操作
今天小編就為大家分享一篇Thinkphp頁面跳轉(zhuǎn)設置跳轉(zhuǎn)等待時間的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10最新評論

