内容编辑器被广泛地应用在各种信息管理后台,它的作用是在线查看和编辑HTML代码和脚本程序。在本章中使用内容编辑器(FCKeditor)对文章内容进行预览和编辑。
FCKeditor是一个专门使用在网页上的文字编辑器,属于开放源代码的所见即所得形式,它是轻量级、不需要复杂安装过程的文字编辑器。下面来了解FCKeditor在PHP中的配置方法及使用。广州网站设计
1.下载及解压
从下面的地址下载新版本FCKeditor:
广州网站设计
- http://www.fckeditor.net/download
将下载后的文件解压到Apache的Web发布目录www/中。
2.调用FCKeditor
新建一个PHP文件,并在程序中加载FCKeditor文件include("FCKeditor/fckeditor.php"),然后加入初始化FCKeditord的各项配置。初始化及配置选项代码如下:
- <?php
- //功能:FCK初始化及配置
- $oFCKeditor = new FCKeditor ( 'FCKeditor1' ); //建立对象
- $oFCKeditor->BasePath = 'FCKeditor/'; //FCKeditor所在的位置
- $oFCKeditor->ToolbarSet = 'Default'; //工具按钮
- $oFCKeditor->Create ( 'EditorDefault', '60%', 150 );
- ?>
【代码解读】
以上的代码实例化编辑器类,并设置了FCKeditor编辑器的基本参数。通过$oFCKeditor->Value的方法,将文章内容部分输出到编辑器中。当编辑器中的内容改变后,再将内容变量更新到对应的数据库字段中,完成文本预览和编辑。完成上述功能的完整代码如下:
广州网站建设
- <?php
- $aid = $_GET ['aid']; //文章索引id
- if ($aid) {
- $con = mysql_connect ( 'localhost', 'root', '198251' ) or die ( 'Could not connect: ' . mysql_error () );
- mysql_query ( "set names gb2312" ); //设定数据字符集
- //echo 'Connected successfully';
- $db = mysql_select_db ( 'get_content', $con ); //实例化数据库连接
- if (! $db) {
- die ( "Can\'t use download : " . mysql_error () );
- //数据库链接异常报错抛出
- } else {
- //获得文件详细信息
- $sql = "SELECT * FROM 'articles' WHERE 'ID' ='" . $aid . "' LIMIT 1 ";
- $result = mysql_query ( $sql, $con );
- $row = mysql_fetch_array ( $result ); //获得内容结果集
- }
- }
- $fck = $_POST ["FCKeditor1"]; //获得修改后的内容变量
- if ($fck != "") {
- $sql1 = "UPDATE 'get_content'.'articles' SET 'Content' = '" . $fck . "' WHERE 'articles'.'ID' ='" . $aid . "' LIMIT 1; ";
- echo $sql1;
- $result = mysql_query ( $sql1, $con );
- if ($result) {
- mysql_free_result ( $result ); //释放结果集
- header("location:list.php"); //重定向浏览器
- }
- }
- ?>
- <!--页面模板部分-->
- <table align=center cellspacing=0 cellpadding=0>
- <form action="articles_save.php?aid=<?php
- echo $aid;
- ?>" method="post">
- <tr class=head>
- <td colspan="2"><?php
- echo $row ['Title']?></td>
- </tr>
- <tr class=line>
- <td width="9%">标题</td>
- <td width="91%"><input name="Title" type="text" class="input"
- id="Title" size="70" value="<?php
- echo $row ['Title']?>" /></td>
- </tr>
- <tr class=line>
- <td valign="top">内容</td>
- <td>
- <?php
- include ("FCKeditor/fckeditor.php"); //加载入口文件
- $oFCKeditor = new FCKeditor ( 'FCKeditor1' ); //建立对象
- $oFCKeditor->BasePath = 'FCKeditor/'; //FCKeditor所在的位置
- $oFCKeditor->ToolbarSet = 'Default'; //工具按钮
- $oFCKeditor->Value = $row ['Content'];
- $oFCKeditor->Create ( 'EditorDefault', '60%', 150 ); //创建工作区域
- ?>
- </td>
- </tr>
- <tr class=line>
- <td colspan="2" align="center"><input name="Submit" type="submit"
- class="btn" value="提交" /> <input name="Submit" type="button"
- class="btn" value="返回" onclick="history.go(-1);" /></td>
- </tr>
- </form>
- </table>
- </div>
广州网站建设
注意:观察以上的代码,这段代码是通过程序获得$_GET[ID]的值(即文章的索引id)将对应文章的内容部分从数据库中取出提交到编辑器中,编辑内容的保存也是同样的原理。运行上面的代码,效果如图8.5所示。
![]() |
| 图8.5 FCKeditor内容编辑器 |




