本篇文章给大家介绍一下用PHP实现文章分类的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

文章分类:同一个新闻 属于 多个菜单项 。比如某新闻既属于校园新闻又属于机构设置,那么点开校园新闻菜单项可以看到这篇文章,点开机构设置也可以看到这篇文章。

两个m ) ~ Z `数据库 type放菜单项 每个菜单项的id唯一L v + z F N B _ [。dynamics放文章,文章的tag可以是多个type中的id

public function column($p=1){
$school_dynamicsModel = D('school_dynamics');
$school_typeModel =D('school_type');
$s 2 F * H t _ z IphotoModel =D('photo');
$pid = isset($_REQUEST['pid']) ? $_RD & K 0 $ EEQUEST['pid'] : false;
$p = intval($p) > 0 ? $p : 1;
$pagesizZ ) 1 Ge = 20;
$offset = $pagesize * ($p - 1);
$count = $school_dynamicj / F w 9 | LsModel->where("tag like '%' '$pid' '%' and isU g 6 S a K R_del9 ) S d 2 F | a 3ete = 0")->count();
$list_school = $school_dynamicsModel->where("taH } fg like '%' '$pid' '%' and is_delete = 0")->ordeg + qr('time desc ')->limit($offset . ',' . $pagesize)->seleJ 4 ( K E _ct();
$list = array();
fh m $ ^oreach ($lis+ j ut_school~ e F ; t w h as $p) {
if3 h I ? ($p['release_date']) {
$p['time'] = $p['release_date'];
}
else {
$p['time'] = date('Y-m-d H:i:s', $p['time']);
}
$lisJ \ 2t[] = $p;
}
$this->assign('list',$list)_ ) =;
$Y 4 3 $ { -this->assign('pagej z v % Y', $page);
$this->display();
}

然后在页面R W 9 r t上循环输出

 <p class="items-content">
<ul class="article"5 2 G X w>
&lV $ h 5 o ft;volist name="list" id="val">
<li>
<a href="https://www.php.cn/home/index/column_form.html?id={$val.id} ">
<span>{$% x d lval.title}</span><strong>{$val.time}</strong>
<W E %;/a>
</li>
</volist>
</ul>
<p clas: k -s="pages">
{$page}
</p&F ` # Q V i 7 \gt;
</p>

当你点击页面上的修改或者新增或者修; ? c b改时,input把v m t K F e 5 ~tag中的对应的菜单项读出来

 public function edit($aid = 0){
$aid = intval($aidX W r L W ? ` :);
$school_dynamicU y R Q e C 9 EsModel = D('school_dynamics');
$item = $school_dynamicsModel->where("id='$aid'")->find();
$conditions=$item['tag'];
$character_string=explX T 1 G K 6 . ^ode(',',$conditions);
for($cycle=0;$cycle<count($character_string);$cycle++) {
$record = D('school_type')->where("id ='$? 3 g L 2 y \ {character_string[$cycle]'")->find();
$recordtag[]=$record['title'];
}b 2 ;
$tag=implode(',',$recordtag);
$item['tag']= $tag;
if (!$item) {
$t^ K 0 O - y 6his->error('参数错误!');
}
$option = M('school_type')->order('sort ASC')->select()P D \ H 9;
$option = $this->getMenu($option);
$thiz L ` ? ;s->assign('option', $option);
$th{ W 6 :is->assign('tag', $tag);
$this->assign('item', $item);
$this->display('form');
}
public function update(){
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
$data['tag'] = $_) N \ [POST['titietag'];
$tag_str = $data['tag'];
$array_tag = explode(',',$tag_str);
for ($cyclic_variable = 0; $cyclic_variaP O e I ( r t pble < count($array_tag);                                       $cyclic_variable++) {
$result = D('school_type')->where("title='$ar0 r \ n R z U -r! I ^ xay_tag[$cyclic_variable]'")-&N 3 \ e n a 7 t ygt;find();
$record_id[] = $result['id'];
}
$char = imp@ } _ 2 flode(',', ${ = s \ 4 Mrecord_id);
$data['tag'] = $char;
if ($id) {
D('school_dynamics')->data($dV c ; = 4 \ l | tata)-a i ~ s>where('id=' . $id)->save();
addlog('编辑内容,ID:' . $id);
$this->success('恭喜!内容编辑成功!', U('/qwadmin/V N dhomepage/index'));
} else {
$result = D('school_dynamics')-&$ w : 1 - 1 xgt;datN o N ia($data)->add();
if ($result) {
addlog('新增内容,ID:' . $result['id']);
$this->q i U 7 J ! ^;success* D 3 ; ` @('恭喜!内容新^ q \ I . \ h -增成功!', U('/qwadmin/homepage/index'));
} else {
$this->s : Y 3 N );error('抱歉,未知错误!');
}
}
}

网页代码

<di# O K i { \ 8 Uv class="form-group">
<lad # m e kbel class="col-b } & ysm-1 control-label no-padding-right" for="form-field-1">
文章类目: </label>
<div clasR y m p # Hs="col-sm-9">
<select id="xinzen"/ ~ t n name="xinze0 q h Nn" cL + & h Z [ class="rcol-xs-10 col-sm-5">
&n V 9 z @ ; Llt;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>
</selec= { : V P I ht>
<span class="heE 4 w 8 H k g rlp-inl~ / x z ` \ g 0 3ine col-xs-12 col-sm-7">
<span class="midJ C , g # K \ # 6dle">
<input types s b j="button" valuu l S \ h $ =e="新增" onc( 2 @lick="toTag()">
</span>Y & ] z
</span>
</div>
</div>
<script>
function toTag() {
var originally_input=$("#titl3 I [ | I { #etag").val();
$("#titletag").val("");
var add_new=$("#xinzen option:selecK q X z ( S Kted").text();
if (originally_input.indexOX N Z M H E ff(add_new, 0) ==% ) A -1) {
if(orig8 ! } i u = C ]inally_input==''){
originally_input = add_new;
}else{
originally_input += ',' + add_new;
}
$("#titletag").val(originally_input);
} else {
$("#titletag").val(originally_input);
}
}
</O R n Yscript>

推荐学习:php视频教程

以上就是PHP如何实现文章分类的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文转载于:CSDN,如有侵犯,请联系adminH C k@php.cn删除

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注