这篇文章主要介绍PHP怎么实现文章分类,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1、执行速度快。2、具有很好的开放性和可扩展性。3、PHP支持多种主流与非主流的数据库。4、面向对象编程:PHP提供了类和对象。5、版本更新速度快。6、具有丰富的功能。7、可伸缩性。8、功能全面,包括图形处理、编码与解码、压缩文件处理、xml解析等。
文章分类:同一个新闻 属于 多个菜单项 。比如某新闻既属于校园新闻又属于机构设置,那么点开校园新闻菜单项可以看到这篇文章,点开机构设置也可以看到这篇文章。
两个数据库 type放菜单项 每个菜单项的id唯一。dynamics放文章,文章的tag可以是多个type中的id
public function column($p=1){ $school_dynamicsModel = D('school_dynamics'); $school_typeModel =D('school_type'); $photoModel =D('photo'); $pid = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : false; $p = intval($p) > 0 ? $p : 1; $pagesize = 20; $offset = $pagesize * ($p - 1); $count = $school_dynamicsModel->where("tag like '%' '$pid' '%' and is_delete = 0")->count(); $list_school = $school_dynamicsModel->where("tag like '%' '$pid' '%' and is_delete = 0")->order('time desc ')->limit($offset . ',' . $pagesize)->select(); $list = array(); foreach ($list_school as $p) { if ($p['release_date']) { $p['time'] = $p['release_date']; } else { $p['time'] = date('Y-m-d H:i:s', $p['time']); } $list[] = $p; } $this->assign('list',$list); $this->assign('page', $page); $this->display(); }
然后在页面上循环输出
<p class="items-content"> <ul class="article"> <volist name="list" id="val"> <li> <a href="/home/index/column_form.html?id={$val.id} "> <span>{$val.title}</span><strong>{$val.time}</strong> </a> </li> </volist> </ul> <p class="pages"> {$page} </p> </p>
当你点击页面上的修改或者新增或者修改时,input把tag中的对应的菜单项读出来
public function edit($aid = 0){ $aid = intval($aid); $school_dynamicsModel = D('school_dynamics'); $item = $school_dynamicsModel->where("id='$aid'")->find(); $conditions=$item['tag']; $character_string=explode(',',$conditions); for($cycle=0;$cycle<count($character_string);$cycle++) { $record = D('school_type')->where("id ='$character_string[$cycle]'")->find(); $recordtag[]=$record['title']; } $tag=implode(',',$recordtag); $item['tag']= $tag; if (!$item) { $this->error('参数错误!'); } $option = M('school_type')->order('sort ASC')->select(); $option = $this->getMenu($option); $this->assign('option', $option); $this->assign('tag', $tag); $this->assign('item', $item); $this->display('form'); } public function update(){ $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false; $data['tag'] = $_POST['titietag']; $tag_str = $data['tag']; $array_tag = explode(',',$tag_str); for ($cyclic_variable = 0; $cyclic_variable < count($array_tag); $cyclic_variable++) { $result = D('school_type')->where("title='$array_tag[$cyclic_variable]'")->find(); $record_id[] = $result['id']; } $char = implode(',', $record_id); $data['tag'] = $char; if ($id) { D('school_dynamics')->data($data)->where('id=' . $id)->save(); addlog('编辑内容,ID:' . $id); $this->success('恭喜!内容编辑成功!', U('/qwadmin/homepage/index')); } else { $result = D('school_dynamics')->data($data)->add(); if ($result) { addlog('新增内容,ID:' . $result['id']); $this->success('恭喜!内容新增成功!', U('/qwadmin/homepage/index')); } else { $this->error('抱歉,未知错误!'); } } }
网页代码
<div class="form-group"> <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 文章类目: </label> <div class="col-sm-9"> <select id="xinzen" name="xinzen" class="rcol-xs-10 col-sm-5"> <volist name="option" id="v"> <option value="{$v.id}" >{$v['title']}</option> <volist name="v.children" id="vv"> <option value="{$vv.id}" >{$vv['title']}</option> </volist> </volist> </select> <span class="help-inline col-xs-12 col-sm-7"> <span class="middle"> <input type="button" value="新增" onclick="toTag()"> </span> </span> </div> </div> <script> function toTag() { var originally_input=$("#titletag").val(); $("#titletag").val(""); var add_new=$("#xinzen option:selected").text(); if (originally_input.indexOf(add_new, 0) == -1) { if(originally_input==''){ originally_input = add_new; }else{ originally_input += ',' + add_new; } $("#titletag").val(originally_input); } else { $("#titletag").val(originally_input); } } </script>
以上是“PHP怎么实现文章分类”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。