• smarty2 简单应用 小留言板


    本软件是作为部门内员工之间留言及发送消息使用。

    系统必须通过口令验证,登录进入。方法是从数据库内取出用户姓名和口令的数据进行校验。

    用户管理的工作(比如增加,删除,修改用户)通过直接对数据库操作实现,不在本系统中实现,可以自行在数据库用户表中增加测试数据。

    系统包含四部分功能

    1 登录:验证用户名与口令,保存会话信息,进入主界面。

    2 退出:退出使用状态,清空会话信息,返回登录界面。

    3 信息查询:显示给当前登录人留的信息以及公共信息(给所有人发送)。

    4 发信息:当前登录人员用来给其他人发信息的功能。信息的内容包括:信息的编号(自动编号),发送人,信息内容,接收人,发送时间等,可以发给所有人,也可以发给某个人。

    基本结构:

    controller 数据操作逻辑控制页面
    js javascript脚本页面
    libs smarty目录
    model 与数据库交互CURD数据页面
    templates 模板目录
    templates_c 编译后模板目录
    config.ini.php 配置页
    index.php 入口文件

    数据库结构:

    View Code
     1 DROP TABLE IF EXISTS `emp`;
     2 CREATE TABLE `emp` (
     3   `emp_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
     4   `emp_name` varchar(64) NOT NULL,
     5   `emp_pwd` char(32) NOT NULL,
     6   PRIMARY KEY (`emp_id`),
     7   UNIQUE KEY `emp_name` (`emp_name`)
     8 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
     9 
    10 INSERT INTO `emp` VALUES ('1', 'admin', 'admin');
    11 
    12 DROP TABLE IF EXISTS `message`;
    13 CREATE TABLE `message` (
    14   `message_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    15   `sender` varchar(64) NOT NULL,
    16   `getter` varchar(64) NOT NULL,
    17   `sendtime` datetime NOT NULL,
    18   `content` varchar(2000) NOT NULL,
    19   PRIMARY KEY (`message_id`)
    20 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

    model文件
    EmpModel.class.php

    View Code
     1 <?php
     2     class EmpModel {
     3        public $uid;
     4        public $username;
     5        
     6        public function checkLogin($name, $pwd) {
     7             $db = new SqlHelper();
     8             $sql = "select * from emp where emp_name = '$name'" ;
     9             $re = $db->getrow($sql);
    10             if ($re) {
    11                 if ($re['emp_pwd'] == $pwd) {
    12                     $this->uid = $re['emp_id'];
    13                     $this->username = $re['emp_name'];
    14                     return TRUE;
    15                 } else {
    16                     return FALSE;
    17                 }
    18                 
    19             } else {
    20                 return FALSE;
    21             }
    22         
    23        }
    24        
    25        
    26        
    27        
    28        
    29     }
    30 ?>

    FenyePage.class.php

    View Code
      1 <?php
      2 /**
      3 * 分页类FenyePage
      4 * 
      5 * 
      6 */
      7 
      8 class FenyePage {
      9     // 分页栏每页显示的页数
     10     public $rollPage = 7;
     11     // 页数跳转时要带的参数
     12     public $parameter  ;
     13     // 默认列表每页显示行数
     14     public $listRows = 20;
     15     // 起始行数
     16     public $firstRow    ;
     17     // 分页总页面数
     18     public $totalPages  ;
     19     // 总行数
     20     protected $totalRows  ;
     21     // 当前页数
     22     public $nowPage    ;
     23     // 分页的栏的总页数
     24     protected $coolPages   ;
     25     // 分页显示定制
     26     protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
     27     // 默认分页变量名
     28     protected $varPage;
     29     //默认分页页面
     30     public $goUrl = '';
     31 
     32     /**
     33      +----------------------------------------------------------
     34      * 架构函数
     35      +----------------------------------------------------------
     36      * @access public
     37      +----------------------------------------------------------
     38      * @param array $totalRows  总的记录数
     39      * @param array $listRows  每页显示记录数
     40      * @param array $parameter  分页跳转的参数
     41      +----------------------------------------------------------
     42      */
     43     public function __construct($totalRows,$listRows='10',$parameter='', $varPage='p') {
     44         $this->totalRows = $totalRows;
     45         $this->parameter = $parameter;
     46         $this->varPage = $varPage;
     47         if(!empty($listRows)) {
     48             $this->listRows = intval($listRows);
     49         } else {
     50             $this->listRows = 10; //默认每页10条记录
     51         }
     52         $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
     53         $this->coolPages  = ceil($this->totalPages/$this->rollPage);
     54         $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
     55         if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
     56             $this->nowPage = $this->totalPages;
     57         }
     58         $this->firstRow = $this->listRows*($this->nowPage-1);
     59     }
     60 
     61     public function setConfig($name,$value) {
     62         if(isset($this->config[$name])) {
     63             $this->config[$name]    =   $value;
     64         }
     65     }
     66     
     67     //分页内容
     68     public function showContent() {
     69         
     70     }
     71 
     72     /**
     73      +----------------------------------------------------------
     74      * 分页显示输出
     75      +----------------------------------------------------------
     76      * @access public
     77      +----------------------------------------------------------
     78      */
     79     public function show() {
     80         if(!empty($this->goUrl)) {
     81             $url = $this->getGoUrl($_SERVER['REQUEST_URI']);
     82         } else {
     83             $url  =  $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : "?") . $this->parameter;
     84         }
     85         $parse = parse_url($url);
     86         $p = $this->varPage;
     87         if(isset($parse['query'])) {
     88             parse_str($parse['query'], $params);
     89             if(isset($params[$$p])) {
     90                 unset($params[$$p]);
     91                 unset($params['sub']);
     92             }
     93             
     94             $url = $parse['path'] . '?' . http_build_query($params);
     95 
     96         }
     97         $r = '<span>本次查询结果总数:<strong>' . $this->totalRows . '</strong></span>';
     98         $minpage = $this->nowPage - ceil($this->rollPage / 2);
     99         if($minpage < 1) {
    100             $minpage = 1;
    101         }
    102         //保证每页显示的页数
    103         if($this->totalPages < ($minpage + $this->rollPage)) {
    104             $minpage = $this->totalPages - $this->rollPage + 1;
    105             if($minpage < 1) {
    106                 $minpage = 1;
    107             }
    108         }
    109         if($minpage > 1) {
    110             $r .= '<a href="' . $url . '&' . $p . '=1">1..</a>';
    111         }
    112         if($this->nowPage > 1) {
    113             $r .= '<a href="' . $url . '&' . $p . '=' . ($this->nowPage - 1) . '">' . $this->config['prev'] . '</a>';
    114         }
    115         for($i = 0; $i < $this->rollPage; $i++) {
    116             if($minpage + $i == $this->nowPage) {
    117                 $r .= '<a href="' . $url . '&' . $p . '=' . ($minpage + $i) . '" class="cur">' . ($minpage + $i) . '</a>';
    118             } else {
    119                 $r .= '<a href="' . $url . '&' . $p . '=' . ($minpage + $i) . '">' . ($minpage + $i) . '</a>';
    120             }
    121         }
    122         if($this->nowPage < $this->totalPages) {
    123             $r .= '<a href="' . $url . '&' . $p . '=' . ($this->nowPage + 1) . '">' . $this->config['next'] . '</a>';
    124         }
    125         if($minpage + $this->rollPage < $this->totalPages) {
    126             $r .= '<a href="' . $url . '&' . $p . '=' . $this->totalPages . '">..' . $this->totalPages . '</a>';
    127         }
    128         $r .= '<br/>
    129         <form id="myform">请输入跳转页数
    130         <input type="text" id="pageNow" name="'.$p.'"/>
    131         <input type="button" value="提交" onclick="javascript:checkPageNow()"; />
    132         </form>';
    133         return $r;
    134     }
    135     
    136     //替换url中path最后php文件名
    137     private function getGoUrl($url) {
    138         $url  =  $url . (strpos($url, '?') ? '' : "?" . $this->parameter);
    139         $reg = '/(\/)[\w\.]*(\?)/i';
    140         $url = preg_replace($reg, '$1'.$this->goUrl.'$2', $url);
    141         return $url;
    142     }
    143 
    144     /**
    145      +----------------------------------------------------------
    146      * 分页显示输出
    147      +----------------------------------------------------------
    148      * @access public
    149      +----------------------------------------------------------
    150      */
    151     public function show2() {
    152         if(0 == $this->totalRows) return '';
    153         $p = $this->varPage;
    154         $nowCoolPage      = ceil($this->nowPage/$this->rollPage);
    155         if(!empty($this->goUrl)) {
    156             $url = $this->getGoUrl($_SERVER['REQUEST_URI']);
    157         } else {
    158             $url  =  $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : "?") . $this->parameter;
    159         }
    160         $parse = parse_url($url);
    161         if(isset($parse['query'])) {
    162             parse_str($parse['query'],$params);
    163             unset($params[$p]);
    164             $url   =  $parse['path'].'?'.http_build_query($params);
    165         }
    166         //上下翻页字符串
    167         $upRow   = $this->nowPage-1;
    168         $downRow = $this->nowPage+1;
    169         if ($upRow>0){
    170             $upPage="<a href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
    171         }else{
    172             $upPage="";
    173         }
    174 
    175         if ($downRow <= $this->totalPages){
    176             $downPage="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
    177         }else{
    178             $downPage="";
    179         }
    180         // << < > >>
    181         if($nowCoolPage == 1){
    182             $theFirst = "";
    183             $prePage = "";
    184         }else{
    185             $preRow =  $this->nowPage-$this->rollPage;
    186             $prePage = "<a href='".$url."&".$p."=$preRow' >上".$this->rollPage."页</a>";
    187             $theFirst = "<a href='".$url."&".$p."=1' >".$this->config['first']."</a>";
    188         }
    189         if($nowCoolPage == $this->coolPages){
    190             $nextPage = "";
    191             $theEnd="";
    192         }else{
    193             $nextRow = $this->nowPage+$this->rollPage;
    194             $theEndRow = $this->totalPages;
    195             $nextPage = "<a href='".$url."&".$p."=$nextRow' >下".$this->rollPage."页</a>";
    196             $theEnd = "<a href='".$url."&".$p."=$theEndRow' >".$this->config['last']."</a>";
    197         }
    198         // 1 2 3 4 5
    199         $linkPage = "";
    200         for($i=1;$i<=$this->rollPage;$i++){
    201             $page=($nowCoolPage-1)*$this->rollPage+$i;
    202             if($page!=$this->nowPage){
    203                 if($page<=$this->totalPages){
    204                     $linkPage .= "&nbsp;<a href='".$url."&".$p."=$page'>&nbsp;".$page."&nbsp;</a>";
    205                 }else{
    206                     break;
    207                 }
    208             }else{
    209                 if($this->totalPages != 1){
    210                     $linkPage .= "&nbsp;<span class='current'>".$page."</span>";
    211                 }
    212             }
    213         }
    214         $pageStr     =     str_replace(
    215             array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
    216             array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
    217         return $pageStr;
    218     }
    219 
    220 }

    MessageModel.class.php

    View Code
     1 <?php
     2     class MessageModel {
     3        
     4        //显示所有信息
     5        public function showMessageAll($loginname) {
     6            
     7            $sql = "select * from message where getter = '所有人' or getter = '$loginname'";
     8            $sqlHelper = new SqlHelper();
     9            $res = $sqlHelper->getAll($sql);
    10            $sqlHelper->my_close();
    11            return $res;
    12        }
    13        
    14        //根据分页类获取对应信息
    15        public function showMessageByPage($firstRow, $listRows, $loginname) {
    16            
    17            $sql = "select * from message where getter = '所有人' or getter = '$loginname' limit $firstRow, $listRows";
    18            $sqlHelper = new SqlHelper();
    19            $res = $sqlHelper->getAll($sql);
    20            $sqlHelper->my_close();
    21            return $res;
    22        }
    23        
    24        //添加信息
    25        public function addMessage($getter, $content) {
    26             $sender = unserialize($_SESSION['loginuser'])->username;
    27             $sql = "insert into message (sender,getter,sendtime,content) values ('$sender', '$getter',now(),'$content')";
    28             $sqlHelper = new SqlHelper();
    29             $res = $sqlHelper->query($sql);
    30             $sqlHelper->my_close();
    31             return $res;
    32        }
    33        
    34        //删除信息
    35        public function deleteMessage($id) {
    36             $getter = unserialize($_SESSION['loginuser'])->username;
    37             $sql = "delete from message where (getter = '所有人' or getter = '$getter') and message_id = '$id'";
    38             $sqlHelper = new SqlHelper();
    39             $res = $sqlHelper->query($sql);
    40             $sqlHelper->my_close();
    41             return $res;
    42        }
    43     }
    44 ?>

    SqlHelper.class.php

    View Code
      1 <?php
      2 class SqlHelper
      3 {
      4     protected $link_id;
      5     //初始化数据库连接并选择数据库和设置连接字符集
      6     public function __construct($dbhost = DBHOST, $dbuser = DBUSER, $dbpw = DBPWD, $dbname = DBNAME, $charset = 'utf-8')
      7     {
      8         if(!($this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw)))
      9         {
     10             $this->ErrorMsg("Can't pConnect MySQL Server!");
     11         }
     12         
     13         mysql_query("SET NAMES " . $charset, $this->link_id);
     14         
     15         if ($dbname)
     16         {
     17             if (@mysql_select_db($dbname, $this->link_id) === false )
     18             {
     19                 $this->ErrorMsg("Can't select MySQL database($dbname)!");
     20                 
     21                 return false;
     22             }
     23             else
     24             {
     25                 return true;
     26             }
     27         }
     28 
     29     }
     30     //选择数据库
     31     public function select_database($dbname)
     32     {
     33         return mysql_select_db($dbname, $this->link_id);
     34     }
     35     //列出所有表
     36     public function list_tables($dbname){
     37         return mysql_list_tables($dbname,$this->link_id);
     38         }
     39     //列出一个表的所有字段
     40     public function list_fields($dbname,$tbname){
     41         return  mysql_list_fields($dbname,$tbname,$this->link_id);
     42         }
     43     //取得关联数组的结果表示
     44     public function fetch_array($query, $result_type = MYSQL_ASSOC)
     45     {
     46         return mysql_fetch_array($query, $result_type);
     47     }
     48     //执行sql语句
     49     public function query($sql)
     50     {
     51         return mysql_query($sql, $this->link_id);
     52     }
     53     //取得前一次 MySQL 操作所影响的记录行数
     54     public function affected_rows()
     55     {
     56         return mysql_affected_rows($this->link_id);
     57     }
     58     //取得结果集中行的数目
     59     public function num_rows($query)
     60     {
     61         return mysql_num_rows($query);
     62     }
     63     //取得上一步 INSERT 操作产生的 ID 
     64     public function insert_id()
     65     {
     66         return mysql_insert_id($this->link_id);
     67     }
     68     //限制执行sql的结果条目数
     69     public function selectLimit($sql, $num, $start = 0)
     70     {
     71         if ($start == 0)
     72         {
     73             $sql .= ' LIMIT ' . $num;
     74         }
     75         else
     76         {
     77             $sql .= ' LIMIT ' . $start . ', ' . $num;
     78         }
     79         return $this->query($sql);
     80     }
     81     //获得一条结果true或一个字段false
     82     public function getOne($sql, $limited = false)
     83     {
     84         if ($limited == true)
     85         {
     86             $sql = trim($sql . ' LIMIT 1');
     87         }
     88         
     89         $res = $this->query($sql);
     90         if ($res !== false)
     91         {
     92             $row = mysql_fetch_row($res);
     93         
     94             return $row[0];
     95         }
     96         else
     97         {
     98             return false;
     99         }
    100     }
    101     //获取一条结果 关联数组 
    102     public function getrow($sql)
    103     {
    104         $res = $this->query($sql);
    105         if ($res !== false)
    106         {
    107             return mysql_fetch_assoc($res);
    108         }
    109         else
    110         {
    111             return false;
    112         }
    113     }
    114     //  获取全部结果 关联数组
    115     public function getAll($sql)
    116     {
    117         $res = $this->query($sql);
    118         if ($res !== false)
    119         {
    120             $arr = array();
    121             while ($row = mysql_fetch_assoc($res))
    122             {
    123                 $arr[] = $row;
    124             }
    125         
    126             return $arr;
    127         }
    128         else
    129         {
    130             return false;
    131         }
    132     }
    133     
    134 
    135     //  错误显示
    136     function ErrorMsg($message = '', $sql = '')
    137     {
    138         if ($message)
    139         {
    140             echo "<b>error info</b>: $message\n\n";
    141         }
    142         else
    143         {
    144             echo "<b>MySQL server error report:";
    145             print_r(mysql_error($this->link_id));
    146         }
    147         
    148         exit;
    149     }
    150     //关闭mysql连接
    151     function my_close() {
    152         mysql_close($this->link_id);
    153     }
    154 }
    155 ?>

    所有代码:
    http://pan.baidu.com/share/link?shareid=361388&uk=1074075174

  • 相关阅读:
    Inception V1-V4
    NDCG的理解
    进程与线程
    Java中的接口和抽象类
    HashMap的工作原理
    基于比较排序的算法复杂度的下界
    数据库-left join,right join,inner join,full join
    外排序 External sorting
    数据流中的中位数 Find Median from Data Stream
    Codeforces Round #272 (Div. 2)
  • 原文地址:https://www.cnblogs.com/caps/p/2941529.html
Copyright © 2020-2023  润新知