14-1
上課範例:index.php
003 | include_once '../../mainfile.php' ; |
004 | include_once XOOPS_ROOT_PATH. "/header.php" ; |
005 | include_once 'function.php' ; |
007 | $xoopsOption [ 'template_main' ] = "tad_note_index.html" ; |
012 | function add_form( $note_sn =null){ |
013 | global $xoopsDB , $xoopsUser , $xoopsTpl ; |
015 | if ( empty ( $xoopsUser ))redirect_header( 'index.php' , 3, "請先登入。" ); |
018 | $sql = "select * from " . $xoopsDB ->prefix( "tad_notes" ). " where `note_sn`='$note_sn'" ; |
019 | $result = $xoopsDB ->query( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
020 | $doc = $xoopsDB ->fetchArray( $result ); |
028 | include_once (XOOPS_ROOT_PATH. "/class/xoopsformloader.php" ); |
029 | $XoopsFormHiddenToken = new XoopsFormHiddenToken(); |
030 | $token = $XoopsFormHiddenToken ->render(); |
034 | $sql = "select * from " . $xoopsDB ->prefix( "tad_note_cate" ). " where cate_enable='1' order by `cate_sort`" ; |
035 | $result = $xoopsDB ->query( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
036 | while ( $cate = $xoopsDB ->fetchArray( $result )){ |
037 | $selected =( $cate [ 'cate_sn' ]== $doc [ 'cate_sn' ])? "selected" : "" ; |
038 | $option .= "<option value='{$cate['cate_sn']}' $selected>{$cate['cate_title']}</option>" ; |
042 | $note_sort = empty ( $note_sn )?get_max_sort(): $doc [ 'note_sort' ]; |
045 | $note_date = empty ( $note_sn )? date ( "Y-m-d H:i:s" ): $doc [ 'note_date' ]; |
047 | $note_public1 =( $doc [ 'note_public' ]== '1' )? "checked" : "" ; |
048 | $note_public0 =( $doc [ 'note_public' ]== '0' )? "checked" : "" ; |
052 | <script type= 'text/javascript' src= 'class/ckeditor/ckeditor.js' ></script> |
053 | <script language= 'javascript' type= 'text/javascript' src= 'class/DatePicker/WdatePicker.js' ></script> |
055 | <form action= '{$_SERVER[' PHP_SELF ']}' method= 'post' > |
057 | <tr><th nowrap>所屬分類</th><td> |
058 | <select name= 'cate_sn' > |
059 | <option value= '0' >不分類</option> |
063 | <tr><th>文章標題</th><td><input type= 'text' name= 'note_title' size=40 value= '{$doc[' note_title ']}' ></td></tr> |
065 | <textarea name= 'note_content' cols=40 rows=6 class = 'ckeditor' id= 'ckeditor' >{ $doc [ 'note_content' ]}</textarea> |
066 | <script type= 'text/javascript' > |
067 | CKEDITOR.replace( 'ckeditor' , { skin : 'v2' , toolbar : 'MyToolbar' } ); |
070 | <tr><th>發布日期</th><td><input type= 'text' name= 'note_date' value= '$note_date' onClick=\"WdatePicker({skin: 'whyGreen' , dateFmt: 'yyyy-MM-dd HH:mm:ss' })\" class = 'Wdate' ></td></tr> |
071 | <tr><th>是否公開</th><td> |
072 | <input type= 'radio' name= 'note_public' value= '1' $note_public1 > 是 |
073 | <input type= 'radio' name= 'note_public' value= '0' $note_public0 > 否 |
075 | <tr><th>排序</th><td><input type= 'text' name= 'note_sort' size=2 value= '$note_sort' ></td></tr> |
078 | <input type= 'hidden' name= 'note_sn' value= '$note_sn' > |
079 | <input type= 'hidden' name= 'op' value= '$op' > |
080 | <input type= 'submit' value= '儲存' > |
095 | $xoopsTpl ->assign( "content" , $main ); |
096 | $xoopsTpl ->assign( "mode" , "form" ); |
102 | global $xoopsDB , $xoopsUser ; |
104 | if (! $GLOBALS [ 'xoopsSecurity' ]->check()){ |
105 | $error =implode( "<br />" , $GLOBALS [ 'xoopsSecurity' ]->getErrors()); |
106 | redirect_header( $_SERVER [ 'PHP_SELF' ],3, $error ); |
109 | $myts =& MyTextSanitizer::getInstance(); |
110 | $_POST [ 'note_title' ] = $myts -> addSlashes ( $_POST [ 'note_title' ]); |
111 | $_POST [ 'note_content' ] = $myts -> addSlashes ( $_POST [ 'note_content' ]); |
112 | $_POST [ 'note_date' ] = $myts -> addSlashes ( $_POST [ 'note_date' ]); |
113 | $_POST [ 'note_sort' ] = $myts -> addSlashes ( $_POST [ 'note_sort' ]); |
115 | $uid = empty ( $xoopsUser )? 0 : $xoopsUser ->uid(); |
117 | $sql = "insert into " . $xoopsDB ->prefix( "tad_notes" ). " (`cate_sn`, `note_title`, `note_content`, `note_date`, `note_public`, `note_count`, `uid`, `note_sort`) values('{$_POST['cate_sn']}' , '{$_POST['note_title']}' , '{$_POST['note_content']}' , '{$_POST['note_date']}' , '{$_POST['note_public']}' , '0' , '{$uid}' , '{$_POST['note_sort']}')" ; |
118 | $xoopsDB ->query( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
123 | $main = "<a href='index.php?op=add_form'>新增記事</a>" ; |
128 | function get_max_sort(){ |
131 | $sql = "select max(`note_sort`) from " . $xoopsDB ->prefix( "tad_notes" ). " where `note_public`='1'" ; |
132 | $result = $xoopsDB ->query( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
133 | list( $max_sort )= $xoopsDB ->fetchRow( $result ); |
139 | global $xoopsDB , $xoopsUser , $xoopsModuleConfig , $xoopsTpl ; |
141 | $now_uid =( $xoopsUser )? $xoopsUser ->uid(): "" ; |
143 | $myts =& MyTextSanitizer::getInstance(); |
146 | $sql = "select * from " . $xoopsDB ->prefix( "tad_notes" ). " where `note_public`='1' order by note_sort" ; |
149 | $result = $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
150 | $total = $xoopsDB ->getRowsNum( $result ); |
152 | $navbar = new PageBar( $total , $xoopsModuleConfig [ 'show_num' ], 10); |
153 | $mybar = $navbar ->makeBar(); |
154 | $bar = sprintf(_BP_TOOLBAR, $mybar [ 'total' ], $mybar [ 'current' ]). "{$mybar['left']}{$mybar['center']}{$mybar['right']}" ; |
158 | $result = $xoopsDB ->query( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
161 | while ( $doc = $xoopsDB ->fetchArray( $result )){ |
163 | $doc [ 'note_title' ] = $myts ->htmlSpecialChars( $doc [ 'note_title' ]); |
164 | $doc [ 'note_date' ] = $myts ->htmlSpecialChars( $doc [ 'note_date' ]); |
166 | $tool =( $doc [ 'uid' ]== $now_uid )?"<a href= 'index.php?op=del¬e_sn={$doc[' note_sn ']}' >刪除</a> | |
167 | <a href= 'index.php?op=modify¬e_sn={$doc[' note_sn ']}' >修改</a> ":" "; |
169 | $main [ $i ][ 'note_sn' ]= $doc [ 'note_sn' ]; |
170 | $main [ $i ][ 'note_title' ]= $doc [ 'note_title' ]; |
171 | $main [ $i ][ 'note_date' ]= $doc [ 'note_date' ]; |
172 | $main [ $i ][ 'tool' ]= $tool ; |
177 | $xoopsTpl ->assign( "news" , $main ); |
178 | $xoopsTpl ->assign( "bar" , $bar ); |
179 | $xoopsTpl ->assign( "mode" , "list" ); |
185 | function del_note( $note_sn =null){ |
188 | $sql = "delete from " . $xoopsDB ->prefix( "tad_notes" ). " where note_sn='$note_sn'" ; |
189 | $xoopsDB ->queryF( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
194 | global $xoopsDB , $xoopsUser ; |
196 | if (! $GLOBALS [ 'xoopsSecurity' ]->check()){ |
197 | $error =implode( "<br />" , $GLOBALS [ 'xoopsSecurity' ]->getErrors()); |
198 | redirect_header( $_SERVER [ 'PHP_SELF' ],3, $error ); |
201 | $myts =& MyTextSanitizer::getInstance(); |
202 | $_POST [ 'note_title' ] = $myts -> addSlashes ( $_POST [ 'note_title' ]); |
203 | $_POST [ 'note_content' ] = $myts -> addSlashes ( $_POST [ 'note_content' ]); |
204 | $_POST [ 'note_date' ] = $myts -> addSlashes ( $_POST [ 'note_date' ]); |
205 | $_POST [ 'note_sort' ] = $myts -> addSlashes ( $_POST [ 'note_sort' ]); |
207 | $uid = empty ( $xoopsUser )? 0 : $xoopsUser ->uid(); |
209 | $sql = "update " . $xoopsDB ->prefix( "tad_notes" ). " set `cate_sn`='{$_POST['cate_sn']}' , `note_title`='{$_POST['note_title']}', `note_content`='{$_POST['note_content']}', `note_date`='{$_POST['note_date']}', `note_public`='{$_POST['note_public']}', `note_sort`='{$_POST['note_sort']}' where `note_sn`='{$_POST['note_sn']}'" ; |
210 | $xoopsDB ->queryF( $sql ) or redirect_header( 'index.php' , 3, mysql_error()); |
214 | $op = empty ( $_REQUEST [ 'op' ])? "" : $_REQUEST [ 'op' ]; |
215 | $note_sn = empty ( $_REQUEST [ 'note_sn' ])? "" : intval ( $_REQUEST [ 'note_sn' ]); |
222 | header( "location:index.php" ); |
232 | header( "location:index.php" ); |
238 | header( "location:index.php" ); |
253 | include_once XOOPS_ROOT_PATH. '/footer.php' ; |