PHP _ php instance

Source: Internet
Author: User
This section describes how PHP can highlight a large number of links on the page. it is mainly inspired by a plug-in WordPress. For more information, see Background and requirements

For example, the left side of the page navigation is classification, and there are many links under the category. Now I want to count one of the most clicks under all categories and make a special color!

The general data structure is as follows:

{"Content": [{"id": "1", "name": "htmldrive", "url": "http://www.htmldrive.net/", "cate ": "front-end development", "clickpoint": "100" },{ "id": "2", "name": "17 material network", "url ": "http://www.17sucai.com/", "cate": "front-end development", "clickpoint": "151" },{ "id": "3", "name ": "Alimama icon Library", "url": "http://www.iconfont.cn/", "cate": "front-end development", "clickpoint": "2" },{ "id ": "53", "name": "Animate.css? 1.1.15 "," url ":" http://www.haorooms.com/uploads/example/Animatecss/ "," cate ":" frontend development "," clickpoint ":" 21 "},{" id ":" 55 ", "name": "Dafont", "url": "http://www.dafont.com/", "cate": "font", "clickpoint": "1"}]}

How to classify and highlight the data?

Methods and ideas

The best way to handle this is to align it in the backend PHP.

Idea: we can create a new transition array, loop through the original array, create an array of categories, and put the maximum value and ID in the number of clicks into this array. Through comparison, get the ID and number of clicks with the most clicks in the category, and put the ID with the most clicks in a new array, loop through the original data, through inarray (), if the function determines whether the most frequently-used ID is in this array, it adds a field marked as 1 to the data. if not, it means 0. If the field is marked as 1, the number of clicks in the category is the most.

After adjustment, the output json is:

{"Content": [{"id": "1", "name": "htmldrive", "url": "http://www.htmldrive.net/", "cate ": "front-end development", "clickpoint": "100", "max": 0 },{ "id": "2", "name": "17 material network ", "url": "http://www.17sucai.com/", "cate": "front-end development", "clickpoint": "151", "max": 1 },{ "id ": "3", "name": "Alimama icon Library", "url": "http://www.iconfont.cn/", "cate": "front-end development", "clickpoint": "2 ", "max": 0 },{ "id": "53", "name": "Animate.css? 1.1.15 "," url ":" http://www.haorooms.com/uploads/example/Animatecss/ "," cate ":" frontend development "," clickpoint ":" 21 "," max ": 0 },{" id ": "55", "name": "Dafont", "url": "http://www.dafont.com/", "cate": "font", "clickpoint": "1 ", "max": 0}]}

The php code is as follows:

$ SQL = "select id, name, url, cate, clickpoint from commonwebsite order by id asc"; $ res = $ db-> getAll ($ SQL ); $ temp_arr = array (); foreach ($ res as $ key => $ value) {if (! Isset ($ temp_arr [$ value ['Cate']) {$ temp_arr [$ value ['Cate'] = array (); $ temp_arr [$ value ['Cate'] ['Max '] = $ value ['clickpoint']; $ temp_arr [$ value ['Cate'] ['id'] = $ value ['id'];} else {if ($ value ['clickpoint']> $ temp_arr [$ value ['Cate'] ['Max ']) {$ temp_arr [$ value ['Cate'] ['Max '] = $ value ['clickpoint']; $ temp_arr [$ value ['Cate'] ['id'] = $ value ['id'] ;}}$ temp_id = array (); // create a temporary array for ID storage foreach ($ temp_arr as $ val) {// The temporary array created before the loop, $ temp_id [] = $ val ['id']; // assign the ID with the most clicks to the temporary array} foreach ($ res as $ key => $ vals) {// loop the original data if (in_array ($ vals ['id'], $ temp_id) {// if the id with the most times is in the ID of the original array, add a field max and set its value to 1 $ res [$ key] ['Max '] = 1 ;} else {$ res [$ key] ['Max '] = 0; // otherwise, it is not the most frequently clicked, set to 0 }}$ result ['content'] = $ res; die (json_encode ($ result); // json output exit ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.