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

技术分享图片

技术分享图片

核心代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
$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连接数据库代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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整体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?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;
    
}
  
}
}
 
?>

 

   
技术分享图片CodeIgniter项目实战 PHP CI框架完美实战视频教程 四位老师传智播客+后盾网+兄弟连等 attach_img
技术分享图片2017年全新Laravel实战视频 云之梦Laravel实战学习+项目学习课程 attach_img
技术分享图片DEDE织梦实战+迅雷看看前端开发项目+HDPHP视频 后盾网三套经典PHP开发实战课程 attach_img
技术分享图片ThinkPHP5基础视频教程 attach_img
技术分享图片Thinkphp5底层源码分析
技术分享图片独立实现ThinkPHP开发整站 前端+后台 attach_img
技术分享图片Thinkphp实战微博项目 attach_img
技术分享图片Thinkphp5.0一套博客程序开发实战
技术分享图片Laravel5实战开发在线图书商城项目 attach_img
技术分享图片Laravel框架从入门到精通【第二季】 attachment
技术分享图片特战旅Laravel实战开发商城前台
技术分享图片Laravel5.2官方文档详细讲解 attach_img
技术分享图片Laravel + AngularJS 实战开发知乎 attach_img
技术分享图片laravel5.2博客项目实战开发 attach_img
技术分享图片Laravel P2P实战教程 attachment
技术分享图片NHW的PHP+ laravel attach_img
技术分享图片PHP四大框架之Laravel 框架 attach_img
技术分享图片laravel实战微信商城及基础应用三套视频+源码 attach_img
技术分享图片Laravel5.4电商实战项目-联想商城 attach_img
技术分享图片基于laravel框架的app软件开发

相关文章