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

<?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 .= "<li><spanid']})'>删除</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); } }

目录结构

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

sql语句

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/595dae8475188d8466ef94d0f65e1a01.html