PHP highlights the number of clicks in the page, click on the number highlighted
Background and requirements
It is like saying that the left side of the page navigation is classified, and there are many links below the category. I would now like to count all the categories below the most clicked one, and to do special treatment of its color!
The approximate data structure is as follows:
{" content": [ { "id": "1", "name": "Htmldrive", "url": "http://www.htmldrive.net/", "Cate ":" Front-end development ", " Clickpoint ":" {"}, { " id ":" 2 ", " name ":" 17 mesh ", " url ":"/HTTP/ www.17sucai.com/", " Cate ":" Front-end development ", " Clickpoint ":" 151 " }, { " id ":" 3 ", " name ":" Ali mom icon Gallery ", " url ":" http://www.iconfont.cn/"," Cate ":" Front-end development ", " Clickpoint ":" 2 " }, { "id": "+", "name": "Animate.css", "url": "http://www.haorooms.com/uploads/example/Animatecss/", " Cate ":" Front-end development ", " Clickpoint ":" + " }, { " id ":" "", " name": "Dafont", "url": "/HTTP/ www.dafont.com/", " Cate ":" Font ", " Clickpoint ":" 1 " }]}
How do you classify the data for highlighting?
Methods and Ideas
The best way to handle this is to align in back-end PHP.
Idea: We can create a new transition array, loop through the original array, create an array of categories, and put the maximum and ID of the hit number in this array. By comparison, get the most clicks in the category of ID and click, and then put the most number of IDs in a new array, loop the original data, through the inarray (), the function to determine the most number of IDs is not in this array, is the case, the data to add a marked field 1, No, it's 0. Then the marked field of 1 is the most clicked in the category.
The JSON output after the adjustment is:
{" content": [ { "id": "1", "name": "Htmldrive", "url": "http://www.htmldrive.net/", "Cate ":" Front-end development ", " Clickpoint ":" "," Max ": 0 }, { " id ":" 2 ", " name ":" 17 mesh ", " url ":" http://www.17sucai.com/", " Cate ":" Front-end development ", " Clickpoint ":" 151 ", " Max ": 1 }, { " id ":" 3 ", " name ":" Ali mom Icon Library ", " url ":" http://www.iconfont.cn/", " Cate ":" Front-end development ", " Clickpoint ":" 2 ", "Max": 0 }, { "id": "+", "name": "Animate.css", "url": "http://www.haorooms.com/uploads/example/Animatecss/", " Cate": "Front-end development", "Clickpoint": "+", "Max": 0 }, { "id": "+", "name": "Dafont", "url": "http://www.dafont.com/", " Cate": "Font", "Clickpoint": "1", "Max": 0 } ] }
The code for PHP 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 that holds the ID of foreach ($temp _arr as $val) {///loop created before the temporary array, hold the classification and the maximum number of clicks $temp _id[] = $val [' id '];//Assign the most-clicked ID to the temporary array} foreach ($res as $key + $vals) {//loop the original data if (In_array ($vals [' id '], $temp _id)) {//If the ID of the most number is in the ID of the original array, the one that adds a field max and sets The value is 1 $res [$key] [' max '] = 1; }else{$res [$key] [' Max '] =0;//otherwise not the most clicked, set to 0}} $result [' content '] = $res; Die (Json_encode ($result)); JSON output exit ();
http://www.bkjia.com/PHPjc/1133043.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133043.html techarticle PHP will click a high number of links in the page to highlight the method, click on the number of highlighting background and demand is like saying that the left side of the page navigation is classified, there are many links below the category ...