thinkphp3.2.1 留言板小项目总结

1.数据库
CREATE DATABASE `message_center` /*!40100 DEFAULT CHARACTER SET utf8 */

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `age` int(11) NOT NULL,
  `sex` varchar(10) NOT NULL,
  `email` varchar(50) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` char(32) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8

CREATE TABLE `t_message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(50) NOT NULL,
  `content` varchar(200) NOT NULL,
  `post_time` datetime NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `t_message_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8

2.Model类
Message有点特殊,用到了关联映射
<?php
namespace Home\Model;
use Think\Model\RelationModel;
class MessageModel extends RelationModel{
    protected $_link = array(
        'user' =>array(
                'mapping_type'  =>  self::BELONGS_TO,     
                'class_name'    => 'User',    
                'foreign_key'   => 'user_id',    
                'mapping_name'  => 'user',
            ),
    );
}  
User不用去建Model。

3.界面模版
<css href="__PUBLIC__/style.css" />
    <js href="__PUBLIC__/function.js" />
    <title>用户登录</title>
</head> 
< body > 
<div class="wrap">
    <div class="content">
        <br/><br/>
        <h1>欢迎来到留言板</h1>
        <br/><br/>
        <form id="loginForm" action="{:U('Login/loginCheck')}" method="post" onsubmit="return checkLogin()">
        <table class="table">
            <tr>
                <td class="td_label">用户名:</td>
                <td class="td_input"><input class="text" type="text" name="username" id="username" size="30" /></td>
            </tr>
            <tr>
                <td class="td_label">密码:</td>
                <td class="td_input"><input class="text"  type="password" name="password" id="password" size="30" /></td>
            </tr>
            <tr>
                <td class="td_label"></td>
                <td colspan="1">
                <center>
                    <button class="cupid-blue" type="submit" id="submit_button" >登录</button>
                    <button class="cupid-blue" id="register_button" onclick="window.location.href='__MODULE__/Login/register';return false;">注册</button>
                </center>
                </td>
            </tr>
        </table>
        </form>
    </div>
</div>
    <div class="push"></div>
    <include file="Public/footer.html" />
</body>  
布局时用到了div置底,还有模版包含文件。
div.wrap {
    /*     float:center; */
    height: auto !important;
    height: 100%;
    min-height:75%;
    width720px;
    margin-top:20px;
    margin-leftauto;
    margin-rightauto;
/*     border:1px solid green; */
}
div.content {
    height:auto !important;
    height100%;
    min-height100%;
}  

div.push {
/*     height: auto !important; */
/*     height: 100%; */
     height:85px;
/*     position:absolute; */
/*     bottom: 0; */
}  
/* menu and footer */
#footer {
    color: #555;
    text-aligncenter;
    border-top1px solid #333;
    height30px;
    margin-top: -33px;
/*     position:absolute; */
/*     bottom: 0; */
    width720px;
    margin-leftauto;
    margin-rightauto;
}  

/* for pages */
.pages{
    width:720px;
/*     position:absolute; */
/*     bottom:31px; */
    text-aligncenter;
/*     border-top: 1px solid #333; */
    padding10px 0;
     margin-top:-85px;
    height:64px;
    width720px;
    margin-leftauto;
    margin-rightauto;
}  

4.控制器
├─Controller
│      │      AboutMeController.class.php   //关于作者
│      │      CommonController.class.php   //基类,用作统一登录验证
│      │      EmptyController.class.php          //空控制器,处理空操作
│      │      LoginController.class.php          //登录控制器,用作登录验证和用户注册
│      │      MessageController.class.php     //留言控制器,用作发表留言,查看留言列表
│      │      UserController.class.php          //用户控制器,用作用户登出,用户信息修改
其中基类:
class CommonController extends Controller{
    public function _initialize(){
//         dump(session('user'));
        if(!session('user')){
            $this->error("请先登录!",U("Login/login"),2);
        }
    }
} 

登录控制器
  class  LoginController  extends  Controller{
    
    public function login(){
        $this->display();
    }
    
    public function loginCheck(){
        $username = I('param.username');
        $password = md5(I('param.password'));
        $user = M('user');
        $data = $user->query("select * from t_user where binary username='".$username."' and binary password='".$password."'");
        if($data){
            session('user', $data[0]);
            $this->success("Login success!",U('Message/listAll'),2);
        }  else {
            $this->error("Username or password wrong!",'',2);
        }
    }
    
    public function register(){
        $this->display();
    }
    
    public function add(){
        $user = M('user');
        $user->create();
        $user->password = md5($_POST['password']);
        $user2 = M('user');
        $re = $user2->where("username='$user->username'")->select();
        if ($re){
            $this->error("用户已存在!");
        }else{
            if($user->add()){
                $this->success("注册成功!",U('Login/login'),2);
            }else{
                $this->error("注册失败!!");
            }
        }
    }
    
}

分页方法
/**
 * TODO 基础分页的相同代码封装,使前台的代码更少
 * @param $count 要分页的总记录数
 * @param int $pagesize 每页查询条数
 * @param int $rollPage 分页栏每页显示的页数
 * @return \Think\Page
 */
function getpage($count, $pagesize = 10, $rollPage=11) {
    $p = new Think\Page($count, $pagesize);
     $p->rollPage=$rollPage;
    $p->setConfig('header''<span class="rows">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</span>');
    $p->setConfig('prev''上一页');
    $p->setConfig('next''下一页');
    $p->setConfig('last''末页');
    $p->setConfig('first''首页');
    $p->setConfig('theme''%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
    $p->lastSuffix = false;//最后一页不显示为总页数
    return $p;
}  
留言列表时,使用分页方法
public function listAll() {
        $m = D('message');
        $count = $m->count();
        $p = getpage($count,5,6);
        $list = $m->order('id')->relation(true)->limit($p->firstRow, $p->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->display('listAll');
    }  

4.目录结构

D:\TOOLS\WAMP\WWW\MESSAGECENTER
│  .htaccess
│  index.php
│      
├─Application
│  └─Home
│      │  index.htm
│      │  index.html
│      │  
│      ├─Common
│      │      index.htm
│      │      index.html
│      │      
│      ├─Conf
│      │      config.php
│      │      index.htm
│      │      index.html
│      │      
│      ├─Controller
│      │      AboutMeController.class.php
│      │      CommonController.class.php
│      │      EmptyController.class.php
│      │      LoginController.class.php
│      │      MessageController.class.php
│      │      UserController.class.php
│      │      
│      ├─Model
│      │      index.htm
│      │      index.html
│      │      MessageModel.class.php
│      │      
│      └─View
│              AboutMe_index.html
│              Login_login.html
│              Login_register.html
│              Message_add.html
│              Message_listAll.html
│              User_modify.html
│              
├─Common
│  │  index.htm
│  │  index.html
│  │  
│  ├─Common
│  │      function.php
│  │      index.htm
│  │      index.html
│  │      
│  └─Conf
│          config.php
│          index.htm
│          index.html
│          
├─Public
│      background.jpg
│      footer.html
│      front.jpg
│      function.js
│      menu.html
│      style.css
│      
├─Runtime
│  │  index.htm
│  │  index.html
│  │  
│  ├─Cache
│  │  └─Home         
│  ├─Data
│  │      index.htm
│  │      index.html
│  │      
│  ├─Logs
│  │      15_05_11.log
│  │      15_05_12.log
│  │      15_05_13.log
│  │      index.htm
│  │      index.html
│  │      
│  └─Temp
│          index.htm
│          index.html
└─ThinkPHP










附件列表

 

转载于:https://www.cnblogs.com/homer3000/p/4501722.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值