本篇文章给大家介绍一下用PHP实现文章分类的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
文章分类:同一个新闻 属于 多个菜单项 。比如某新闻既属于校园新闻又属于机构设置,那么点开校园新闻菜单项可以看到这篇文章,点开机构设置也可以看到这篇文章。
两个数据库 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视频教程
以上就是PHP如何实现文章分类的详细内容,更多请关注php中文网其它相关文章!
已抢16988个
抢已抢49931个
抢已抢184235个
抢已抢66074个
抢已抢350392个
抢已抢664162个
抢