return!

解决方案 »

  1.   


    以下实例为:测试了文章的添加\修改\删除功能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.viewindex.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.phpvar $default = array(
       'driver' => 'mysql',
       'persistent' => false,
       'host' => 'localhost:3308',
       'login' => 'root',
       'password' => '123456',
       'database' => 'test',
       'prefix' => '',
       'encoding'=>'utf8',
    );
      

  2.   

    这个例子,不错哦。是个基础的CAKEPHP的案例。可以学到如何做一个简单的功能的基本要素
      

  3.   

    最近正在用cakephp做开发,学习了.....
    不过本人用的setFlash()和flash()方法都不起作用,不知道怎么回事能否指点以下呢?
    http://topic.csdn.net/u/20120323/15/d624f34a-52d9-4a23-895d-573119f3badf.html?49501