swm8023

index.php

 1 <?php /*index.php 主入口文件*/ ?>
 2 <html>
 3     <head>
 4         <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
 5         <style type="text/css">
 6             body {font-size:12px;}
 7             td {font-size:12px;}
 8         </style>
 9     </head>
10     <body>
11         <h1>图书表管理</h1>
12         <p>
13             <a href="index.php?action=add">添加图书</a>&nbsp;
14             <a href="index.php?action=list">图书列表</a>&nbsp;
15             <a href="index.php?action=ser">搜索图书</a><hr/>
16         </p>
17     <?php 
18         error_reporting(E_ALL & ~E_NOTICE);
19         include "conn.inc.php";
20         if ($_GET[\'action\'] == \'insert\') {
21             $sql = "INSERT INTO books(bookname,publisher,author,price,ptime,detail) VALUES(
22                     \'{$_POST[\'bookname\']}\',\'{$_POST[\'publisher\']}\',\'{$_POST[\'author\']}\',
23                     \'{$_POST[\'price\']}\',".time().",\'{$_POST[\'detail\']}\')";
24             $stmt = $pdo->exec($sql);
25             echo $stmt ? "添加图书成功" : "添加图书失败";
26         }  elseif ($_GET[\'action\'] == \'update\') {
27             $sql = "UPDATE books SET bookname=\'{$_POST[\'bookname\']}\', publisher=\'{$_POST[\'publisher\']}\',author=\'{$_POST[\'author\']}\',
28                     price=\'{$_POST[\'price\']}\',detail=\'{$_POST[\'detail\']}\' WHERE id = \'{$_POST[\'id\']}\'";
29             $stmt = $pdo->exec($sql);
30             echo $stmt ? "修改图书成功" : "修改图书失败";
31         } elseif ($_GET[\'action\'] == \'del\') {            
32             $sql = "DELETE FROM books WHERE id = \'{$_GET[\'id\']}\';";
33             $stmt = $pdo->exec($sql);
34             echo $stmt ? "删除图书成功" : "删除图书失败";
35         }  elseif ($_GET[\'action\'] == \'add\') {
36             include "add.inc.php";
37         } elseif ($_GET[\'action\'] == \'mod\') {
38             include "mod.inc.php";
39         } elseif ($_GET[\'action\'] ==\'ser\') {
40              include "ser.inc.php";
41         } else {
42             include "list.inc.php";
43         }
44     
45     
46     ?>
47     
48     
49     </body>
50 </html>

conn.inc.php

1 <?php
2     try {
3         $pdo = new PDO("mysql:host=localhost;dbname=bookstore", "root", "root");
4     } catch (PDOException $e) {
5         echo $e->getMessage();
6     }
7 ?>

 

add.inc.php

1 <h3>添加图书</h3>
2 <form action="index.php?action=insert" method="post">
3     图书名称: <input type="text" name="bookname" value="" /><br>
4     出版商名: <input type="text" name="publisher" value="" /><br>
5     图书作者: <input type="text" name="author" value="" /><br>
6     图书价格: <input type="text" name="price" value="" /><br>
7     图书介绍: <input type="text" name="detail" value="" /><br>
8     <input type="submit" name="add" value="添加图书" />
9 </form>

mod.inc.php

 1 <?php
 2     $sql = "SELECT * FROM books WHERE id = \'{$_GET[\'id\']}\';";
 3     $stmt = $pdo->query($sql);
 4     $st = $stmt->fetch(PDO::FETCH_ASSOC);
 5 ?>
 6 <h3>修改图书</h3>
 7 <form action="index.php?action=update" method="post">
 8     <input type="hidden" name="id" value="<?php echo $st[\'id\']?>" />
 9     图书名称: <input type="text" name="bookname" value="<?php echo $st[\'bookname\']?>" /><br>
10     出版商名: <input type="text" name="publisher" value="<?php echo $st[\'publisher\']?>" /><br>
11     图书作者: <input type="text" name="author" value="<?php echo $st[\'author\']?>" /><br>
12     图书价格: <input type="text" name="price" value="<?php echo $st[\'price\']?>" /><br>
13     图书介绍: <input type="text" name="detail" value="<?php echo $st[\'detail\']?>" /><br>
14     <input type="submit" name="add" value="修改图书" />
15 </form>

 

list.inc.php

 1 <?php
 2     $ser = empty($_POST) ? $_GET : $_POST;
 3     
 4     $param_name = array(\'bookname\', \'publisher\', \'author\', \'startprice\', \'endprice\');  
 5     $nametrans = array(\'bookname\'=>\'书名\', \'publisher\'=>\'出版社\', \'author\'=>\'作者\', 
 6             \'startprice\'=>\'价格高于\', \'endprice\'=>\'价格低于\');
 7     
 8     $where = array();
 9     $param = "";
10     $title = "";
11     foreach ($param_name as $name) {
12         if (!empty($ser[$name])) {
13             if (strpos($name, "price") == 3) {
14                 $where[] = "price < \'{$ser[$name]}\'";
15             } elseif (strpos($name, "price") == 5) {
16                 $where[] = "price > \'{$ser[$name]}\'";
17             } else {
18                 $where[] = $name." like \'%{$ser[$name]}%\'";
19             }
20             $param .= "&".$name."={$ser[$name]}";
21             $title .= $nametrans[$name]."$ser[$name] ";
22         }
23     }
24     if (!empty($where)) {
25         $where = "WHERE ".implode(" and ", $where);
26         $title = "搜索: ".$title;
27     } else {
28         $where = "";
29         $title = "图书列表";
30     }
31     echo \'<h3>\'.$title.\'</h3>\';
32 ?>
33 <table>
34     <tr align="left" bgcolor="#cccccc">
35         <th>ID</th><th>图书名称</th><th>出版商</th><th>作者</th><th>价格</th><th>上架时间</th><th>操作</th>
36     </tr>
37     <?php 
38         include "page.class.php";
39         //得到记录数
40         $sql = "SELECT count(*) FROM books {$where}";
41         $stmt = $pdo->query($sql);
42         list($total) = $stmt->fetch(PDO::FETCH_NUM);
43         $page = new Page($total, 10, 9, $param);
44         
45         //查询当前页列表
46         $sql = "SELECT * from books {$where} ORDER BY id DESC {$page->limt};";
47         
48         $stmt = $pdo->query($sql);
49         $row = 0;
50         while (@$st = $stmt->fetch(PDO::FETCH_ASSOC)) {
51             echo \'<tr\'.($row++%2?"":" bgcolor=\"#eeeeee\"").\'>\';
52             echo "<td>{$st[\'id\']}</td><td>{$st[\'bookname\']}</td><td>{$st[\'publisher\']}</td>
53             <td>{$st[\'author\']}</td><td>¥{$st[\'price\']}</td><td>".date("Y-m-d", $st[\'ptime\'])."</td>
54             <td><a href=\'index.php?action=mod&id=".$st[\'id\']."\'>修改</a><a href=\'index.php?action=del&id=".$st[\'id\']."\'>删除</a></td>";
55             echo \'</tr>\';
56         }
57         if ($row == 0)
58             echo "<tr><td colspan=\'6\' align=\'center\'>没有图书被找到</td></tr>";
59         else
60             echo "<tr><td colspan=\'6\'>".$page->fpage()."</td></tr>";    
61         
62     ?>
63 
64 </table>

 

ser.inc.php

1 <h3>搜索图书</h3>
2 <form action="index.php?action=list" method="post">
3     图书名称: <input type="text" name="bookname" value="" /><br>
4     出版商名: <input type="text" name="publisher" value="" /><br>
5     图书作者: <input type="text" name="author" value="" /><br>
6     图书价格: <input type="text" name="startprice" size="5" /> ——
7             <input type="text" name="endprice" size="5" /><br>
8     <input type="submit" name="ser" value="搜索图书" />
9 </form>

 

page.class.php

  1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2 <?php
  3     class Page {
  4         private $total;        //记录数
  5         private $listRows;    //每页显示行数
  6         private $limit;        //sql语句的Limit从句,限制获取记录个数
  7         private $uri;        //自动获取url地址
  8         private $pageNum;    //总页数
  9         private $listNum;    //显示的页数
 10         private $page;        //当前页
 11         private $config = array(\'head\'=>"条记录",\'prev\'=>"上一页",\'next\'=>\'下一页\',\'first\'=>\'首页\',\'last\'=>\'末页\');    
 12         
 13         //构造函数,query是向目标页面传递参数,ord是查询顺序,true从第一页开始,false从末页开始
 14         public function __construct($total, $listRows=25, $listNum = 9, $query="", $ord=true){
 15             $this->total= $total;
 16             $this->listRows = $listRows;
 17             $this->listNum = $listNum;
 18             $this->uri = $this->getUri($query);
 19             $this->pageNum = ceil($this->total / $this->listRows);
 20             //设置页面
 21             $page = !empty($_GET[\'page\'])?$_GET[\'page\']:($ord?1:$this->pageNum);
 22             //如果传进来page参数非法就到第一面(\D=[^0-9])
 23             $this->page = $total > 0?(preg_match(\'/\D/\', $page)?1:$page):0;
 24             $this->limit = "LIMIT ".$this->setLimit();
 25         }
 26         //自动获取访问的当前url
 27         private function getUri($query){
 28             $request_uri = $_SERVER[\'REQUEST_URI\'];
 29             $url = strstr($request_uri, \'?\') ? $request_uri : $request_uri.\'?\';
 30             if (is_array($query))
 31                 $url .=http_build_query($query);
 32             else if ($query != "")
 33                 $url .="&".trim($query, "?&");
 34             
 35             $arr = parse_url($url);    //将url分成path和url两部分
 36             //去掉page属性
 37             if (isset($arr[\'query\'])) {
 38                 parse_str($arr[\'query\'], $arrs);
 39                 unset($arrs[\'page\']);
 40                 $url = $arr[\'path\'].\'?\'.http_build_query($arrs);
 41             }
 42             if(strstr($url, \'?\')) {
 43                 if (substr($url, -1)!= \'?\') $url .= \'&\';
 44             }  else {
 45                 $url .= \'?\';
 46             }
 47             return $url;
 48         }
 49         
 50         //设置limit从句的范围(limit m,n 表示从m开始取n条,limit m 表示从头取m条)
 51         private function setLimit() {
 52             if($this->page > 0)
 53                 return ($this->page-1)*$this->listRows.", {$this->listRows}";
 54             else
 55                 return 0;
 56         }
 57         //连贯操作设置参数
 58         function set($param, $value) {
 59             if(array_key_exists($param, $this->config))
 60                 $this->config[$param] = $value;
 61             return $this; 
 62         }
 63         //外部可以获得limit和page
 64         function __get($args) {
 65             if ($args == "limit" || $args == "page") 
 66                 return $this->$args;
 67             else 
 68                 return null;
 69         }
 70         
 71         
 72         //输出分页信息,0~7表示需要哪些模块,默认全都要
 73             /* fpage                    传入0~7至多8个参数,表示需要显示哪些模块
 74              * firstAndPrev,nextAndLast    显示首页前一页/下一页末页链接
 75              * pagelist                    显示页面列表(附近几页的链接)
 76              * gopage                    直接跳转到某页的链接
 77              * disnum                    本页显示的记录条数
 78              * start,end                本页显示的页面范围
 79              */
 80         function fpage(){
 81             $arr = func_get_args();
 82             if (count($arr) < 1)
 83                 $arr = range(0, 7);
 84             $html[0] = "&nbsp;共<b>{$this->total}</b>{$this->config[\'head\']}&nbsp;";
 85             $html[1] = "&nbsp;本页<b>{$this->disnum()}</b>条&nbsp;";
 86             $html[2] = "&nbsp;本页从<b>{$this->start()}-{$this->end()}条&nbsp;";
 87             $html[3] = "&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;";
 88             $html[4] = $this->firstAndPrev();
 89             $html[5] = $this->pagelist();
 90             $html[6] = $this->nextAndLast();
 91             $html[7] = $this->goPage();
 92             
 93             $fpage = \'<div style="font:12px \\'\5B8B\4F53\\',san-serif">\';
 94             foreach($arr as $i) $fpage .= $html[$i];
 95             $fpage .= \'</div>\';
 96             return $fpage;
 97         }
 98         private function disnum(){
 99             return $this->total> 0 ? $this->end()-$this->start()+1 : 0;
100         }
101         private function start(){
102             return $this->total > 0 ? ($this->page -1) * $this->listRows + 1 : 0;
103         }
104         private function end(){
105             return min($this->page*$this->listRows, $this->total);
106         }
107         private function firstAndPrev(){
108             $str = "";
109             if ($this->page > 1) {
110                 $str .= "&nbsp;<a href=\'{$this->uri}page=1\'>{$this->config[\'first\']}</a>&nbsp;";
111                 $str .="&nbsp;<a href=\'{$this->uri}page=".($this->page-1)."\'>{$this->config[\'prev\']}</a>&nbsp;";
112             }
113             return $str;
114         }
115         private function pagelist(){
116             $linkpage = "&nbsp;<b>";
117             $half = floor($this->listNum/2);
118             for ($i = max(1, $this->page - $half); $i < $this->page; $i++) 
119                 $linkpage .= "<a href=\'{$this->uri}page=$i\'>$i</a>&nbsp;";
120             $linkpage .= "<span style=\'padding:1px 2px;background:#BBB;color:white\'>$this->page</span>&nbsp;";
121             for ($i = $this->page+1; $i < min($this->pageNum+1, $this->page+$this->listNum-$half); $i++)
122                 $linkpage .= "<a href=\'{$this->uri}page=$i\'>$i</a>&nbsp;";
123             $linkpage .= "</b>";
124             return $linkpage;
125         }
126         private function nextAndLast(){
127             $str = "";
128             if ($this->page < $this->pageNum) {
129                 $str .="&nbsp;<a href=\'{$this->uri}page=".($this->page+1)."\'>{$this->config[\'next\']}</a>&nbsp;";
130                 $str .= "&nbsp;<a href=\'{$this->uri}page={$this->pageNum}\'>{$this->config[\'last\']}</a>&nbsp;";
131             }
132             return $str;
133         }
134         private function goPage(){
135             $str ="&nbsp<input type=\'text\' style=\'width:20px;heigght:17px\' value={$this->page} />";
136             $str .= "<input type=\'button\' value=\'Go\' onclick=\'javascript:var page=(this.previousSibling.value);
137                     location=\"".$this->uri."page=\"+page\'>&nbsp;";
138             return $str;
139         }
140     }
141     
142 ?>
143 
144     
145     
146     

执行结果

分类:

技术点:

相关文章: