Examples of PHP processing JSON data

Source: Internet
Author: User

Recent work needs to be on the basis of the original static kanban (large screen) page, to achieve dynamic display of data, and need to refresh regularly.
After receiving the task, we start to modify the page:
Obviously the best solution to this requirement is to use AJAX to make timed requests for background data and refresh on the front page

The basic effect is like this:

Iterate over the background data and loop through the table.

To be honest, it's really easy to do an AJAX back-end request for data, but it's exciting to have a module that doesn't have a background interaction for me to test, and as an intern I can't possibly greet someone else to help me complete the test.
So I still have to rely on myself.
Then I can only rely on my blogging system to accumulate some of the PHP experience to write a back-end module, independent testing work.

The code is as follows:

<?php//Even basic meta-data is my own shot of the brain simulation, a little worship I sent a few!                $json = ' {' data ': [{' Top_line ': 10000, ' Bottom_line ': 500, "Now_number": 5121, "This_mouth_takein": 0, "This_mouth_takeout": 0, "Last_mo                Uth_takein ": 0," Last_mouth_takeout ": 0," Raw_material_batch_number_ ":" SDS-7120 ", "Own_adress": "GEM", "material_name_": "Real Axis"}, {"Top_line": 20000                , "Bottom_line": Now_number, "This_mouth_takein": 0, "This_mouth_takeout": 0, "Last_mouth_takein": 0, "Last_mouth_takeout": 0, "RA            W_material_batch_number_ ":" SDS-7120 "," own_adress ":" GEM "," material_name_ ":" Outer star "          }, {"Top_line": 30000, "bottom_line": 5000,      "Now_number": 5121, "This_mouth_takein": 0, "This_mouth_takeout": 0, "last                _mouth_takein ": 0," Last_mouth_takeout ": 0," Raw_material_batch_number_ ":" SDS-7120 ", "Own_adress": "GEM", "material_name_": "Real Axis"}, {"Top_line": 40                "Bottom_line": "Now_number": "This_mouth_takein": 0,                "This_mouth_takeout": 0, "Last_mouth_takein": 0, "Last_mouth_takeout": 0,            "Raw_material_batch_number_": "SDS-7120", "own_adress": "GEM", "material_name_": "Outer star Wheel" }, {"Top_line": 50000, "Bottom_line":, "Now_number": 51                , "This_mouth_takein": 0, "This_mouth_takeout": 0, "Last_mouth_takein": 0, "Last_mouth_Takeout ": 0," Raw_material_batch_number_ ":" SDS-7120 "," own_adress ":" GEM "," M                Aterial_name_ ":" Real Axis "}, {" Top_line ": 60000," bottom_line ": 5000,                "Now_number": 5121, "This_mouth_takein": 0, "This_mouth_takeout": 0,                "Last_mouth_takein": 0, "Last_mouth_takeout": 0, "Raw_material_batch_number_": "SDS-7120", "Own_adress": "GEM", "material_name_": "Real Axis"}, {"Top_line                ": 20000," Bottom_line ":" Now_number ": 5121," This_mouth_takein ": 0,                "This_mouth_takeout": 0, "Last_mouth_takein": 0, "Last_mouth_takeout": 0,            "Raw_material_batch_number_": "SDS-7120", "own_adress": "GEM", "material_name_": "Real axis"            },{"Top_line": 20000, "Bottom_line": "Now_number": 5121, " This_mouth_takein ": 0," This_mouth_takeout ": 0," Last_mouth_takein ": 0," Last_mo                Uth_takeout ": 0," Raw_material_batch_number_ ":" SDS-7120 "," own_adress ":" GEM ",                "Material_name_": "Real Axis"}, {"Top_line": 20000, "Bottom_line": 5000,                "Now_number": 5121, "This_mouth_takein": 0, "This_mouth_takeout": 0,                 "Last_mouth_takein": 0, "Last_mouth_takeout": 0, "Raw_material_batch_number_": "SDS-7120", "Own_adress": "GEM", "material_name_": "Real Axis"}, {"Top_                Line ": 20000," Bottom_line ":" Now_number ": 5121," This_mouth_takein ": 0, "This_mouTh_takeout ": 0," Last_mouth_takein ": 0," Last_mouth_takeout ": 0," Raw_material_b            Atch_number_ ":" SDS-7120 "," own_adress ":" GEM "," material_name_ ":" Real Axis "},                {"Top_line": 20000, "Bottom_line": "Now_number": 3000, "This_mouth_takein": 0, "This_mouth_takeout": 0, "Last_mouth_takein": 0, "LAS                T_mouth_takeout ": 0," Raw_material_batch_number_ ":" SDS-7120 "," own_adress ":" GEM ",    "Material_name_": "Outer Star Wheel"}], "Success": true, "Total": 2} ';    Ajax passed the parameters, page is the page number, LISTNUM is the number of data displayed per page $page = $_get[' page ');    $listNum = $_get[' listNum ');    This function is quite important json_decode (), because actually $json we define a string, this function converts it into JSON-formatted data $jsondata =json_decode ($json); Define header file to prevent garbled header ("content-type:text/html;    Charset=utf-8 "); Print_r ($jSondata);     So we can get the array we want $arr = $jsondata->data;    Echo $arr;    The following is the effort to split the background data: $sign is to determine how many pages the total data can be divided into $sign = Intval (count ($arr)/$listNum) +1;    If the page number is more than the total page number, I am sorry, intelligent mode, so as to continue to loop if ($page >= $sign) {$page = $page% $sign;    }//array_slice (Array,start,length), the method by which PHP intercepts arrays $toget = Array_slice ($arr, $page * $listNum, $listNum);     Return data echo Json_encode ($toget); ?>

