首頁 >後端開發 >PHP問題 >PHP如何實作文章分類

PHP如何實作文章分類

醉折花枝作酒筹
醉折花枝作酒筹轉載
2021-05-08 17:51:462189瀏覽

本篇文章為大家介紹一下用PHP實作文章分類的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

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(&#39;school_dynamics&#39;);
        $item = $school_dynamicsModel->where("id=&#39;$aid&#39;")->find();
        $conditions=$item[&#39;tag&#39;];
        $character_string=explode(&#39;,&#39;,$conditions);
        
        for($cycle=0;$cycle<count($character_string);$cycle++) {
	        $record = D(&#39;school_type&#39;)->where("id =&#39;$character_string[$cycle]&#39;")->find();
	        $recordtag[]=$record[&#39;title&#39;];
        }
        
        $tag=implode(&#39;,&#39;,$recordtag);
	    $item[&#39;tag&#39;]= $tag;
	    
        if (!$item) {
            $this->error(&#39;参数错误!&#39;);
        }
        
        $option = M(&#39;school_type&#39;)->order(&#39;sort ASC&#39;)->select();
        $option = $this->getMenu($option);
        $this->assign(&#39;option&#39;, $option);
	    $this->assign(&#39;tag&#39;, $tag);
        $this->assign(&#39;item&#39;, $item);
        $this->display(&#39;form&#39;);
    }

    public function update(){
    	
		    $id = isset($_REQUEST[&#39;id&#39;]) ? $_REQUEST[&#39;id&#39;] : false;
                
            $data[&#39;tag&#39;] = $_POST[&#39;titietag&#39;];

		    $tag_str = $data[&#39;tag&#39;];
		    $array_tag = explode(&#39;,&#39;,$tag_str);
			
		    for ($cyclic_variable = 0; $cyclic_variable < count($array_tag);                                       $cyclic_variable++) {
			
			   
			    $result = D(&#39;school_type&#39;)->where("title=&#39;$array_tag[$cyclic_variable]&#39;")->find();

			    $record_id[] = $result[&#39;id&#39;];
			   
		    }
		    $char = implode(&#39;,&#39;, $record_id);
	   
		    $data[&#39;tag&#39;] = $char;             

		    if ($id) {
			    D(&#39;school_dynamics&#39;)->data($data)->where(&#39;id=&#39; . $id)->save();
			    addlog(&#39;编辑内容,ID:&#39; . $id);
			    $this->success(&#39;恭喜!内容编辑成功!&#39;, U(&#39;/qwadmin/homepage/index&#39;));
		    } else {

			    $result = D(&#39;school_dynamics&#39;)->data($data)->add();
			    if ($result) {
				    addlog(&#39;新增内容,ID:&#39; . $result[&#39;id&#39;]);
				    $this->success(&#39;恭喜!内容新增成功!&#39;, U(&#39;/qwadmin/homepage/index&#39;));
			    } else {
				    $this->error(&#39;抱歉,未知错误!&#39;);
			    }
		    }
	    }

網頁代碼

 
                            <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[&#39;title&#39;]}</option>
                                            <volist name="v.children" id="vv">
                                                <option value="{$vv.id}"
 
                                                >{$vv[&#39;title&#39;]}</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==&#39;&#39;){
          originally_input = add_new;
     }else{
         originally_input += &#39;,&#39; + add_new;
      }
      $("#titletag").val(originally_input);
       } else {
            $("#titletag").val(originally_input);
       }
   }                               
</script>

推薦學習:php影片教學

以上是PHP如何實作文章分類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除