散分,求一个cakePhp小实例,一定以要能用!

llj480028 2010-01-20 10:12:23
return!
...全文
421 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sara 2012-03-23
  • 打赏
  • 举报
回复
最近正在用cakephp做开发,学习了.....
不过本人用的setFlash()和flash()方法都不起作用,不知道怎么回事能否指点以下呢?
http://topic.csdn.net/u/20120323/15/d624f34a-52d9-4a23-895d-573119f3badf.html?49501
woshizhongguorenna 2010-01-22
  • 打赏
  • 举报
回复
学习了
z445619791 2010-01-20
  • 打赏
  • 举报
回复
jf
llj480028 2010-01-20
  • 打赏
  • 举报
回复
关键是好多 东西不对的,我还的修改,估计是代码不全!
llj480028 2010-01-20
  • 打赏
  • 举报
回复
我这倒是有一个,就是太乱了!
看着头痛!!
谢谢1 楼!
云轩2013 2010-01-20
  • 打赏
  • 举报
回复
这个例子,不错哦。是个基础的CAKEPHP的案例。可以学到如何做一个简单的功能的基本要素
liuahuilele 2010-01-20
  • 打赏
  • 举报
回复
我jf
iwantnet 2010-01-20
  • 打赏
  • 举报
回复

以下实例为:测试了文章的添加\修改\删除功能

1.数据表:

CREATE TABLE `news` (
`id` int(5) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`content` text NOT NULL,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
`sort` int(2) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `sort` (`sort`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=24 ;

--
-- 导出表中的数据 `news`
--

INSERT INTO `news` VALUES (3, '我的温带那个', '我带那个', '2009-04-29 00:00:00', 2);
INSERT INTO `news` VALUES (19, 'gyuyu', '我的温带那个我的的温带那个', '2009-04-29 11:12:00', 0);
INSERT INTO `news` VALUES (23, 'hhhhhhhhhhhh', 'nfssssssssssssssssss', '2009-04-27 02:18:21', 0);

2.models

<?php
//app/models/new.php
class New extends AppModel{
var $name='New';
}
?>

3.controllers

<?php
//app/controllers/news_controller.php
class NewsController extends AppController
{

var $uses=array('news'); //使用news这个模型

//定义index.thtml,在这里定义了index函数
//我们就可以通过http://localhost/news/index来访问了此函数,以下同理
function index(){
$this->set('lists',$this->news->findAll());
}

//显示单一信息
function view($id=null){
$this->news->id=$id;
$this->set('news',$this->news->find());
}

//添加/修改
function write($id=0) {

if($id){
$this->set('id',$id);
$this->news->id=$id;
if(empty($this->data))
{
$this->set('news',$this->news->find());
}
}else{
$this->set('news',false);
}

if (!empty($this->data)) {

if(empty($this->data["news"]["title"])){
$this->flash("对不起,标题不能为空!","javascript:history.go(-1);");
}
else
{
if ($this->news->save($this->data)) {
if($id){
$this->flash("数据修改成功!","javascript:history.go(-1);");
}else{
$this->redirect(array('action'=>'index'), null, true); //数据添加成功
}

} else {
$this->flash("数据上传失败,稍后再试!","javascript:history.go(-1);");
} }
}
} //创建删除文章的action,


//删除
function delete($id) {
$this->news->id=$id;
$ret=$this->news->remove();
if($ret){
$this->redirect(array('action'=>'index'), null, true);
} else{
$this->flash('删除失败','/News/');
}
}
}

?>

4.view

index.html

<?php
//app/views/news/index.html
foreach ($lists as $lists):
//显示文章时间
echo $lists['news']['time'].' ';

//显示标题,带文章连接
echo $html->link( $lists['news']['title'], "/news/view/".$lists['news']['id'],array('target'=>"blank",'title'=>"属性数组")).'<br/>';

//图片放在 app/webroot/img/ 目录中,是相对目录
echo $html->image('cake.icon.gif', array('alt'=>"测试图片显示", 'border'=>"0")) ;

//建立编辑的连接
echo $html->link('编辑', "/news/write/".$lists['news']['id']).' ';

//建立删除的连接
echo $html->link('删除', "/news/delete/".$lists['news']['id'],null,'确认删除?').' ';

//以图片做为超级连接
echo $html->link($html->image('cake.icon.gif',array('alt'=>"图片显示超级连接", 'border'=>"0")),"/news/view/".$lists['news']['id'], null, null, false) ;


echo '<br/><br/>';

endforeach;

echo '<hr style="color:#FF0000;height: 2px;width:1000px">';
?>

<p><?php echo $html->link('增加新文章', '/news/write',array('target'=>"blank")); ?></p>

view.html

<!-- app/views/news/index.html -->

title:<?php echo $news['news']['title'];?>
<hr size="1" style="color:#CC0000">
content:<?php echo $news['news']['content'];?>

write.html

<!-- app/views/news/write.html -->

<h1>文章添加</h1>
<?php
echo $form->create('news', array('action'=>'write','method'=>"post"));
echo $form->input('title', array('type'=>"text",'size' => "50",'label'=>"标题",'value'=>$news['news']['title']));
echo $form->textarea('content', array('cols' => '60', 'rows' => '6','value'=>$news['news']['content']));
$tmpdate = ($news['news']['time'])?$news['news']['time']:date("Y-m-d H:i:s");
echo $form->input('time', array('type'=>"text",'size' => "15",'value'=>$tmpdate ,'label'=>"时间" ));
echo $form->input('id', array('type'=>"hidden",'value'=>$news['news']['id'] ));
echo $form->end('确认提交');
?>

5.默认显示效果 // app/views/layouts/default.thml
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php echo $html->charset();?>
</head>
<BODY>
<?php echo $content_for_layout ;?>
</BODY>
</HTML>

==========================================

注:要让上述代码正确运行,不要忘了修改:

//app\config\database.php

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost:3308',
'login' => 'root',
'password' => '123456',
'database' => 'test',
'prefix' => '',
'encoding'=>'utf8',
);
ProgrammerNO1 2010-01-20
  • 打赏
  • 举报
回复
好东东
llj480028 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xuzuning 的回复:]
把简单的东西弄复杂了
[/Quote]
现在没什么事可做,研究一下!呵呵
xuzuning 2010-01-20
  • 打赏
  • 举报
回复
把简单的东西弄复杂了

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