By the way, one of the things that our boss gives me when I dispatch a mission:

  • My original idea was to use an AJAX request to get all the background data, and then the front-end paging loop (which might be a common thought of front-end developers), this idea seems to be no problem.
  • Because our systems are deployed to the industrial production site, network latency and real-time data are the two issues that have to be considered, so this time you have to reduce the load on each request and consider the effectiveness of the data.
  • This clearly makes it more reliable to request data through small batches of multiple frequencies.
  • So with the current solution.

To facilitate the reader to understand the whole idea, attach the AJAX request part of the code:

Window.onload=function () {var page = 0;    var listNum = 6;    Console.log (' 000 ');    Getajax (Page,listnum);    Getajax (Page,listnum);        SetInterval (function () {page++;        Getajax (Page,listnum);                Console.log (page);            Getajax (Page,listnum);    },3000);        function Getajax (page,listnum) {var args = {"page":p Age, "ListNum": ListNum, "Time": New Date ()}; $.getjson ('..            /json/storage_rawinventeryview.php ', args,function (result) {//console.log (' 123 ');            var arr = result;                Console.log (arr);//debug data//Change color special variable var markup = true;                var color;             Adjust the order of the data//iterate through the JSON to insert the obtained data into the page $ (' tbody '). empty ();                for (var i = 0; i < arr.length; i++) {$ (' tbody '). Append (' <tr></tr> ');                markup =!markup;                if (markup) {color = ' #58D5FF ';}else{color = ' #fff '; }//Adjust the order of the data var turnlist = ["Raw_material_batch_number_", "Material_name_", "Top_line", "BOTTOM _line "," Now_number "," Last_mouth_takeout "," This_mouth_takein "," This_mouth_takeout "," Last_mouth_takein "," OWN_                                ADRESS "];                    for (var q = 0;q < turnlist.length; q++) {//console.log (turnlist[q]); if (arr[i][turnlist[q]]== ' is ') {var tdhtml = ' <td style= ' word-wrap:break-word;text-align:center;f                    Ont-size:2.5vw;color: #1DFC8A; "class=" ng-binding ng-scope "> Qualified </td>"; }else if (arr[i][turnlist[q]]== ' no ') {var tdhtml = ' <td style= ' word-wrap:break-word;text-align:ce                    Nter;font-size:2.5vw;color: #F3423C; "class=" ng-binding Ng-scope "> returns </td>"; }else{var tdhtml = ' <td style= ' Word-wrap:break-word;text-align:center;font-size:2.5vw;color: ' +color+ '; "class=" ng-binding ng-scope "> ' +arr[i][turnlist[q]]+ ' </td> ';                    }//console.log (tdhtml);                $ (' Tbody>tr:eq (' +i+ ') '). Append (tdhtml);    }             }        }); }    }

Examples of PHP processing JSON data

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.