這篇文章主要為大家詳細介紹了使用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); } } }) }); })
admin.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; ?>