How to implement article classification in PHP

醉折花枝作酒筹
Release: 2023-03-09 13:30:02
forward
2104 people have browsed it

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.

How to implement article classification in PHP

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();
    }
Copy after login

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>
Copy after login

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(&#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;);
			    }
		    }
	    }
Copy after login

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[&#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>
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!