This article will introduce to you how to use PHP to classify articles. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Article category: The same news belongs to multiple menu items. For example, if a certain news belongs to both campus news and institutional settings, you can click on the campus news menu item to see this article, and click on the institutional settings to see this article.
Two database types put menu items. Each menu item has a unique id. Dynamics puts an article. The tag of the article can be the ID of multiple types
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(); }
and then the output is looped on the page
<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>
When you click Modify or Add or Modify on the page, input Read out the corresponding menu item in the 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('抱歉,未知错误!'); } } }
Web page code
<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>
Recommended learning:php video tutorial
The above is the detailed content of How to implement article classification in PHP. For more information, please follow other related articles on the PHP Chinese website!