thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器示例_第1頁(yè)
thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器示例_第2頁(yè)
thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器示例_第3頁(yè)
thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器示例_第4頁(yè)
thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器示例_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器示例本文實(shí)例講述了thinkPHP框架實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器。分享給大家供大家參考,具體如下:

HTML部分文件名index.html

!DOCTYPEhtml

htmllang="en"

head

metacharset="UTF-8"

title計(jì)算器/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請(qǐng)輸入你的手機(jī)號(hào)開(kāi)始使用/span

brbr

inputid="myphone"type="text"

inputid="use"type="button"value="使用"

brbr

/form

/div

div

div

span計(jì)算器/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當(dāng)前結(jié)果:/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

//計(jì)算屏幕寬高

varw_width=$(window).width();

varw_height=$(window).height();

varoperator=0;//運(yùn)算符號(hào)

varchange=0;//屬于運(yùn)算符后需要清空上一數(shù)值

varnum1=0;//元算的第一個(gè)數(shù)據(jù)

varnum2=0;//運(yùn)算的第二個(gè)數(shù)據(jù)

varsum=0;//運(yùn)算結(jié)果

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ù)到后臺(tái)

functionsendMessage(){

//手機(jī)號(hào)

varmyphone=$("#myphone").val();

//計(jì)算器

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();

//獲取結(jié)果

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));

//點(diǎn)擊登錄顯示輸入

$(".login").click(function(){

setCenter($(".register").show());

$(this).hide();

//點(diǎn)擊使用顯示計(jì)算器

$("#use").click(function(){

if(testReg('phone',$("#myphone").val())){

setCenter($(".calculator").show());

$(".register").hide();

getResultByPhone()

}else{

alert("你輸?shù)氖謾C(jī)格式不對(duì)");

returnfalse;

$(".order").click(function(){//點(diǎn)擊數(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(){//點(diǎn)擊運(yùn)算符號(hào)觸發(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(){//點(diǎn)擊等號(hào)

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)建一個(gè)Mysqli類繼承接口實(shí)現(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;

//設(shè)計(jì)表

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){

配置項(xiàng)文件名config.php

return[

'db'=[

'host'='127.0.0.1',

'user'='root',

'pwd'='root',

'db'='cal'

'author'=[

'adminuser'='mao',

操作計(jì)算器文件名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);

目錄結(jié)構(gòu)

sql語(yǔ)句

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*/;

--資料庫(kù):`cal`

----------------------------------------------------------

--表的結(jié)構(gòu)`calculate`

CREATETABLEIFNOTEXISTS`calculate`(

`id`int(10)NOTNULLAUTO_INCREMENTCOMMENT'序號(hào)主鍵自增',

`user_phone`varchar(100)NOTNULLCOMMENT'用戶手機(jī)號(hào)',

`num1`varchar(100)NOTNULLCOMMENT'第一個(gè)數(shù)字',

`num2`varchar(100)NOTNULLCOMMENT'第二個(gè)數(shù)字',

`option`varchar(10)NOTNULLCOMMENT'加減乘除選項(xiàng)',

`result`varchar(100)NOTNULLCOMMENT'結(jié)果',

`status`int(10)NOTNULLCOMMENT'狀態(tài)-1刪除0禁用1正常',

`admin_user`varchar(100)NOTNULLCOMMENT'管理員',

PRIMARYKEY(`id`)

)ENGINE=MyISAMDEFAULTCHARSET=utf8COMMENT='計(jì)算表'AUTO_INCREMENT=40;

--轉(zhuǎn)存資料表中的資料`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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論