入口文件 文件名 index.php
<?php
namespace mao;
include "jsPHP/jsmvc.php";
$app = new JSMVC(true);
$app->init();
配置文件 文件名 config.php
<?php
return array(
'dbconfig'=>array(
'host'=>'127.0.0.1',
'user'=>'root',
'pwd'=>'root',
'db'=>'jsmvc'
),
'viewconfig'=>array()
);
控制器 文件名 indexController.class.php
<?php
namespace mao;
class IndexController{
public function index(){
// 测试插入数据
$dbuser = M('User');// 实例化 UserModel 类
$dbuser->add_user();// 插入数据
$user = $dbuser->getList();// 获取全部的用户信息
$news = M('News')->getList();// 获取全部的新闻信息
// 整理数据
$data = array(
'user'=>$user,
'news'=>$news
);
// 显示数据
VIEW::assign($data);
VIEW::display('Index/index.html');
}
}
模型 文件名NewsModel.class.php
<?php
namespace mao;
class NewsModel extends DB{
//获取列表的方法
private $table = 'js_news';
public function getList($condition=array()){
//执行多表联合查询
return self::select($this->table,$condition);
}
}
模型 文件名 UserModel.class.php
<?php
namespace mao;
class UserModel extends DB{
//获取列表的方法
private $table = 'js_user';
public function getList($condition=array()){
//执行多表联合查询
return self::select($this->table,$condition);
}
public function add_user(){
$data = array('username'=>'jingshan33','pwd'=>'123123');
self::add($this->table,$data);
}
}
视图 文件名index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
测试二维数组循环
{foreach item=v key=k from=$user}
<li>ID:{$v.id}用户名:{$v.username}密码:{$v.pwd}</li>
{/foreach}
<table>
{foreach item=v key=k from=$news}
<tr>
<td>标题:{$v.title}内容:{$v.content}</td>
</tr>
{/foreach}
</table>
</body>
</html>
视图 文件名login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{$test}
</body>
</html>
默认配置项 文件名config.php
<?php
return array(
'dbconfig'=>array(
'host'=>'127.0.0.1',
'user'=>'root',
'pwd'=>'',
'db'=>''
),
'viewconfig'=>array()
);
文件引入配置 文件名 includelist.php
<?php
return array(
"/core/DB.class.php",
"/core/View.class.php",
"/db/JsMysqli.class.php",
"/view/smarty/Smarty.class.php"
);
加载配置项 文件名 helper.php
<?php
//加载配置
function load(){//循环加载全部文件
$config = include ROOT_PATH."/jsPHP/config/includelist.php";
foreach ($config as $k => $v){
include LIB_PATH.$v;
}
}
/**
* @param $model
* @return mixed
* 第一:包含 UserModel 文件
* 第二:new 一下
* url/index.php?contoller=index&method=index
* route 找到对应位置, $obj->$method
*/
function M($model){
include ROOT_PATH.'/application/Model/'.$model.'Model.class.php';
$class = '\\mao\\'.$model.'Model';
return new $class();// 返回了一个实例化 不需要在此初始化了
}
库文件 文件名 DB.class.php
<?php
namespace mao;
class DB{
public static $db = '';
public static function init($dbtype,$config){
$dbtype = '\\mao\\'.$dbtype;
self::$db = $dbtype::getInstance($config);
}
public static function select($table,$condition){
return self::$db->table($table)->select($condition);
}
public static function add($table,$data){
self::$db->table($table)->add($data);
}
public static function update($table,$data,$id){
self::$db->table($table)->update($data,$id);
}
public static function del($table,$id){
self::$db->table($table)->del($id);
}
}
库文件 文件名 View.class.php
<?php
//此类 对smarty进行处理 调用或初始化
//总分 写法
namespace mao;
class View{
public static $view;
//初始化函数
public static function init($viewtype,$config = []){
$class = '\\'.$viewtype;
self::$view = new $class;
//配置 模板目录
self::$view->setTemplateDir("./application/templates/");
//配置 编译后的目录
self::$view->setCompileDir("./runtime/template_c/");
//配置 配置文件目录
self::$view->setConfigDir("./runtime/configs/");
//配置缓存文件
self::$view->setCacheDir("./runtime/cache/");
//配置通用标签
foreach ($config as $key => $value){
self::$view->$key = $value;
}
}
//assign 循环赋值
public static function assign($data){
foreach ($data as $key => $value){
self::$view->assign($key,$value);
}
}
//display 显示对应的模板
public static function display($template){
//处理模板线上
self::$view->display($template);
}
}
对数据库的操作 文件名 JsMysqli.class.php
<?php
namespace mao;
class JsMysqli{
private $table = '';
private static $_instance = null;
private $conn = null;
private $sysConfig = array(
'host' => '',
'user' => '',
'pwd' => '',
'db' => ''
);
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']);
$this->conn->set_charset('utf8');
}
}
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;
}
public function changeCondition($condition){
$where_array = array();
foreach ($condition as $k => $v){
$temp = '';
if (is_array($v)){
if ($v[0] == 'like'){
$temp = "$k $v[0] '%$v[1]%'";
}else{
$temp = "$k $v[0] '$v[1]'";
}
}
if (is_string($v)){
$temp = "$k = '$v'";
}
$where_array[] = $temp;
}
$where = implode(' AND ',$where_array);
return $where?$where:1;
}
public function select($condition){
$where = $this->changeCondition($condition);
$sql = "select * from $this->table where $where";
$result = $this->conn->query($sql);
$ret = array();
while ($row = $result->fetch_assoc()){
$ret[] = $row;
}
return $ret;
}
public function add($data){
$fields_array = array();
$values_array = array();
foreach ($data as $k => $v){
$fields_array[] = "$k";
$values_array[] = "'$v'";
}
$fields = implode(',',$fields_array);
$values = implode(',',$values_array);
$sql = "insert into $this->table($fields) values($values)";
$this->conn->query($sql) or die(mysqli_error());
}
public function del($id){
$primaryKey = 'id';
$sql = "delete from `$this->table` where `$primaryKey` = '$id'";
$this->conn->query($sql);
}
public function update($data,$id){
$str_array = array();
foreach ($data as $k => $v){
$str_array[] = "$k = '$v'";
}
$str = implode(',',$str_array);
$primaryKey = 'id';
$sql = "update `$this->table` set $str where $primaryKey = $id";
$this->conn->query($sql) or die(mysqli_error());
}
}
目录结构
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 */;
--
-- 資料庫: `jsmvc`
--
-- --------------------------------------------------------
--
-- 表的結構 `js_news`
--
CREATE TABLE IF NOT EXISTS `js_news` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号 自增',
`title` varchar(200) DEFAULT NULL COMMENT '标题',
`uid` int(11) DEFAULT NULL COMMENT '作者',
`content` text COMMENT '内容',
`from` varchar(200) DEFAULT NULL COMMENT '出处',
`author` varchar(200) DEFAULT NULL COMMENT '作者',
`dateline` int(10) DEFAULT NULL COMMENT '写作时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--
-- 轉存資料表中的資料 `js_news`
--
INSERT INTO `js_news` (`id`, `title`, `uid`, `content`, `from`, `author`, `dateline`) VALUES
(3, '1', 1, '2555', '4', '3', 1478608506);
-- --------------------------------------------------------
--
-- 表的結構 `js_user`
--
CREATE TABLE IF NOT EXISTS `js_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
`username` varchar(200) NOT NULL COMMENT '用户名',
`pwd` varchar(200) NOT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='景山MVC显示用户表' AUTO_INCREMENT=148 ;
--
-- 轉存資料表中的資料 `js_user`
--
INSERT INTO `js_user` (`id`, `username`, `pwd`) VALUES
(1, 'admin', 'admin'),
(2, 'jingshan33', '123123'),
(3, 'jingshan33', '123123'),
(4, 'jingshan33', '123123'),
(5, 'jingshan33', '123123'),
(6, 'jingshan33', '123123'),
(7, 'jingshan33', '123123'),
(8, 'jingshan33', '123123'),
(9, 'jingshan33', '123123'),
(10, 'jingshan33', '123123'),
(11, 'jingshan33', '123123'),
(12, 'jingshan33', '123123'),
(13, 'jingshan33', '123123'),
(14, 'jingshan33', '123123'),
(15, 'jingshan33', '123123'),
(16, 'jingshan33', '123123'),
(17, 'maomaomao', '123456'),
(18, 'jingshan33', '123123'),
(19, 'jingshan33', '123123'),
(20, 'jingshan33', '123123'),
(21, 'jingshan33', '123123'),
(22, 'jingshan33', '123123'),
(23, 'jingshan33', '123123'),
(24, 'jingshan33', '123123'),
(25, 'jingshan33', '123123'),
(26, 'jingshan33', '123123'),
(27, 'jingshan33', '123123'),
(28, 'jingshan33', '123123'),
(29, 'jingshan33', '123123'),
(30, 'jingshan33', '123123'),
(31, 'jingshan33', '123123'),
(32, 'jingshan33', '123123'),
(33, 'jingshan33', '123123'),
(34, 'jingshan33', '123123'),
(35, 'jingshan33', '123123'),
(36, 'jingshan33', '123123'),
(37, 'jingshan33', '123123'),
(38, 'jingshan33', '123123'),
(39, 'jingshan33', '123123'),
(40, 'jingshan33', '123123'),
(41, 'jingshan33', '123123'),
(42, 'jingshan33', '123123'),
(43, 'jingshan33', '123123'),
(44, 'jingshan33', '123123'),
(45, 'jingshan33', '123123'),
(46, 'jingshan33', '123123'),
(47, 'jingshan33', '123123'),
(48, 'jingshan33', '123123'),
(49, 'jingshan33', '123123'),
(50, 'jingshan33', '123123'),
(51, 'jingshan33', '123123'),
(52, 'jingshan33', '123123'),
(53, 'jingshan33', '123123'),
(54, 'jingshan33', '123123'),
(55, 'jingshan33', '123123'),
(56, 'jingshan33', '123123'),
(57, 'jingshan33', '123123'),
(58, 'jingshan33', '123123'),
(59, 'jingshan33', '123123'),
(60, 'jingshan33', '123123'),
(61, 'jingshan33', '123123'),
(62, 'jingshan33', '123123'),
(63, 'jingshan33', '123123'),
(64, 'jingshan33', '123123'),
(65, 'jingshan33', '123123'),
(66, 'jingshan33', '123123'),
(67, 'jingshan33', '123123'),
(68, 'jingshan33', '123123'),
(69, 'jingshan33', '123123'),
(70, 'jingshan33', '123123'),
(71, 'jingshan33', '123123'),
(72, 'jingshan33', '123123'),
(73, 'jingshan33', '123123'),
(74, 'jingshan33', '123123'),
(75, 'jingshan33', '123123'),
(76, 'jingshan33', '123123'),
(77, 'jingshan33', '123123'),
(78, 'jingshan33', '123123'),
(79, 'jingshan33', '123123'),
(80, 'jingshan33', '123123'),
(81, 'jingshan33', '123123'),
(82, 'jingshan33', '123123'),
(83, 'jingshan33', '123123'),
(84, 'jingshan33', '123123'),
(85, 'jingshan33', '123123'),
(86, 'jingshan33', '123123'),
(87, 'jingshan33', '123123'),
(88, 'jingshan33', '123123'),
(89, 'jingshan33', '123123'),
(90, 'jingshan33', '123123'),
(91, 'jingshan33', '123123'),
(92, 'jingshan33', '123123'),
(93, 'jingshan33', '123123'),
(94, 'jingshan33', '123123'),
(95, 'jingshan33', '123123'),
(96, 'jingshan33', '123123'),
(97, 'jingshan33', '123123'),
(98, 'jingshan33', '123123'),
(99, 'jingshan33', '123123'),
(100, 'jingshan33', '123123'),
(101, 'jingshan33', '123123'),
(102, 'jingshan33', '123123'),
(103, 'jingshan33', '123123'),
(104, 'jingshan33', '123123'),
(105, 'jingshan33', '123123'),
(106, 'jingshan33', '123123'),
(107, 'jingshan33', '123123'),
(108, 'jingshan33', '123123'),
(109, 'jingshan33', '123123'),
(110, 'jingshan33', '123123'),
(111, 'jingshan33', '123123'),
(112, 'jingshan33', '123123'),
(113, 'jingshan33', '123123'),
(114, 'jingshan33', '123123'),
(115, 'jingshan33', '123123'),
(116, 'jingshan33', '123123'),
(117, 'jingshan33', '123123'),
(118, 'jingshan33', '123123'),
(119, 'jingshan33', '123123'),
(120, 'jingshan33', '123123'),
(121, 'jingshan33', '123123'),
(122, 'jingshan33', '123123'),
(123, 'jingshan33', '123123'),
(124, 'jingshan33', '123123'),
(125, 'jingshan33', '123123'),
(126, 'jingshan33', '123123'),
(127, 'jingshan33', '123123'),
(128, 'jingshan33', '123123'),
(129, 'jingshan33', '123123'),
(130, 'jingshan33', '123123'),
(131, 'jingshan33', '123123'),
(132, 'jingshan33', '123123'),
(133, 'jingshan33', '123123'),
(134, 'jingshan33', '123123'),
(135, 'jingshan33', '123123'),
(136, 'jingshan33', '123123'),
(137, 'jingshan33', '123123'),
(138, 'jingshan33', '123123'),
(139, 'jingshan33', '123123'),
(140, 'jingshan33', '123123'),
(141, 'jingshan33', '123123'),
(142, 'jingshan33', '123123'),
(143, 'jingshan33', '123123'),
(144, 'jingshan33', '123123'),
(145, 'jingshan33', '123123'),
(146, 'jingshan33', '123123'),
(147, 'jingshan33', '123123');
/*!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 */;