0 Comments

内容编辑器

发布于:2012-11-21  |   作者:广州网站建设  |   已聚集:人围观
内容编辑器

内容编辑器被广泛地应用在各种信息管理后台,它的作用是在线查看和编辑HTML代码和脚本程序。在本章中使用内容编辑器(FCKeditor)对文章内容进行预览和编辑。

FCKeditor是一个专门使用在网页上的文字编辑器,属于开放源代码的所见即所得形式,它是轻量级、不需要复杂安装过程的文字编辑器。下面来了解FCKeditor在PHP中的配置方法及使用。广州网站设计

1.下载及解压

从下面的地址下载新版本FCKeditor:

广州网站设计
  1. http://www.fckeditor.net/download 

将下载后的文件解压到Apache的Web发布目录www/中。

2.调用FCKeditor

新建一个PHP文件,并在程序中加载FCKeditor文件include("FCKeditor/fckeditor.php"),然后加入初始化FCKeditord的各项配置。初始化及配置选项代码如下:


  1. <?php 
  2. //功能:FCK初始化及配置  
  3. $oFCKeditor = new FCKeditor ( 'FCKeditor1' );       //建立对象  
  4. $oFCKeditor->BasePath = 'FCKeditor/';               //FCKeditor所在的位置  
  5. $oFCKeditor->ToolbarSet = 'Default';                //工具按钮  
  6. $oFCKeditor->Create ( 'EditorDefault', '60%', 150 );  
  7. ?> 

【代码解读】

以上的代码实例化编辑器类,并设置了FCKeditor编辑器的基本参数。通过$oFCKeditor->Value的方法,将文章内容部分输出到编辑器中。当编辑器中的内容改变后,再将内容变量更新到对应的数据库字段中,完成文本预览和编辑。完成上述功能的完整代码如下:

广州网站建设
  1. <?php 
  2. $aid = $_GET ['aid'];                               //文章索引id  
  3. if ($aid) {  
  4.     $con = mysql_connect ( 'localhost', 'root', '198251' ) or die ( 'Could  not connect: ' . mysql_error () );  
  5.     mysql_query ( "set names gb2312" );         //设定数据字符集  
  6.     //echo 'Connected successfully';  
  7.     $db = mysql_select_db ( 'get_content', $con );  //实例化数据库连接  
  8.     if (! $db) {  
  9.         die ( "Can\'t use download : " . mysql_error () );  
  10.                                                     //数据库链接异常报错抛出  
  11.     } else {  
  12.         //获得文件详细信息  
  13.         $sql = "SELECT * FROM 'articles' WHERE 'ID' ='" . $aid . "' LIMIT 1 ";  
  14.         $result = mysql_query ( $sql, $con );  
  15.         $row = mysql_fetch_array ( $result );       //获得内容结果集  
  16.     }  
  17. }  
  18.  
  19. $fck = $_POST ["FCKeditor1"];                       //获得修改后的内容变量  
  20. if ($fck != "") {  
  21.     $sql1 = "UPDATE 'get_content'.'articles' SET 'Content' = '" . $fck . "'     WHERE 'articles'.'ID' ='" . $aid . "' LIMIT  1; ";  
  22.     echo $sql1;  
  23.     $result = mysql_query ( $sql1, $con );  
  24.     if ($result) {  
  25.         mysql_free_result ( $result );              //释放结果集  
  26.     header("location:list.php");                    //重定向浏览器  
  27.     }  
  28. }  
  29. ?> 
  30. <!--页面模板部分--> 
  31. <table align=center cellspacing=0 cellpadding=0> 
  32.     <form action="articles_save.php?aid=<?php 
  33.     echo $aid;  
  34.     ?>method="post"> 
  35.     <tr class=head> 
  36.         <td colspan="2"><?php 
  37.         echo $row ['Title']?></td> 
  38.     </tr> 
  39.     <tr class=line> 
  40.         <td width="9%">标题</td> 
  41.         <td width="91%"><input name="Title" type="text" class="input" 
  42.             id="Title" size="70" value="<?php 
  43.             echo $row ['Title']?>/></td> 
  44.     </tr> 
  45.     <tr class=line> 
  46.         <td valign="top">内容</td> 
  47.         <td> 
  48. <?php 
  49. include ("FCKeditor/fckeditor.php");                //加载入口文件  
  50. $oFCKeditor = new FCKeditor ( 'FCKeditor1' );       //建立对象  
  51. $oFCKeditor->BasePath = 'FCKeditor/';               //FCKeditor所在的位置  
  52. $oFCKeditor->ToolbarSet = 'Default';                //工具按钮  
  53. $oFCKeditor->Value = $row ['Content'];  
  54. $oFCKeditor->Create ( 'EditorDefault', '60%', 150 );    //创建工作区域  
  55. ?> 
  56. </td> 
  57.     </tr> 
  58.     <tr class=line> 
  59.         <td colspan="2" align="center"><input name="Submit" type="submit" 
  60.             class="btn" value="提交" /> <input name="Submit" type="button" 
  61.             class="btn" value="返回" onclick="history.go(-1);" /></td> 
  62.     </tr> 
  63.     </form> 
  64. </table> 
  65. </div> 

广州网站建设

注意:观察以上的代码,这段代码是通过程序获得$_GET[ID]的值(即文章的索引id)将对应文章的内容部分从数据库中取出提交到编辑器中,编辑内容的保存也是同样的原理。运行上面的代码,效果如图8.5所示。
图8.5  FCKeditor内容编辑器
标签:
飞机