转载注明:
PHP开发资源网(http://www.phpres.com)
文章标题:
Smarty+FCKeditor新闻系统实例教程
关键字:
Smarty实例 Smarty教程 Smarty学习
首先是站点结构(是不是很简单啊~~~):
[FCKeditor] (FCKeditorWEB在线编辑器)
[smarty] (smarty模板
引擎)
[template] (
template 模板文件目录)
global.php
index.php
login.php
mysql.php
new.sql
1.首先大家先new.sql这个文件,创建两个表:新闻和用户表, 并增加一些新闻测试数据。
复制内容到剪贴板
代码:
-- phpMyAdmin SQL Dump
-- version 2.8.2.4
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2006 年 12 月 18 日 02:57
-- 服务器版本: 5.0.24
-- PHP 版本: 5.1.6
--
-- 数据库: `new`
--
-- --------------------------------------------------------
--
-- 表的结构 `news`
--
CREATE TABLE `news` (
`id` int(11) NOT NULL auto_increment,
`author` varchar(100) NOT NULL,
`title` varchar(200) NOT NULL,
`content` mediumtext NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- 导出表中的数据 `news`
--
INSERT INTO `news` (`id`, `author`, `title`, `content`, `date`) VALUES (2, '2111', 'sam', 'sam', '2006-12-18 10:53:24'),
(3, 'admin', '33', '33333333333333', '2006-12-18 10:50:05'),
(8, 'admin', 'sam', 'sam', '2006-12-18 10:50:00');
-- --------------------------------------------------------
--
-- 表的结构 `user`
--
CREATE TABLE `user` (
`id` int(20) NOT NULL auto_increment,
`username` varchar(100) NOT NULL,
`passwd` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- 导出表中的数据 `user`
--
INSERT INTO `user` (`id`, `username`, `passwd`) VALUES (1, 'admin', 'admin');2.然后修改数据库连接文件,把mysql用户名和密码改成你自己的。
mysql.php代码如下:
复制内容到剪贴板
代码:
<?php
class mysql
{
var $db_host = 'localhost';
var $db_username= 'root';
var $db_password= '123456';
var $db_database= 'new';
function connect() {
$db = new mysqli($this-> db_host,$this-> db_username,$this-> db_password,$this-> db_database);
if (mysqli_connect_errno()) {
echo "连接数据库失败!";
exit;
}
return $db;
}
function query_exec($query) {
$db = $this-> connect();
$result = $db-> query($query);
return $result;
}
}
?>3. 然后做登陆文件login.php:
复制内容到剪贴板
代码:
<?php
require('./mysql.php');
$username=$_REQUEST['username'];
$passwd=$_REQUEST['passwd'];
session_start();
$_SESSION['s_username']=$username;
$query_user="select * from user where username = '$username' and passwd = '$passwd'";
$db=new mysql();//实例化类mysql
$result = $db->query_exec($query_user);//验证用户
$num_results=$result->num_rows;//取得数据库中的记录行
if($num_results==0)
{
echo 'login fail!!';
?>
<p><a href="./template/login.htm">返回登陆</a></p>
<?php
}else{
header("Location: ./index.php");
}
?>如果没有问题就转入新闻管理页了。index.php
复制内容到剪贴板
代码:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require('./global.php');
require('./smarty/libs/Smarty.class.php');
require('./mysql.php');
require('./FCKeditor/fckeditor.php');
$action=$_REQUEST['action'];
//定义一个函数用于调用FCK
function editor($input_name, $input_value)
{
global $smarty;
$editor = new FCKeditor($input_name) ;
$editor->BasePath = "./FCKeditor/";//指定编辑器路径
$editor->ToolbarSet = "Default";//编辑器工具栏有Basic(基本工具),Default(所有工具)选择
$editor->Width = "100%";
$editor->Height = "320";
$editor->Value = $input_value;
$editor->Config['AutoDetectLanguage'] = true ;
$editor->Config['DefaultLanguage'] = 'en' ;//语言
$FCKeditor = $editor->CreateHtml();
$smarty->assign("editor", $FCKeditor);//指定区域
}
switch ($action){
case 'addnewsview':
$smarty= new Smarty();
$smarty->template_dir = './template';
$smarty->compile_dir = './smarty/templates_c';
$smarty->assign('page_title','新建新闻');
$smarty->assign('actionvalue','addnews');
editor('content','');//调用编辑器,并定义文本域名为content(与下面addnews中的$_REQUEST['content']对应
$smarty->display('addnews.htm');
break;
case 'addnews':
$title=$_REQUEST['title'];
$content=$_REQUEST['content'];
$db=new mysql();
$button=$_REQUEST['Submit'];
if(empty($title) || empty($content)){
echo "请填写完成!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php?action=addnewsview\">";
}else{
$sql="insert into news values(id,'admin','$title','$content',NOW())";
$db->query_exec($sql);
echo "操作成功!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php\">";
}
break;
case 'editnewsview':
$smarty= new Smarty();
$smarty->template_dir = './template';
$smarty->compile_dir = './smarty/templates_c';
$smarty->assign('page_title','修改新闻');
$smarty->assign('actionvalue','addnews');
$id=$_REQUEST['id'];
$query="select * from news where id=$id";
$db=new mysql();
$result = $db->query_exec($query);
$rs = $result-> fetch_assoc();
$smarty->assign('title',$rs['title']);
//$smarty->assign('content',$rs['content']);
$smarty->assign('actionvalue','editnews');
$smarty->assign('id',$rs['id']);
editor('content',$rs['content']);
$smarty->display('addnews.htm');
break;
case 'editnews':
$title=$_REQUEST['title'];
$content=$_REQUEST['content'];
$id=$_REQUEST['id'];
$button=$_REQUEST['Submit'];
$db=new mysql();
if ($button=='提交'){
$sql="update news set title='$title',content='$content',date=NOW() where id=$id";
$db->query_exec($sql);
echo "操作成功!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php\">";
}
break;
case 'delnews':
$db=new mysql();
if ($checkbox!="" or count($checkbox)!=0) {
for ($i=0;$i<count($checkbox);$i++){
$db->query_exec("delete from news where id='$checkbox[$i]'");
}
}
echo "操作成功!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php\">";
break;
default:
$smarty= new Smarty();
$smarty->template_dir = './template';
$smarty->compile_dir = './smarty/templates_c';
$smarty->assign('page_title','新闻管理');
$smarty->assign('actionvalue','delnews');
$query="select * from news";
$db=new mysql();
$result = $db->query_exec($query);
while ($rs = $result-> fetch_assoc()) {
$array[]= array("id"=>$rs['id'], "title"=>$rs['title'],"date"=>$rs['date']);
$smarty->assign('news',$array);
}
$smarty->display('index.htm');
}
?>附件附上
下载,有不懂的在下面问,本人一问给你因复。