本篇文章為大家介紹一下用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中文網其他相關文章!