




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第thinkPHP框架實現(xiàn)的簡單計算器示例本文實例講述了thinkPHP框架實現(xiàn)的簡單計算器。分享給大家供大家參考,具體如下:
HTML部分文件名index.html
!DOCTYPEhtml
htmllang="en"
head
metacharset="UTF-8"
title計算器/title
scripttype="text/javascript"src="public/js/jquery-2.2.3.js"/script
linkrel="stylesheet"href="public/css/index.css"rel="externalnofollow"
/head
body
div
span登錄/span
/div
div
formaction=""
span請輸入你的手機號開始使用/span
brbr
inputid="myphone"type="text"
inputid="use"type="button"value="使用"
brbr
/form
/div
div
div
span計算器/span
brbr
inputtype="text"
brbr
formaction=""
brbr
inputid="one"type="button"value="1"
inputid="two"type="button"value="2"
inputid="three"type="button"value="3"
inputid="four"type="button"value="4"
inputid="five"type="button"value="5"
brbr
inputid="six"type="button"value="6"
inputid="seven"type="button"value="7"
inputid="eight"type="button"value="8"
inputid="nine"type="button"value="9"
inputid="zero"type="button"value="0"
brbr
inputid="plus"type="button"value="+"
inputid="add"type="button"value="-"
inputid="mul"type="button"value="*"
inputid="sub"type="button"value="/"
inputclass=""id="equal"type="button"value="="
brbr
inputtype="button"value="."
inputid="backspace"type="button"value="←"
inputid="clear"type="button"value="c"
inputtype="button"value="CE"
inputtype="button"value="MC"
/form
/div
div
div
span當前結果:/spanspanid="current_results"/span
/div
brbr
span歷史記錄:/span
ulid="cal_result"
lispan刪除/span/li
/ul
/div
/div
/body
scripttype="text/javascript"src="public/js/index.js"
/script
/html
CSS樣式文件名index.css
.login{/*登錄*/
height:30px;
width:100px;
background-color:#00a2d4;
text-align:center;
cursor:pointer;
padding-top:10px;
position:fixed;
.register{
display:none;
position:fixed;
.calculator{
display:none;
position:fixed;
.counter{
border:1pxsolidblack;
height:400px;
width:320px;
float:left;
.import{
height:20px;
width:180px;
margin-top:50px;
margin-left:50px;
.snap{
margin-left:50px;
margin-top:-30px;
.snapinput{
height:30px;
width:30px;
.result{
border:1pxsolidblack;
height:400px;
width:320px;
float:left;
margin-left:50px;
.brand{
position:relative;
top:50px;
left:90px;
JS部分文件名index.js
//計算屏幕寬高
varw_width=$(window).width();
varw_height=$(window).height();
varoperator=0;//運算符號
varchange=0;//屬于運算符后需要清空上一數(shù)值
varnum1=0;//元算的第一個數(shù)據(jù)
varnum2=0;//運算的第二個數(shù)據(jù)
varsum=0;//運算結果
functionsetCenter(obj){
varthis_width=$(obj).width();
varthis_height=$(obj).height();
varthis_left=parseInt((w_width-this_width)/2);
varthis_height=parseInt((w_height-this_height)/2);
$(obj).css({left:this_left,top:this_height});
functiontestReg(){
//定義參數(shù)
varregType=arguments[0]arguments[0]:'phone';
varmyString=arguments[1]arguments[1]:false;
varregArray=newArray();
regArray['phone']=/^1[3|4|5|7|8]\d{9}$/;
regArray['email']=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;//郵箱*代表{0,}+代表{1,}代表{0,1}
//判斷
if(myString){
returnregArray[regType].test(myString);
returnfalse;
//發(fā)送數(shù)據(jù)到后臺
functionsendMessage(){
//手機號
varmyphone=$("#myphone").val();
//計算器
varmyUrl='/app/base.php';
varmyData={num1:num1,num2:num2,cal_option:operator,cal_result:sum,myphone:myphone,type:'add'};
$.post(myUrl,myData,function(msg){
//TODO
},'json')
getResultByPhone();
//獲取結果
functiongetResultByPhone(){
varmyphone=$("#myphone").val();
varmyUrl='/app/base.php';
varmyData={myphone:myphone,type:'getResult'};
$.post(myUrl,myData,function(msg){
//TODO
$("#cal_result").html(msg);
},'text')
//獲取數(shù)據(jù)
functiondeleteHistory(id){
varmyUrl='/app/base.php';
varMyData={id:id,type:'delete'};
$.post(myUrl,MyData,function(msg){
//TODO
},'json')
getResultByPhone();
$(function(){
//登錄居中
setCenter($(".login").show(8000));
//點擊登錄顯示輸入
$(".login").click(function(){
setCenter($(".register").show());
$(this).hide();
//點擊使用顯示計算器
$("#use").click(function(){
if(testReg('phone',$("#myphone").val())){
setCenter($(".calculator").show());
$(".register").hide();
getResultByPhone()
}else{
alert("你輸?shù)氖謾C格式不對");
returnfalse;
$(".order").click(function(){//點擊數(shù)字
varnum=$(this).val();
varoldValue=$(".import").val();
if(change==1){
oldValue="0";
change=0;
varnewValue="";
if(num==-1){
oldValue=parseFloat(oldValue);
newValue=oldValue*-1;
}elseif(num=="."){
if(oldValue.indexOf('.')==-1)
newValue=oldValue+".";
else
newValue=oldValue;
}else{
if(oldValue==0oldValue.lastIndexOf('.')==-1){
newValue=num;
}else{
newValue=oldValue+num;
$(".import").val(newValue);
$("#clear").click(function(){//清除
$(".import").val("0");
operator=0;
change=0;
num1=0;
num2=0;
$("#backspace").click(function(){//退格
if(change==1){
operator=0;
change=0;
varvalue=$(".import").val();
if(value.length==1){
$(".import").val("0");
}else{
value=value.substr(0,value.length-1);
$(".import").val(value);
$(".operator").click(function(){//點擊運算符號觸發(fā)事件
change=1;
operator=$(this).val();
varvalue=$(".import").val();
vardianIndex=value.indexOf(".");
if(dianIndex==value.length){
value=value.substr(0,value.length-1);
num1=parseFloat(value);
$("#equal").click(function(){//點擊等號
varvalue=$(".import").val();
vardianIndex=value.indexOf(".");
if(dianIndex==value.length){
value=value.substr(0,value.length-1);
varequal=$(this).val();
num2=parseFloat(value);
if(operator=="+"){
sum=num1+num2;
}elseif(operator=="-"){
sum=num1-num2;
}elseif(operator=="*"){
sum=num1*num2;
}elseif(operator=="/"){
sum=num1/num2;
}elseif(operator==""||num1==0||num2==0){
sum=num1+num2;
varre=/^[0-9]+.[0-9]*$/;
if(re.test(sum)){
sum=sum.toFixed(2);
$(".import").val(sum);
sendMessage();
$("#current_results").text(num1+operator+num2+equal+sum);
change=1;
operator=0;
num1=0;
num2=0;
接口文件名IDB.php
namespacemao;
interfaceIDB{
publicfunctioninsert($data);
publicfunctionupdate($data);
publicfunctionselect($data);
publicfunctiondel($data);
創(chuàng)建一個Mysqli類繼承接口實現(xiàn)增刪改查
文件名MySqli.clsaa.php
namespacemao;
include"IDB.php";
classMySqliimplementsIDB{
private$conn=null;
private$table=null;
private$sysConfig=array(
'host'='',
'user'='',
'pwd'='',
'db'=''
privatestatic$_instance=null;
privatefunction__construct($config){
if(is_array($config)){
$this-sysConfig=array_merge($this-sysConfig,$config);
$this-conn=new\Mysqli($this-sysConfig['host'],$this-sysConfig['user'],$this-sysConfig['pwd'],$this-sysConfig['db']);
if(!$this-conn){
echo"連接失敗".mysqli_error();
publicstaticfunctiongetInstance($config){
if(is_null(self::$_instance)){
self::$_instance=newself($config);
returnself::$_instance;
//設計表
publicfunctiontable($table){
$this-table=$table;
return$this;
//查詢
privatefunctionchangeCondition($condition){
$where_array=array();
foreach($conditionas$k=$v){
if(is_array($v)){
if(strtolower($v[0])=='like'){
$where_array[]=$k.''.$v[0].'\'%'.$v[1].'%'';
}else{
$where_array[]=$k.''.$v[0].'\''.$v[1].''';
if(is_string($v)){
$where_array[]=$k.'=\''.$v.''';
$where=implode('AND',$where_array);
return$where$where:1;
publicfunctionselect($condition){
$where=$this-changeCondition($condition);
$sql="select*from$this-tablewhere".$where."orderbyiddesclimit10";
$res=$this-conn-query($sql);
$ret=array();
while($row=$res-fetch_assoc()){
$ret[]=$row;
return$ret;
publicfunctioninsert($data){
$sql="insertinto`{$this-table}`(`id`,`user_phone`,`num1`,`num2`,`option`,`result`,`status`,`admin_user`)VALUES(NULL,'{$data['myphone']}','{$data['num1']}','{$data['num2']}','{$data['cal_option']}','{$data['cal_result']}','1','mao')";
$this-conn-query($sql);
publicfunctionupdate($id){
$sql="UPDATE`{$this-table}`SET`status`='-1'WHERE`id`={$id}";
$this-conn-query($sql);
publicfunctiondel($condition){
配置項文件名config.php
return[
'db'=[
'host'='127.0.0.1',
'user'='root',
'pwd'='root',
'db'='cal'
'author'=[
'adminuser'='mao',
操作計算器文件名base.php
namespacemao;
define("ROOT_PATH",dirname(dirname(__FILE__)));
$config=includeROOT_PATH."/lib/config/config.php";
includeROOT_PATH."/lib/db/MySqli.class.php";
$db=MySqli::getInstance($config['db']);
if($_POST){
//查詢
if($_POST['type']=='getResult'){
$condition=array(
'user_phone'=array('like',$_POST['myphone']),
'status'='1'
$result=$db-table('calculate')-select($condition);
$result_string='';
foreach($resultas$k=$v){
$result_string.="lispanid']})'刪除/span{$v['num1']}{$v['option']}{$v['num2']}={$v['result']}/li
echo$result_string;
//刪除
if($_POST['type']=='delete'){
$id=isset($_POST['id'])$_POST['id']:'';
$db-table('calculate')-update($id);
if($_POST['type']=='add'){
$data=$_POST;
$db-table('calculate')-insert($data);
目錄結構
sql語句
SETSQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SETtime_zone="+00:00";
/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101SET@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101SETNAMESutf8*/;
--資料庫:`cal`
----------------------------------------------------------
--表的結構`calculate`
CREATETABLEIFNOTEXISTS`calculate`(
`id`int(10)NOTNULLAUTO_INCREMENTCOMMENT'序號主鍵自增',
`user_phone`varchar(100)NOTNULLCOMMENT'用戶手機號',
`num1`varchar(100)NOTNULLCOMMENT'第一個數(shù)字',
`num2`varchar(100)NOTNULLCOMMENT'第二個數(shù)字',
`option`varchar(10)NOTNULLCOMMENT'加減乘除選項',
`result`varchar(100)NOTNULLCOMMENT'結果',
`status`int(10)NOTNULLCOMMENT'狀態(tài)-1刪除0禁用1正常',
`admin_user`varchar(100)NOTNULLCOMMENT'管理員',
PRIMARYKEY(`id`)
)ENGINE=MyISAMDEFAULTCHARSET=utf8COMMENT='計算表'AUTO_INCREMENT=40;
--轉存資料表中的資料`calculate`
INSERTINTO`calculate`(`id`,`user_phone`,`num1`,`num2`,`option`,`result`,`status`,`admin_user`)VALUES
(1,,'','','','',0,''),
(2,,'7','6','+','13',-1,'jingshan'),
(3,,'9','6','+','15',-1,'jingshan'),
(4,,'8','7','+','15',-1,'jingshan'),
(5,,'8','9','*','72',-1,'jingshan'),
(6,,'6','7','+','13',1,'jingshan'),
(7,,'89','7','+','96',-1,'jingshan'),
(8,,'67','8','+','75',1,'jingshan'),
(9,,'2','7','+','9',1,'jingshan'),
(10,,'78','7','+','85',1,'jingshan'),
(11,,'12','9','*','108',1,'jingshan'),
(12,,'23','7','-','16',1,'jingshan'),
(13,,'67','2','-','65',1,'jingshan'),
(14,,'34','7','+','41',1,'jingshan'),
(15,,'78','8','/','9.75',1,'jingshan'),
(16,,'72','9','+','81',1,'jingshan'),
(17,,'78','9','+','0',1,'mao'),
(18,,'67','9','+','0',1,'mao'),
(19,,'78','9','+','0',1,'mao'),
(20,,'78','9','+','0',1,'mao'),
(21,,'67','8','+','0',1,'mao'),
(22,,'62','8','+','0',1,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2026學年南陽市淅川縣數(shù)學三年級第一學期期末學業(yè)質量監(jiān)測試題含解析
- 2025-2026學年福建省福州市晉安區(qū)數(shù)學三上期末復習檢測模擬試題含解析
- 2025-2026學年安徽省宣城市寧國區(qū)三上數(shù)學期末質量檢測模擬試題含解析
- 2025-2026學年阿城市數(shù)學三年級第一學期期末綜合測試試題含解析
- 2024年江蘇省南京市六合區(qū)三年級數(shù)學第一學期期末監(jiān)測模擬試題含解析
- 公選《心理咨詢學派舉要》課件
- 八年級期中考試家長會課件
- 執(zhí)業(yè)藥師考試解題技巧試題及答案
- 新時代中國文化的傳播方式與試題及答案
- 執(zhí)業(yè)醫(yī)師考試強化訓練計劃試題及答案
- 工程材料封樣表
- 高中英語-Live form the Louvre教學設計學情分析教材分析課后反思
- 實驗室安全程序文件
- 醫(yī)療器械生產企業(yè)現(xiàn)場檢查表質量體系考核檢查表
- 部編版語文初一(下)期末復習:詞語成語運用檢測卷
- 《字體設計》模塊四 具象性變化設計技巧的訓練
- 年產10噸功能益生菌凍干粉的工廠設計改
- 英語老師家長會課件95908
- 盆底重建手術治療新進展
- 樹脂安全技術說明書(MSDS)
- 員工食堂廚師人員考核細則
評論
0/150
提交評論