前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TP入门第八天

TP入门第八天

作者头像
苦咖啡
发布2018-05-07 17:41:18
7180
发布2018-05-07 17:41:18
举报
文章被收录于专栏:我的博客我的博客

[小型留言板-增删查改]

1、先建立数据库lyb,然后建立表lyb_notes

建表代码如下:

CREATE TABLE `lyb_notes` (

`id` int(10) NOT NULL auto_increment,

`title` varchar(100) collate utf8_bin NOT NULL,

`time` varchar(11) collate utf8_bin NOT NULL,

`content` text collate utf8_bin NOT NULL,

`clientip` varchar(15) collate utf8_bin NOT NULL,

PRIMARY KEY  (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;

2、书写入口文件

<?php

//入口文件

//定义核心文件夹、项目名称和路径

define(‘THINKPHP_PATH’,’./ThinkPHP/’);

define(‘APP_NAME’, ‘lyb’);

define(‘APP_PATH’, ‘./lyb/’);

//开启调试模式

define(‘APP_DEBUG’,true);

// 加载框架入口文件

require_once (THINKPHP_PATH.”ThinkPHP.php”);

?>

3、编写配置文件

<?php

if(!defined(‘THINKPHP_PATH’)){echo “非法错误!”;exit;}

return array(

//’配置项’=>’配置值’

//很多选项都是默认配置都可以,这里基本配置项目把用的配置都列举出来了

‘URL_MODEL’=>1,//0是普通模式,1是pathinfo模式(默认配置),2是rewrite模式,3是兼容模式

‘DB_TYPE’               => ‘mysql’,     // 数据库类型

‘DB_HOST’               => ‘localhost’, // 服务器地址

‘DB_NAME’               => ‘lyb’,       // 数据库名

‘DB_USER’               => ‘root’,      // 用户名

‘DB_PWD’                => ‘joyous’,    // 密码

‘DB_PORT’               => ‘3306’,      // 端口

‘DB_PREFIX’             => ‘lyb_’,      // 数据库表前缀

‘DB_CHARSET’            => ‘utf8’,      // 数据库编码默认采用utf8

‘APP_DEBUG’=>true,

);

?>

4、修改index方法

在lib/action/里面找到indexaction.class.php

<?php

class IndexAction extends Action {

public function index(){

header(“Content-Type:text/html; charset=utf-8”);

$notes=M(‘Notes’);

$list=$notes->select();

$this->assign(‘title’,’Joyous-小型留言板’);

$this->assign(‘mylist’,$list);

$this->display();

}

//php默认方法为public方法

//增加

function add()

{

$note=M(‘notes’);

if($vo=$note->create()){

$note->time=time();//获取时间

$note->clientip=get_client_ip();//获取客户端ip

if($note->add()){

$this->success(“信息添加成功,返回上一页”);

}else{

$this->error(‘信息添加失败,返回上一页’);

}

}else{

$this->error($note->getError());

}

}

//删除

function del()

{

$note=M(‘Notes’);

if($note->delete($_GET[‘id’])){

$this->success(‘删除成功’);

}else{

$this->error(‘删除失败’);

}

}

//更新

function edi()

{

$note=M(‘Notes’);

$id=(int)$_GET[‘id’];

$list=$note->where(“id=$id”)->find();//这里只要一条信息

$this->assign(‘list’,$list);

$this->assign(‘title’,’修改信息’);

$this->display();

}

//

function update()

{

$note=M(‘notes’);

if($note->create()){

$note->time=time();

if($note->save()){

$this->success(‘更新成功’);

}else{

$this->error(‘更新失败’);

}

}

}

}

5、运行之后提示没有模板

注意:TP3.0的模板省去了default,也就是说直接存在tpl目录下

建立index.html

<html>

<head>

<title>{$title}</title>

</head>

<body>

<form action=”__URL__/add/” method=”post”>

留言标题:<input name=”title” type=”text”><br>

留言内容:<textarea name=”content” cols=”20″ rows=”5″>

</textarea><br>

<input name=”sub”  value=”发布” type=”submit”>

<!–数据库留言列表 –>

<hr><!– 美丽的分割线 –>

<!– 这里说下模板标签volist,其中name的值是在控制器里赋值了,而id是本次循环的临时变量–>

<volist name=’mylist’ id=’vo’>

序号:{$vo[‘id’]}<br>

标题:{$vo[‘title’]}<br>

内容:{$vo.content}<br>

时间:{$vo[‘time’]|date=”Y-m-d”,###} <br>

<a href=’__URL__/del/id/{$vo[‘id’]}’>删除</a> <a href=’__URL__/edi/id/{$vo.id}’>修改 </a>

<hr>

</volist>

</form>

</body>

</html>

建立edi.html

<html>

<head><title>{$title}</title>

<body>

<form action=”__URL__/update” method=”post”>

标题:<input name=’title’ value='{$list.title}’ type=’text’><br>

<input name=’id’ type=’hidden’ value='{$list.id}’>

内容:<input name=’content’ value='{$list.content}’ type=’text’><br>

<input name=’sub’ value=’更新’ type=’submit’>

</form>

</body>

</head></html>

6、疑问讲解

注意:获取ip在3.0中可以直接使用(get_client_ip()),不需要导入什么类库

注意:存储的时候我用了time获取时间戳,但是显示的时候用的是date函数,其中

时间:{$vo[‘time’]|date=”Y-m-d”,###}可以查看手册中8.3中使用函数章节进行学习

注意:volist标签是8.9章节知识(随后会详细讲解volist标签)

注意:success.html模板是在tpl目下存放

附:压缩包是所有的代码需要手册的可以去官方下载或者Q我

代码示例TP3.0

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012年8月8日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档