thinkPHP框架实现的简单计算器示例

前端之家收集整理的这篇文章主要介绍了thinkPHP框架实现的简单计算器示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了thinkPHP框架实现的简单计算器。分享给大家供大家参考,具体如下:

HTML部分 文件名 index.html

<Meta charset="UTF-8">

计算器

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: 1px solid black;

height: 400px;

width: 320px;

float: left;

}

.import{

height: 20px;

width: 180px;

margin-top: 50px;

margin-left: 50px;

}

.snap{

margin-left: 50px;

margin-top: -30px;

}

.snap input{

height: 30px;

width: 30px;

}

.result{

border: 1px solid black;

height: 400px;

width: 320px;

float: left;

margin-left: 50px;

}

.brand{

position: relative;

top: 50px;

left: 90px;

}

JS部分 文件名 index.js

//计算屏幕宽高

var w_width = $(window).width();

var w_height = $(window).height();

var operator = 0;//运算符号

var change = 0;//属于运算符后需要清空上一数值

var num1 = 0;//元算的第一个数据

var num2 = 0;//运算的第二个数据

var sum = 0;//运算结果

//居中

function setCenter(obj){

var this_width = $(obj).width();

var this_height = $(obj).height();

var this_left = parseInt((w_width-this_width)/2);

var this_height = parseInt((w_height-this_height)/2);

$(obj).css({left:this_left,top:this_height});

}

//正则

function testReg() {

//定义参数

var regType = arguments[0]?arguments[0]:'phone';

var myString = arguments[1]?arguments[1]:false;

var regArray = new Array();

regArray['phone'] = /^1[3|4|5|7|8]d{9}$/;

regArray['email'] = /w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/;//邮箱 *代表{0,}+代表{1,}?代表{0,1}

//判断

if (myString){

return regArray[regType].test(myString);

}

return false;

}

//发送数据到后台

function sendMessage() {

//手机号

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

//计算器

var myUrl = '/app/base.PHP';

var myData = {num1:num1,num2:num2,cal_option:operator,cal_result:sum,myphone:myphone,type:'add'};

$.post(myUrl,myData,function (msg) {

//TODO

},'json')

getResultByPhone();

}

//获取结果

function getResultByPhone() {

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

var myUrl = '/app/base.PHP';

var myData = {myphone:myphone,type:'getResult'};

$.post(myUrl,function (msg) {

//TODO

$("#cal_result").html(msg);

},'text')

}

//获取数据

function deleteHistory(id) {

var myUrl = '/app/base.PHP';

var MyData = {id:id,type:'delete'};

$.post(myUrl,MyData,'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 ("你输的手机格式不对");

return false;

}

})

