PHP takes turns to sort, every once in a while to sort by position, poll the leaderboard: function Datapollinginterval ()

Source: Internet
Author: User
Tags rounds set time

/** @ name: PHP, the array every time (from the set time) to rotate the position of the ranking, polling the leaderboard. Rounds the list of data to a specified second or minute or hour or day. * @ parameter: (array) $list sequential array, passing in an array that needs to be wheeled; * @ parameter: (time string) $polling the _time interval, the interval between rotations. Can be: number + seconds, minutes, hours, days (English words); * @ parameter: (int) $polling _number The number of rows per rotation of the data; * @ return value: Array | False if the sort returns an array successfully, otherwise the exception condition returns false.** @author: Wangji sparse, qq:876635409*/functionDatapollinginterval ($list,$polling _time= ' Ten second minute Hour day ',$polling _number=1 ) {    //parameters for planning polling interval time:    $interval=false; //determine the type of $polling_time is seconds, minutes, hours, days of which 1.     $arg=Array(         ' s ' =>1,//seconds' m ' =>60,//minutes = sec' H ' =>3600,//Time = 3600 sec' d ' = 86400,//Day = 86400 sec    ); //determine the type of interval time and calculate the interval time    foreach($arg  as $k=$v ) {        if(false!==Stripos($polling _time,$k ) ) {            $interval=intval($polling _time) *$v;  Break; }    }        //Judging interval time    if( !Is_int($interval ) ){          return false; }    //number of seconds from the beginning of the year    $this _year_begin_second=Strtotime(Date(' y-01-01 01:00:01 ', Time() ) ); //Current number of seconds-the number of seconds to start this year, and the number of seconds to date this year.     $polling _time= Time() -$this _year_begin_second; //number of seconds from this year to date, calculated to get the current number of rounds    $len=Count($list);//Total length    $start _index=intval($polling _time/$interval ); $start _index=$polling _number*$start _index%$len;//Number of wheels * Number of rounds, total amount of surplus.     $res=Array(  ); //point the number of wheels to the index of the array and then iterate through the index.      for($i= 0;$i<$len; ++$i ) {        $index=$i+$start _index;//The index change is based on time//when the traversal index exceeds the maximum subscript of the array,        if($index>=$len ) {            $index=$index-$len ; }        $res[] =$list[$index];//Deposit Results    }    return $res;} 

Data processing Examples:
--------------------------------------------
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

-------------------------------------------
Next second

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
==============================

"Complete Example":

<?php Date_default_timezone_set (' PRC ' ); if(isset($_get[' Go '] ) ) {        //Array Data:        $list=Range(1, 100 ); //Sort by Time: The list lists every 5 seconds for a position polling sort, with 10 rows at a time.         $list= Datapollinginterval ($list, ' 2 sec ', 3 ) ; //output sorted data.        $out= ' '; foreach($list  as $k=$v ) {            $out. = '.$v; if($k% 20 = = 19 ) {                $out. = ' <br/><br/> '; }        }        Echo' <pre> ';Print_r($out);Echo' </pre> '; Exit; }        /** @ Name: Log the group every certain time (from the set time) to rotate the position sorting, polling the leaderboard.    Rounds the list of data to a specified second or minute or hour or day.    * @ parameter: (array) $list sequential array, passing in an array that needs to be wheeled; * @ parameter: (time string) $polling the _time interval, the interval between rotations.    Can be: number + seconds, minutes, hours, days (English words);    * @ parameter: (int) $polling _number The number of rows per rotation of the data; * @ return value: Array |    False if the sort returns an array successfully, otherwise the exception condition returns false. * * @author: Wangji sparse, qq:876635409*/    functionDatapollinginterval ($list,$polling _time= ' Ten second minute Hour day ',$polling _number=1 ) {        //parameters for planning polling interval time:        $interval=false; //determine the type of $polling_time is seconds, minutes, hours, days of which 1.         $arg=Array(             ' s ' =>1,//seconds' m ' =>60,//minutes = sec' H ' =>3600,//Time = 3600 sec' d ' = 86400,//Day = 86400 sec        ); //determine the type of interval time and calculate the interval time        foreach($arg  as $k=$v ) {            if(false!==Stripos($polling _time,$k ) ) {                $interval=intval($polling _time) *$v;  Break; }        }                //Judging interval time        if( !Is_int($interval ) ){              return false; }        //number of seconds from the beginning of the year        $this _year_begin_second=Strtotime(Date(' y-01-01 01:00:01 ', Time() ) ); //Current number of seconds-the number of seconds to start this year, and the number of seconds to date this year.         $polling _time= Time() -$this _year_begin_second; //number of seconds from this year to date, calculated to get the current number of rounds        $len=Count($list);//Total length        $start _index=intval($polling _time/$interval ); $start _index=$polling _number*$start _index%$len;//Number of wheels * Number of rounds, total amount of surplus.         $res=Array(  ); //point the number of wheels to the index of the array and then iterate through the index.          for($i= 0;$i<$len; ++$i ) {            $index=$i+$start _index;//The index change is based on time//when the traversal index exceeds the maximum subscript of the array,            if($index>=$len ) {                $index=$index-$len ; }            $res[] =$list[$index];//Deposit Results        }        return $res; }    ? ><! doctype>class= "" ></div></body>var$ =function(ID) {returntypeof id = = "string"? document.getElementById (ID):ID; }    //Ajax method: Ajax (URL, function () {...}, if_post_param);    functionAjax (B,a) {this.bindfunction=function(e,d) {return function(){returnE.apply (D,[d])}};this.statechange=functionD {if(this.request.readystate==4) {this.callbackfunction (This.request.responseText)}}; this.getrequest=function(){if(Window. ActiveXObject) {return NewActiveXObject ("Microsoft.XMLHTTP")}Else{if(Window. XMLHttpRequest) {return NewXMLHttpRequest ()}}return false};this.postbody= (arguments[2]| | ""); This.callbackfunction=a;this.url=b;this.request=this.getrequest ();if(this.request) {varC=this.request; C.onreadystatechange=this.bindfunction (This.statechange,this);if(this.postbody!== "") {C.open ("POST", B,true); C.setrequestheader ("X-requested-with", "XMLHttpRequest"); C.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); C.setrequestheader ("Connection", "Close")}Else{C.open ("GET", B,true)}c.send (this.postbody)}}; Window.SetInterval (function() {Ajax ('? Go=1 ',function(text) {$ ("Containner"). InnerHTML =text;        }            ); } , 1000    );// -</script>

Related Article

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.