Home > Backend Development > PHP Tutorial > Use php to implement the function of sticking articles to the top

Use php to implement the function of sticking articles to the top

不言
Release: 2023-03-31 20:40:01
Original
3261 people have browsed it

This article mainly introduces the method of php to implement the article top function, and analyzes the operation steps and related implementation techniques of php top function in the form of examples. Friends in need can refer to the following

The examples in this article describe php How to achieve the function of pinning articles to the top. Share it with everyone for your reference, the details are as follows:

Yesterday, the customer asked me to pin the article to the top. I haven't done it before. I did it with the guidance of my colleagues. It was a very simple thing at first, but it took me a long time to do it. I really lack understanding of the program. Let’s write a blog to record it.

The first step is to create two fields in the article table for the pin function. One is the top field, timestamp type, "Definition" is selected by default, and is used to store the time of the pin operation; there is also a flag field, int type, used to determine whether to pin the item to the top. The pin is 1, otherwise it is 0. .

The second step is to modify the template page. Add a top column to the list option, and write the following code in the corresponding line below: {{if $logs[data].flag}} Cancel top{{else}} Top{{/if}}

Just use the flag to determine whether it is pinned.

The pinned js corresponding to the page:

function istop(id,flag){
$.ajax({
type: "POST",
url: "?m={{'article'|encrypt}}&a=savePost",
data: "id="+id+"&flag="+flag,
dataType:"json",
success: function(msg){
if(msg.status == "true")
{
 window.location= '?m={{'article'|encrypt}}';
}
else
{
 alert(msg.message);
}
}
});
}
Copy after login

The third step is to make slight modifications in the program. First sort by whether to stick to the top, then sort by the stick time, and then sort by the default.

$sql .=" ORDER BY flag DESC,top DESC,id DESC ";
Copy after login

(Remember the fields we added?)

This completes the function of pinning the article to the top.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Usage of remainder and remainder in PHP

How to generate promotion posters in PHP

Use PHP to implement the function of packaging zip and downloading

The above is the detailed content of Use php to implement the function of sticking articles to the top. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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