文章添加功能
1,編寫(xiě)文章添加模板
新建ArticleAddHtml.php文件:
這里用到了百度的ueditor插件
具體用法:
通過(guò)官網(wǎng)訪問(wèn):(http://ueditor.baidu.com),進(jìn)入下載頁(yè)面下載PHP版本的如圖所示:
下載好解壓到項(xiàng)目根目錄可以新建個(gè)umeditor文件夾,我的目錄是這樣的:
載入代碼如下所示:(ArticleAddHtml.php)
<?php require './header.php'; header("Content-Type:text/html;charset=utf-8"); ?> <h1>后臺(tái)文章管理頁(yè)面</h1> <form method="post"> 文章分類(lèi): <select name="category"> <?php foreach ($category as $v):?> <option value="<?php echo $v['id'];?>"><?php echo $v['name'];?></option> <?php endforeach;?> </select> <a href="category.php">分類(lèi)管理</a><br> 標(biāo)題:<input type="text" name="title"><br> 作者:<input type="text" name="author"> <div> <link href="./umeditor/themes/default/css/umeditor.min.css" rel="stylesheet"> <script src="./umeditor/third-party/jquery.min.js"></script> <script src="./umeditor/umeditor.config.js"></script> <script src="./umeditor/umeditor.min.js"></script> <script src="./umeditor/lang/zh-cn/zh-cn.js"></script> <script> $(function () { UM.getEditor('myEditor'); }); </script> <script type="text/plain" id="myEditor" style="width: 1025px;height: 250px" name="content"> <p>添加文章內(nèi)容......</p> </script> </div> <input type="submit" value="提交"> <input type="button" value="取消" onclick="{if(confirm('確定要取消添加文章嗎?')){window.location.href='index.php';}return false;}"> </form>
展示如下:
2,新建ArticleAdd.php文件
獲取表單提交后進(jìn)行數(shù)據(jù)庫(kù)的添加操作
代碼如下:
<?php require './init.php'; $sql='select id,name from cms_category order by sort'; $category=$db->fetchAll($sql); if (!empty($_POST)){ //獲取文章分類(lèi) $data['cid']=isset($_POST['category'])?abs(intval($_POST['category'])):0; //獲取文章標(biāo)題 $data['title']=isset($_POST['title'])?trim(htmlspecialchars($_POST['title'])):''; //獲取作者 $data['author']=isset($_POST['author'])?trim(htmlspecialchars($_POST['author'])):''; //獲取文章內(nèi)容 $data['content']=isset($_POST['content'])?trim($_POST['content']):''; if(empty($data['cid'])||empty($data['title'])||empty($data['author'])){ $error[]='文章分類(lèi),標(biāo)題,作者不能為空!'; }else{ $sql="insert into cms_article(title,content,author,addtime,cid)values(:title,:content,:author,now(),:cid)"; $db->data($data)->query($sql); //跳轉(zhuǎn)到首頁(yè) header("location:index.php"); } } require './ArticleAddHtml.php';
3,效果展示: