Home > php教程 > php手册 > 简单的博客和分类关联代码(20140509更新)

简单的博客和分类关联代码(20140509更新)

WBOY
Release: 2016-06-07 11:42:09
Original
1644 people have browsed it

关联代码,包括修改,分页,获取,增加,删除,修改,由于我是新手,写的不是很好,求高手指点修正:)
控制器代码(是Action里的 IndexAction.class.php):<?php <br /> class IndexAction extends Action {<br>     //实例化博客,还有关联分类在Model里<br>     public function index(){<br>            //实例公共模型类<br>            $Blog=D('Blog');<br>            //调用分页类<br>            import('ORG.Util.Page');<br>            //统计分页<br>            $count = $Blog->count();<br>            //创建分页数<br>            $Page = new Page($count,5);<br>            //分页输出<br>            $show = $Page->show();<br>            //赋值给关联查询,并且倒序输出<br>            $list=$Blog->relation(true)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();<br>            //赋值给列表标签<br>            $this->assign('list',$list);<br>            //赋值给分页标签<br>            $this->assign('page',$show);<br>            //创建视图<br>            $this->display();<br>     }<br>     <br>     //删除博客<br>     public function del(){<br>                 // 在模版里修改地址是 __APP__/你的文件(INDEX或者ADMIN)/del/id/{$x.id}<br>          //视图$_GET提交ID<br>          $id =$_GET['id'];<br>          //其中ID传值,如果不写$where,而是$id 会把整个数据<br>          $where = array('id' => $id);<br>          //判断博客删除<br>          if (M('Blog')->where($id)->delete()) {<br>             $this->success('删除成功');<br>          } else {<br>              $this->error('删除失败');   <br>          }  <br>          <br>    }<br>   <br>     //增加博客<br>     Public function add () {<br>     //显示修改数据<br>     $this->cate =$cate = M('cate')->select();<br>     //组装主表数据<br>     $id = $_POST['id'];<br>     $cid = $_POST['cid'];<br>     //tutle博客标题 content博客内容 cid 父级分类关联 cate<br>     $data = array(<br>             'title' => $_POST['title'],<br>             'content' => $_POST['content'],<br>             'cid' => $cid,<br>         );<br>         //增加新数据<br>         if ($bid = M('blog')->add($data)) {<br>                 if (isset($_POST['cid'])) {<br>                     $this->success('添加成功','index');<br>                 } else {<br>                     $this->error('添加失败'); <br>                 } <br>                 } <br>           //创建增加视图     <br>           $this->display();<br>     }<br> <br>     //修改<br>     Public function mod () {<br>                          // 在模版里修改地址是 __APP__/你的文件(INDEX或者ADMIN)/mod/id/{$x.id}<br>         $id=$_GET['id'];<br>         if(!empty($id)){<br>             //获取博客文章标题和内容<br>             $art=M('Blog');<br>             $date=$art->getById($id);<br>             //赋值给博客标签datas<br>             $this->assign('datas',$date);<br>             //获取分类<br>             $cat=M('cate');<br>             $list=$cat->select();<br>             //复制给分类标签clist<br>             $this->assign('clist',$list);<br>         }<br>         //创建修改模版视图<br>         $this->display();<br>     }<br> <br>     //修改提交处理<br>     public function update(){<br>         //create方法<br>         $Form= M('blog');<br>         if($Form->create()) {<br>         $result = $Form->save();<br>         if($result) {<br>              //如果修改成功,跳转到首页<br>              $this->success('操作成功!','index');<br>         }else{<br>              //否则修改错误<br>              $this->error('写入错误!');<br>         } <br>              //否则系统异常错误<br>         }else{<br>         $this->error($Form->getError());<br>         }<br>     }<br> } 模型代码(是Model里的 ,文件名BlogModel.class.php):<?php <br /> class BlogModel extends RelationModel{<br>     protected $_link = array(<br> <br>          'Cate'=>array(<br>               'mapping_type'=> BELONGS_TO,<br>               'class_name'=>'Cate',<br>               'mapping_name'=>'Cate',<br>               'foreign_key'=>'cid',<br>               //只获取关联里name内容<br>               'mapping_fields'=>'name',<br>               //把关联的name拿到父级上<br>               'as_fields'=>'name',<br>               //'as_fields' =>'id:uid,title,name',<br> <br>              ),<br>     <br>     );}<br>     <br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template