dbsystem.php PHP: <?php function db($variable) { $db = array(); $db['website'] = 'http://192.168.0.200:8085/test/'; $db['dbhost'] = 'localhost'; //MySQL Host $db['dbname'] = 'snews'; //Database Name $db['dbuname'] = 'root'; //Database Username $db['dbpass'] = '123558'; //Database password $db['prefix'] = ''; //Database prefix $db['dberror'] = '<strong>There was an error while connecting to the database.</strong> <br /> Check your database settings.'; //Database error message return $db[$variable]; } function connect_to_db() { $db = mysql_connect(db('dbhost'), db('dbuname'), db('dbpass')); mysql_select_db(db('dbname')) or die(db('dberror')); mysql_query("SET NAMES UTF-8"); if (mysql_num_rows(mysql_query("SHOW TABLES LIKE '".db('prefix')."articles'")) != 1) { die(db('db_tables_error')); } } function html_input($type, $name, $id, $value, $label, $css, $script1, $script2, $script3, $checked, $rows, $cols, $method, $action, $legend) { $lbl = !empty($label) ? '<label for="'.$id.'">'.$label.'</label>' : ''; $ID = !empty($id) ? ' id="'.$id.'"' : ''; $style = !empty($css) ? ' class="'.$css.'"' : ''; $js1 = !empty($script1) ? ' '.$script1 : ''; $js2 = !empty($script2) ? ' '.$script2 : ''; $js3 = !empty($script3) ? ' '.$script3 : ''; $attribs = $ID.$style.$js1.$js2.$js3; $val = ' value="'.$value.'"'; $input = '<input type="'.$type.'" name="'.$name.'"'.$attribs; switch($type) { case 'form': $output = (!empty($method) && $method != 'end') ? '<form method="'.$method.'" action="'.$action.'"'.$attribs.' >' : '</form>'; break; case 'fieldset': $output = (!empty($legend) && $legend != 'end') ? '<fieldset><legend'.$attribs.'>'.$legend.'</legend>' : '</fieldset>'; break; case 'text': case 'password': $output = '<p>'.$lbl.':<br />'.$input.$val.' /></p>'; break; case 'checkbox': case 'radio': $check = $checked == 'ok' ? ' checked="checked"' : ''; $output = '<p>'.$input.$check.' /> '.$lbl.'</p>'; break; case 'hidden': case 'submit': case 'reset': case 'button': $output = $input.$val.' />'; break; case 'textarea': $output = '<p>'.$lbl.':<br /><textarea name="'.$name.'" rows="'.$rows.'" cols="'.$cols.'"'.$attribs.'>'.$value.'</textarea></p>'; break; } # 1.6.0 - was "echo $output;" in 1.5.31. return $output; } function l($variable) { $l = array(); #SITE LANGUAGE VARIABLES $l['home'] = 'Home'; $l['home_sef'] = 'home'; //default value is used only if "home_SEF" is not set in the database - allowed characters are [a-z] [A-Z] [0-9] [-] [_] $l['archive'] = 'Archive'; $l['rss_feed'] = 'RSS Feed'; #表单 $l['username'] = '用户名'; $l['psw'] = '密码'; $l['submit'] = '提交'; $l['reset'] = '恢复'; $l['back'] = '返回'; #出错信息 $l['err_1'] = '用户名和密码不能为空!'; $l['err_2'] = '参数错误!'; return $l[$variable]; } function Center(){ switch(true) { case isset($_GET['category']): ; break; case isset($_GET['action']): DoAction($_GET['action']);return ; break; } switch(true) { case isset($_POST['action']): DoAction($_POST['action']);return; break; } switch ($_action){ default: UserList(); } } function Input(){ echo html_input('form', '', '', '','', '', '', '', '', '', '', '', 'post', 'du_index.php', ''); echo html_input('text', 'username', 'username', '',l('username')); echo html_input('password', 'psw', 'psw', '',l('psw')); echo html_input('submit', '', '', l('submit')); echo html_input('button', '', '', l('back'),'','','onclick=location="'.$_SERVER['HTTP_REFERER'].'"', '', '', '', '', '', '', '', ''); echo html_input('hidden', 'action', 'action', 'adduser','', '', '', '', '', '', '', '', '', '', ''); echo html_input('form', 'uname', 'uname', '','', '', '', '', '', '', '', '', 'end', 'cmd.php', ''); } function DoAction($strAct){ switch($strAct){ case 'adduser':AddUser(); break; case 'add':Input(); break; case 'del':DelUser(); break; case 'modify':InputModify(); break; case 'modifyuser':Modify(); break; } } connect_to_db(); function AddUser(){ $_name = isset($_POST['username'])?$_POST['username']:''; $_psw = isset($_POST['psw'])?$_POST['psw']:''; if($_name==''||$_psw==''){ echo l('err_1') ;exit();} $_query = "INSERT INTO users( user_login, user_pass)values('$_name','$_psw')"; mysql_query($_query) or die('err'); UserList(); } function UserList(){ $_query = "SELECT id, user_login, user_pass FROM users ORDER BY id DESC "; $_result = mysql_query($_query); while ($_r = mysql_fetch_array($_result)) { $_list .= sprintf("<li><span><a href='du_index.php?action=del&id=%d'>删除</a> | <a href='du_index.php?action=modify&id=%d'>修改</a></span>%s [%s]</li>", $_r['id'],$_r['id'],$_r['user_login'], $_r['user_pass'] ); } echo "<ol>$_list</ol><div><a href='du_index.php?action=add'>加一个用户</a></div>"; } function DelUser(){ $_id = isset($_GET['id'])?$_GET['id']:0; if($_id==0||!is_numeric($_id)){ echo l('err_2') ;exit();} //mysql_query("SELECT * FROM users WHERE id = $_id") or die('err'); if(mysql_num_rows(mysql_query("SELECT * FROM users WHERE id = $_id"))==0){ echo "此记录已被删除,<a href='du_index.php'>返回</a>"; exit(); } $_query = "DELETE FROM users WHERE id = $_id"; mysql_query($_query) or die('err'); echo "删除成功,<a href='".$_SERVER['HTTP_REFERER']."'>返回</a>"; } function InputModify(){ $_id = isset($_GET['id'])?$_GET['id']:0; if($_id==0||!is_numeric($_id)){ echo l('err_2') ;exit();} $_query = "SELECT * FROM users WHERE id = $_id"; $_result = mysql_query($_query) or die('err'); if(mysql_num_rows($_result)==0){ echo "此记录已被删除,<a href='du_index.php'>返回</a>"; exit(); } $_r = mysql_fetch_array($_result); echo html_input('form', '', '', '','', '', '', '', '', '', '', '', 'post', 'du_index.php', ''); echo html_input('text', 'username', 'username', $_r['user_login'],l('username')); echo html_input('password', 'psw', 'psw', $_r['user_pass'],l('psw')); echo html_input('submit', '', '', l('submit')); echo html_input('button', '', '', l('back'),'','','onclick=location="'.$_SERVER['HTTP_REFERER'].'"', '', '', '', '', '', '', '', ''); echo html_input('hidden', 'action', 'action', 'modifyuser','', '', '', '', '', '', '', '', '', '', ''); echo html_input('hidden', 'id', 'id', $_id,'', '', '', '', '', '', '', '', '', '', ''); echo html_input('form', 'uname', 'uname', '','', '', '', '', '', '', '', '', 'end', 'cmd.php', ''); } function Modify(){ $_name = isset($_POST['username'])?$_POST['username']:''; $_psw = isset($_POST['psw'])?$_POST['psw']:''; $_id = $_id = isset($_POST['id'])?$_POST['id']:0; if($_name==''||$_psw==''){ echo l('err_1') ;exit();} if($_id==0||!is_numeric($_id)){ echo l('err_2') ;exit();} $_query = "UPDATE users SET user_login = '$_name', user_pass = '$_psw' WHERE id = $_id"; mysql_query($_query) or die('err'); echo "修改成功,<a href='du_index.php'>返回</a>"; } /*别人的一个分页的东西,先留着*/ function splitpage($pageall,$page=1,$urled=null,$strpage = "page",$pageaverage = 10){ $pageaverage -= 1; $page=intval($page >= 1 ?$page:1 ); $page=$page>$pageall?$pageall:$page; $startpage=$page- $pageaverage >0?$page- ceil(($pageaverage / 2)):1; $startpage=($page + ceil($pageaverage /2) >$pageall)?$pageall-$pageaverage:$startpage; $startpage=$startpage >0?$startpage:1; $stoppage= $startpage+$pageaverage >$pageall?$pageall:$startpage+$pageaverage; if($urlfile==null) $urlfile=$php_self; if(!strrpos($urlfile,'?')) $urled .= '?'; foreach($_GET as $k => $v) { $urled = ($k<>$strpage) ? $urled.$k.'='.urlencode($v).'&' : $urled; } if ($page>1){ $mess ="<a href='".$urled.$strpage."=1'>|<</a>"; $mess .="<a href='".$urled.$strpage."=".($page-1)."'><</a>"; }else{ $mess ="|< "; $mess .="< "; } for($i= $startpage; $i<= $stoppage ;$i++){ if($i<= $pageall&& !($page==$i)) $mess .= "<a href='".$urled.$strpage."=".$i."'>".$i."</a> "; else $mess .= "".$i." "; } if ($page < $pageall){ $mess .=" <a href='".$urled.$strpage."=".($page+1)."'> ></a>"; $mess .=" <a href='".$urled.$strpage."=".$pageall."'> >|</a>"; }else{ $mess .=" >"; $mess .=" >|"; } return $mess; } ?>
page.php PHP: <?php class Page { var $_page,$_url; var $_totalRecord,$_totalPage; var $_pageSize; var $_modifyUrl; var $_selectSql,$_updateSql,$_delSql; var $_itemNum; var $_item_arr,$_itemField_arr,$_itemStyle_arr,$_itemLink_arr,$_itemType_arr; var $_pageUrl; function Page(){ $this->_itemNum = 0; $this->_item_arr = ''; $this->_itemField_arr = ''; $this->_itemLink_arr = ''; $this->_itemStyle_arr = ''; $this->_itemType_arr = ''; $this->_pageSize = 5; $this->_page = 1; } function SetPageSize($intSize){ $_pageSize = $intSize; } function GetPageSize(){ return $this->_pageSize; } function SetModifyUrl($strUrl){ $this->_modifyUrl = "$strUrl?"; if (substr_count($strUrl,'?')>0) $this->_modifyUrl = "$strUrl&"; } # 获取Sql function SetSelectSql($strSql){ $this->_selectSql = $strSql; } function SetUpdateSql($strSql){ $this->_updateSql = $strSql; } function SetDelSql($strSql){ $this->_delSql = $strSql; } #获取Item function SetItem($item){ $this->_item_arr = explode('!$!',$item); $this->_itemNum = count($this->_item_arr); } function SetItemField($item){ $this->_itemField_arr = explode('!$!',$item); } function SetItemStyle($item){ $this->_itemStyle_arr = explode('!$!',$item); } function SetItemLink($item){ $this->_itemLink_arr = explode('!$!',$item); } function SetItemType($item){ $this->_itemType_arr = explode('!$!',$item); } function ShowList(){ //addslashes() //stripcslashes() //$_SERVER['PHP_SELF'] #总纪录数 $this->_totalRecord = mysql_num_rows(mysql_query($this->_selectSql)) or die('err'); if ($this->_totalRecord ==0) {echo 'nothing';exit();} #总页数 $this->_totalPage = ceil($this->_totalRecord/$this->_pageSize); #过滤Page $this->_page = is_numeric($this->_page)?$this->_page:1; $_query = sprintf($this->_selectSql.' LIMIT %d, %d',$this->_page, $this->_pageSize); $_result = mysql_query($_query) or die('err'); $_list .="<table class='widefat'><thead><tr>"; for($i=0;$i<$this->_itemNum;$i++){ $_list .="<th>".$this->_item_arr[$i]."</th>"; } $_list .= "<th style='text-align:center'>操作</th>"; $_list .= "</tr></thead><tbody id='the-list'>"; $n = 0; while ($_r = mysql_fetch_array($_result)) { $styleTR = $n%2==0?' class="alternate" ':''; $_list .="<tr $styleTR>"; for($i=0;$i<$this->_itemNum;$i++){ $styleTD = $this->_itemStyle_arr[$i]!=''&&count($this->_itemStyle_arr)==$this->_itemNum?$this->_itemStyle_arr[$i]:''; $_list .= "<td $styleTD>"; if($this->_itemLink_arr[$i]!=''&&count($this->_itemLink_arr)==$this->_itemNum){ $_list .=sprintf("<a href='%s'>%s</a>",$this->_itemLink_arr[$i],$_r[$this->_itemField_arr[$i]]); }else{ $_list .="".$_r[$this->_itemField_arr[$i]].""; } $_list .= "</td>"; } $_list .="<td>操作区</td>"; $_list .="</tr>"; $n ++; } $_list .= "</tbody><table>"; echo "$_list".$this->PageNum($this->_totalPage,$this->_totalRecord,$this->_page,5,'p',$this->_pageUrl); } function PageNum($totalPage,$totalRecord,$page=1,$middle=10,$key='p',$url){ $middle -= 1; $page = intval($page>=1?$page:1); $page = $page>$totalPage?$totalPage:$page; $startPage = $page-$middle>0?$page-ceil(($middle/2)):1; $startPage = ($page+ceil($middle /2)>$totalPage)?$totalPage-$middle:$startPage; $startPage = $startPage >0?$startPage:1; $stopPage = $startPage+$middle>$totalPage?$totalPage:$startPage+$middle; $url = !strrpos($url,'?')?$url.'?':$url.'&'; //if($urlfile==null) $urlfile=$_SERVER['PHP_SELF']; //if(!strrpos($urlfile,'?')) $url .= '?'; //echo $url.'<br>'; //echo $_SERVER['PHP_SELF']; //foreach($_GET as $k => $v) { // $url = ($k<>key)?$url.$k.'='.urlencode($v).'&':$url; // $url = str_replace($key.'='.$v|$key.'='.$v.'&','',$url); //} //echo $url; #输出部分 if($page<>1){ $_html .= sprintf('<a href="%s">|<</a> <a href="%s"><<</a> ',$url.$key.'=1',$url.$key.'='.($page-1)); } for ($i=$startPage;$i<=$stopPage;$i++){ $_html .= sprintf(' <a href="%s" >%d</a> ',$url.$key.'='.$i,$i); } if($page<>$totalPage){ $_html .= sprintf(' <a href="%s">>></a> <a href="%s">>|</a>',$url.$key.'='.($page+1),$url.$key.'='.$totalPage); } return '<div class="dupagebar">'.$_html.'</div>'; } } ?>
示例: PHP: <?php require_once('page.php'); require_once('dbsystem.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> /* 列表页标准 */ #listmain{font-size:16px; padding:10px} #wrap {background:#FFFFFF none repeat scroll 0%;border:1px solid #CCCCCC;margin:15px auto;padding:1em;font-size:13px} .widefat{width:100%} .widefat thead,.thead {background:#DFDFDF none repeat scroll 0%; color:#006666} .widefat td,.widefat th {padding:5px 6px;} .alternate {background:#F1F1F1 none repeat scroll 0%;} .page{padding:5px 6px; text-align:center; background:#DFDFDF none repeat scroll 0%;font-size:12px; text-align:center} #the-list td{padding:10px} #div_sreach{margin:0; padding:0; color:#006666; font-size:12px; background-color:#dbdbdb; height:30px; margin-bottom:12px} .bar{font-size:14px} .dupagebar{padding:5px; text-align:center} </style> </head> <body> <div id="listmain"> <?php $test = new Page(); $test->_page = isset($_GET['p'])?$_GET['p']:1; $test->_pageUrl = 'test.php'; $test->SetSelectSql('SELECT id, user_login, user_pass FROM users ORDER BY id DESC'); $test->SetItem('名字!$!密码'); $test->SetItemField('user_login!$!user_pass'); $test->SetItemLink('#!$!'); $test->SetItemStyle("!$!style='color:red;text-align:center'"); $test->ShowList(); ?> </div> </body> </html>