本文為大家分享了php微信公眾號(hào)開發(fā)之翻頁查詢的具體代碼,供大家參考,具體內(nèi)容如下
注意:公眾號(hào)列表最多只能列出8列,超出會(huì)報(bào)錯(cuò)
- 分頁原理
- limit 開始位置 , 條數(shù)
- (當(dāng)前頁數(shù) - 1) x 每頁條數(shù) , 每頁條數(shù)
- limit ($Page - 1) * $PageSize , $PageSize
- 0 為開始位置
- mysql_num_rows 條數(shù)
- require() 與 require_once() 開始加載,錯(cuò)誤停止
- include() 與 include_once() 使用加載,錯(cuò)誤跳過
- ceil 進(jìn)一 向上取整
原理:
- 總共10條,每頁9條
- n條
- sum 總共幾頁 ceil ( n / 9 )
- 開始條數(shù) : (當(dāng)前頁數(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 = "感謝你的關(guān)注\n欄目正在搭建,敬請(qǐng)期待\n回復(fù)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 ="此項(xiàng)功能尚未開發(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()連接服務(wù)器*/
// 連主庫
$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);
/*至此連接已完全建立,就可對(duì)當(dāng)前數(shù)據(jù)庫進(jìn)行相應(yīng)的操作了*/
/*?。。∽⒁猓瑹o法再通過本次連接調(diào)用mysql_select_db來切換到其它數(shù)據(jù)庫了?。?!*/
/* 需要再連接其它數(shù)據(jù)庫,請(qǐng)?jiān)偈褂胢ysql_connect+mysql_select_db啟動(dòng)另一個(gè)連接*/
/**
* 接下來就可以使用其它標(biāo)準(zhǔn)php mysql函數(shù)操作進(jìn)行數(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 = "感謝你的關(guān)注\n欄目正在搭建,敬請(qǐng)期待\n回復(fù)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 ="此項(xiàng)功能尚未開發(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;
}
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP翻頁跳轉(zhuǎn)功能實(shí)現(xiàn)方法
- PHP 長(zhǎng)文章分頁函數(shù) 帶使用方法,不會(huì)分割段落,翻頁在底部
- PHP 翻頁 實(shí)例代碼
- 一個(gè)很不錯(cuò)的PHP翻頁類
- php實(shí)現(xiàn)網(wǎng)頁上一頁下一頁翻頁過程詳解