$(".order").click(function () {//点击数字

var num = $(this).val();

var oldValue = $(".import").val();

if (change ==1){

oldValue = "0";

change = 0;

}

var newValue = "";

if (num == -1){

oldValue = parseFloat(oldValue);

newValue = oldValue * -1;

}else if (num == "."){

if (oldValue.indexOf('.') == -1)

newValue = oldValue + ".";

else

newValue = oldValue;

}else {

if (oldValue == 0 && oldValue.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;

}

var value = $(".import").val();

if (value.length == 1){

$(".import").val("0");

}else {

value = value.substr(0,value.length - 1);

$(".import").val(value);

}

});

$(".operator").click(function() {//点击运算符号触发事件

change = 1;

operator = $(this).val();

var value = $(".import").val();

var dianIndex = value.indexOf(".");

if (dianIndex == value.length) {

value = value.substr(0,value.length - 1);

}

num1 = parseFloat(value);

});

$("#equal").click(function () {//点击等号

var value = $(".import").val();

var dianIndex = value.indexOf(".");

if (dianIndex == value.length){

value = value.substr(0,value.length - 1);

}

var equal = $(this).val();

num2 = parseFloat(value);

if (operator == "+"){

sum = num1 + num2;

}else if (operator == "-"){

sum = num1 - num2;

}else if (operator == "*"){

sum = num1 * num2;

}else if (operator == "/"){

sum = num1 / num2;

}else if (operator == "" || num1 ==0 || num2 == 0){

sum = num1 +num2;

}

var re = /^[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

PHP

namespace mao;

interface IDB{

public function insert($data);

public function update($data);

public function select($data);

public function del($data);

}

创建一个MysqLi类继承接口实现增删改查

文件MysqLi.clsaa.PHP

PHP

namespace mao;

include "IDB.PHP";

class MysqLi implements IDB{

private $conn = null;

private $table = null;

private $sysConfig = array(

'host' => '',

'user' => '',

'pwd' => '',

'db' => ''

);

private static $_instance = null;

private function __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();

}

}

}

public static function getInstance($config){

if (is_null(self::$_instance)){

self::$_instance = new self($config);

}

return self::$_instance;

}

//设计表

public function table($table){

$this->table = $table;

return $this;

}

//查询

private function changeCondition($condition){

$where_array = array();

foreach($condition as $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;

}

public function select($condition){

$where = $this->changeCondition($condition);

$sql = "select * from $this->table where ".$where."order by id desc limit 10";

$res = $this->conn->query($sql);

$ret = array();

while ($row = $res->fetch_assoc()){

$ret[] = $row;

}

return $ret;

}

public function insert($data){

$sql = "insert into `{$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);

}

public function update($id){

$sql = "UPDATE `{$this->table}` SET `status` = '-1' WHERE `id` ={$id}";

$this->conn->query($sql);

}

public function del($condition){

}

}

配置项 文件名 config.PHP

PHP

return [

'db'=>[

'host' => '127.0.0.1',

'user' => 'root',

'pwd' => 'root',

'db' => 'cal'

],

'author' =>[

'adminuser' => 'mao',

]

];

操作计算器 文件名 base.PHP

PHP

namespace mao;

define("ROOT_PATH",dirname(dirname(__FILE__)));

$config = include ROOT_PATH."/lib/config/config.PHP";

include ROOT_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 ($result as $k => $v){

$result_string .= "

  • 删除{$v['num1']} {$v['option']} {$v['num2']} = {$v['result']}
  • ";

    }

    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语句

    SET sql_MODE="NO_AUTO_VALUE_ON_ZERO";

    SET time_zone = "+00:00";

    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

    /*!40101 SET NAMES utf8 */;

    --

    -- ?料?? `cal`

    --

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

    --

    -- 表的??? `calculate`

    --

    CREATE TABLE IF NOT EXISTS `calculate` (

    `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '序号 主键 自增',

    `user_phone` varchar(100) NOT NULL COMMENT '用户手机号',

    `num1` varchar(100) NOT NULL COMMENT '第一个数字',

    `num2` varchar(100) NOT NULL COMMENT '第二个数字',

    `option` varchar(10) NOT NULL COMMENT '加减乘除选项',

    `result` varchar(100) NOT NULL COMMENT '结果',

    `status` int(10) NOT NULL COMMENT '状态-1 删除 0 禁用 1 正常',

    `admin_user` varchar(100) NOT NULL COMMENT '管理员',

    PRIMARY KEY (`id`)

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='计算表' AUTO_INCREMENT=40 ;

    --

    -- ?存?料表中的?料 `calculate`

    --

    INSERT INTO `calculate` (`id`,`admin_user`) VALUES

    (1,'15727228076','',''),

    (2,'7','6','+','13',-1,'jingshan'),

    (3,'13880425377','9','15',

    (4,'8',

    (5,'*','72',

    (6,1,

    (7,'89','96',

    (8,'67','75',

    (9,'2',

    (10,'78','85',

    (11,'12','108',

    (12,'23','-','16',

    (13,'65',

    (14,'34','41',

    (15,'/','9.75',

    (16,'81',

    (17,'0','mao'),

    (18,

    (19,

    (20,

    (21,

    (22,'62',

    (23,

    (24,

    (25,

    (26,'4',

    (27,

    (28,

    (29,'108.00',

    (30,'15.00',

    (31,'9.00',

    (32,'29','203.00',

    (33,'95','11.88',

    (34,'98','6566.00',

    (35,'22','13.00',

    (36,'45','5.00',

    (37,'555','777','1332.00',

    (38,'89.00',

    (39,'mao');

    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

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

    更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

    希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

    原文链接:https://www.f2er.com/thinkphp/60557.html

    猜你在找的ThinkPHP相关文章