加入文章顶或踩功能 修改原程序文件
1.加入jquery.js到include目录
2.修改模版文件show.php:
引入jquery.js
<script type="text/javascript" src="include/jquery.js"></script>
引入ding.js
<script type="text/javascript" src="include/ding.js"></script>
ding.js内容:
JavaScript代码
- function ding(type, aid, showHtml) {//可自定义类型,文章ID,要显示的文本(例如:顶或踩)
- $.get('ding.php',{
- 'check' : 'checkcookies',//发送check检查session是否存在返回0或者1
- 'aid' : aid,//发送文章ID
- 'type' : type//类型()
- },function(data){
- //alert(data);
- if(data > 0){//如果大于0,则表示存在
- $("#dig" + type).html('<font color="#999999">' + showHtml + '</font>');//去掉超链接并改变颜色
- }
- });
- $("#dig" + type +" a").click(function(){//点击事件
- var n = parseInt($("#show" + type).text());//得到当前值
- $.get('ding.php',{
- 'act' : type,
- 'aid' : aid},function(data){
- });
- n += 1;//原值加1,这里直接在原值的基础上加1并显示,不重新查询数据库
- $("#dig" + type).html('<font color="#999999">' + showHtml + '</font>');//去掉超链接并改变颜色
- $("#show" + type).html(n);//显示结果数值
- });
- }
适当位置加入
xml/HTML代码
- <div class="digbox">
- <div class="dig" id="showding">$d</div>
- <div class="digaction" id="digding"><a href="###">顶一下</a></div>
- </div>
加入代码:
JavaScript代码
- <script type="text/javascript">
- $(document).ready(function(){
- ding('ding','$article[articleid]','顶一下');
- });
- </script>
3.修改数据库表articles
增加ding字段 varchar(10),默认值为0|0(原设计为顶或踩两个功能)
4.增加css样式
CSS代码
- .digbox{
- width:52px;
- height:80px;
- float:rightright;
- }
- .dig{
- width:52px;
- height:52px;
- background:url(img/dig.gif) repeat-y;
- font-size: 20px;
- color:#FFFFFF;
- line-height: 52px;
- text-align: center;
- clear: both;
- }
- .digaction{
- width:52px;
- height:20px;
- font-size: 12px;
- margin-top:5px;
- border: 1px solid #DEEAF7;
- text-align: center;
- line-height: 20px;
- color:white;
- background:url(img/btn_normal.gif);
- font-weight:bold;
- }
- .digaction a:link{
- color:white;
- text-decoration:none
- }
- .digaction a:hover{
- color:black;
- text-decoration:none;
- border: 1px solid #ADCAEC;
- display:block
- }
5. 增加PHP处理文件 ding.php 内容如下
PHP代码
- <?php
- require_once('global.php');
- require_once(SABLOG_ROOT.'include/visits.php');
- if($check == 'checkcookies'){
- session_start();
- if($_SESSION['article'.$type.$aid] != '') {//如果session不存在返回0,反之返回1
- echo 1;
- } else {
- echo 0;
- }
- }
- if (!$act && !$cid) {
- exit();
- }
- $sql = $DB->fetch_one_array("SELECT ding FROM {$db_prefix}articles WHERE articleid='$aid'");
- //print_r($sql);
- $ding = explode("|", $sql['ding']);
- $d = $ding[0];
- $c = $ding[1];
- if($act == "ding") {
- $d = $d + 1;
- $data = $d."|".$c;
- } else {
- $c = $c + 1;
- $data = $d."|".$c;
- }
- if ($data != '') {
- $DB->unbuffered_query("UPDATE {$db_prefix}articles SET ding='$data' WHERE articleid='$aid'");
- session_start();
- session_register("article".$act.$aid);
- $_SESSION["article".$act.$aid] = md5("article".$act.$aid);
- } else {
- echo "更新失败";
- }
- ?>



#1