PHP to highlight the number of links in the page highlighted by the method _php instance

Source: Internet
Author: User

Background and requirements

It's like saying that the left side of the page navigation is categorized, and there are many links below. I now want to count all the categories below the number of clicks most of the one, and its color to do special treatment!

The approximate data structure is as follows:

{"
  content": [
    {
      "id": "1",
      "name": "Htmldrive",
      "url": "http://www.htmldrive.net/", "
      Cate ": Front-End Development",
      "Clickpoint": "M"
    },
    {
      "id": "2",
      "name": "17 material Net",
      "url": "http:// www.17sucai.com/",
      " Cate ":" Front-End Development ",
      " Clickpoint ":" 151 "
    },
    {
      " id ":" 3 ",
      " name ":" Ali Mother Icon Library ",
      " url ":" http://www.iconfont.cn/",
      " Cate ":" Front-End Development ",
      " Clickpoint ":" 2 "
    },
    {
      "id": "A", "
      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 and highlight the data?

Methods and Ideas

The best way to handle this is to align it in the back-end PHP.

Idea: We can create a new transition array, loop through the original array, create an array of categories, and then put the maximum and ID in the number of clicks into the array. By comparing, get the ID and the number of clicks that are most clicked in the category, 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 times the ID is not in this array, is the case, add a mark to the data field 1, No, that's 0. So the Mark field is 1 is the category of the most clicks.

The JSON output after the adjustment is:

 {"
    content": [
      {
        "id": "1",
        "name": "Htmldrive",
        "url": "http://www.htmldrive.net/", "
        Cate ": Front-End Development",
        "Clickpoint": "M",
       "Max": 0
      },
      {
        "id": "2",
        "name": "17 material Net",
        "url": " http://www.17sucai.com/",
        " Cate ":" Front-End Development ",
        " Clickpoint ":" 151 ",
        " Max ": 1
      },
      {
        " id ":" 3 ",
        " name ":" Ali Mother Icon Library ",
        " url ":" http://www.iconfont.cn/",
        " Cate ":" Front-End Development ",
        " Clickpoint ":" 2 ",
        "Max": 0
      },
      {
        "id": "A", "
        name": "Animate.css",
        "url": "http://www.haorooms.com/uploads/example/Animatecss/",
        "Cate": "Front-End Development",
        "Clickpoint": "", "
        Max": 0
      },
      {
        "id": "the",
        "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 [' cl
    Ickpoint '];
   $temp _arr[$value [' Cate ']][' id '] = $value [' id ']; {}}} $temp _id = Array ();
  Creates a temporary array that holds the ID ($temp _arr as $val) {//The temporary array created before the loop, the $temp _id[] = $val [' id '] = the number of clicks (' ids ') = the value of the most clicked ID assigned to the temporary array foreach ($res as $key => $vals) {//loop the original data if (In_array ($vals [' id '], $temp _id)) {//If the most frequently ID 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;//is not the most 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.