这篇文章主要为大家详细介绍了使用PHP+layui实现每日签到及提醒功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,首次签到获得1个积分,第二次签到获得2个积分,第三次签到获得3个积分,以此类推但是签到必须每天连续积分才可以递增,如果有中断再次签到时获得积分仍然从1开始递增,实现效果如下:
数据库设计:
-- --------------------------------------------------------
-- 主机: 127.0.0.1
-- 服务器版本: 5.5.53 - MySQL Community Server (GPL)
-- 服务器操作系统: Win32
-- HeidiSQL 版本: 9.3.0.4984
-- --------------------------------------------------------
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8mb4 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-- 导出 sg 的数据库结构
CREATE DATABASE IF NOT EXISTS `sg` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `sg`;
-- 导出 表 sg.user 结构
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`count` int(11) NOT NULL,
`point` int(11) NOT NULL,
`sign_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- 正在导出表 sg.user 的数据:0 rows
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT IGNORE INTO `user` (`id`, `username`, `count`, `point`, `sign_time`) VALUES
(1, '1', 3, 3, '2018-12-03 15:19:26'),
(2, '2', 2, 2, '2018-12-03 15:03:38'),
(3, '3', 1, 1, '2018-12-03 15:56:38');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
引入jquery和layui,PS:注意需要先下载layui并使用layui.all.js
<script src="https://cdn.bootcss.com/jquery/1.12.0/jquery.min.js"></script>
<script src="./layui/layui.all.js" charset="utf-8"></script>
<link rel="stylesheet" href="./layui/css/layui.css" media="all">
index.html前端,页面布局<怎么好看看自己需要了,我只是简单的写了一个>
<center>
<input type="text" name="username">
<button>签到</button>
</br>
<span id='span'></span>
<div>
<table id="box" border="1"></table>
</div>
</center>
AJAX提交:
$(function(){
$('button').click(function(){
var username=$(':text').val();
$.ajax({
type:'post',
url:'admin.php',
data:{username:username},
dataType:'json',
success:function(res){
if(res.success==1){
layer.alert(res.info, {
icon: 1,
title: "提示"
});
var str='<tr><td>用户名</td><td>连续签到天数</td><td>总积分</td></tr>';
str+='<tr><td>'+res.msg.username+'</td><td>'+res.msg.count+'</td><td>'+res.msg.point+'</td></tr>';
$('#box').html(str);
}
}
})
});
})
PHP处理文件:
<?php
error_reporting(0);
header("content-type:text/html;charset=utf-8");
date_default_timezone_set('PRC');
$dsn = 'mysql:host=localhost;dbname=sg';
$pdo = new PDO($dsn, 'root', '123456');
$pdo->exec('set names utf8');
$username = $_POST['username'];
$sqlQuery = "select * from user where username='$username'";
$row = $pdo->query($sqlQuery)->fetch(PDO::FETCH_ASSOC);
if ($row) {
$sign_time = $row['sign_time'];
$sign_time = strtotime($sign_time);
//echo $sign_time;
$int = date('Y-m-d');
//echo $int;
$int = strtotime($int);
$ints = $int + 86400;
$int_s = $int - 86400;
//当天已签到
if ($int < $sign_time && $sign_time < $ints) {
$infor ="今天您已经签过到了,请明天再来!";//echo '您已签到';
}
//昨天未签到,积分,天数在签到修改为1
if ($sign_time < $int_s) {
$count = 1;
$point = 1;
$sign_time = date('Y-m-d H:s:i');
$sqlRow = "update user set count='$count',point='$point',sign_time='$sign_time' where username='$username'";
$res = $pdo->exec($sqlRow);
// echo '签到成功修改为1';
}
//请签到
if ($int_s < $sign_time && $sign_time < $int) {
$count = $row['count'] + 1;
$point = $row['point'] + 1;
$sign_time = date('Y-m-d H:s:i');
$sqlupdate = "update user set count='$count',point='$point',sign_time='$sign_time' where username='$username'";
$res = $pdo->exec($sqlupdate);
$infor ="签到成功!";// echo '签到成功+1';
}
} else {
$count = 1;
$point = 1;
$sign_time = date('Y-m-d H:s:i');
$sqlAdd = "insert into user values (null,'$username','$count','$point','$sign_time')";
$res = $pdo->exec($sqlAdd);
$infor ="签到成功!请记得再来哦!";// echo '恭喜你签到成功----1';
}
//////////////////////响应
$sqlEnd = "select * from user where username='$username'";
$info = $pdo->query($sqlEnd)->fetch(PDO::FETCH_ASSOC);
echo json_encode(array('success' => 1, 'info'=>$infor,'msg' => $info));
die;
?>
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!