Code Analysis of query methods

Source: Internet
Author: User
Public static function getbak ($ type, $ fromtime, $ endtime, $ pageno = 1, $ pagesize) {if ($ pageno <1) $ pageno = 1; // paging function: if ($ type) {Global $ Lang; $ type = intval ($ type); // $ type is in int format. intval prevents digit overflow, if $ type is a string (including Chinese characters), use TRIM () to delete the blank space if (! $ Type) Exit ("$ Lang [plz_select_type]"); if ($ fromtime) $ fromtime = str_replace ("-", '', substr ($ fromtime, 0, 10 )); convert 20100101 14:38:59 to the 20080101 format else $ fromtime = '000000'; if ($ endtime) $ endtime = str_replace ("-", '', substr ($ endtime, (0, 10); else $ endtime = date ("ymd", time (); // The condition determines the output if (expr) else .. Multiple outputs if (expr ){...} Else {...} ---------------- ********************* If ($ fromtime and $ endtime) {$ sqladd = "and 'day'> = '$ fromtime' and 'day' <= '$ endtime'";} elseif ($ fromtime) {$ sqladd = "and 'day'> = '$ fromtime'";} elseif ($ endtime) {$ sqladd = "and 'day' = <'$ endtime'";} if ($ UID) {$ sqladd = "and 'uid' = '$ uid '";} ------------------ *************** the above interpretation is equal to the following section: $ SQL is followed by the conditions for writing. (and there must be spaces before, $ addsql. do not forget the connector following '$ fromtime' The condition value must be enclosed in single quotes) $ addsql. = "where 1 = 1"; ($ fromtime & $ endtime) & $ addsql. = "and 'day'> = '$ fromtime' and 'day' <= '$ endtime'"; $ uid & $ addsql. = "and uid = '$ uid '"; ------------------- *************************** $ SQL = "select * from ". SELF: $ _ table. "Where 'type' = '$ type' $ sqladd limit ". ($ pageno-1) * $ pagesize. ", $ pagesize"; // flip variable // return $ SQL; print out $ DATA = parent: get_all ($ SQL); Retrieve all data, force convert $ data to array format Foreach (array) $ data as $ k => $ v) {$ day = $ V ["day"]; // corresponds to a two-dimensional array, re-create index --- time field $ data1 [$ Day] ["day"] = $ day; $ soldierinfo = ''; // write $ soliderinfo into memory and read data, improves write memory efficiency $ soldierinfo = unserialize ($ V ["soldierinfo"]); // deserialization, =, converts to an array, serialize converts an array into a database storage format. If (is_array ($ soldierinfo) // you can determine whether it is an array or whether is_integer is an integer ,! Empty () determines whether the variable is null. isset () determines whether the variable is set {foreach ($ soldierinfo as $ k2 => $ V) {$ data1 [$ Day] ["soldierinfo"] [$ k2] + = $ soldierinfo [$ k2]; // calculates the values of each field and adopts the foreach loop, instead of listing them one by one, you do not need to modify the code $ data1 [$ Day] ["all"] + = $ V in the future without adding fields in real time. // calculate the sum of each field, the + = symbol is used to automatically accumulate }}return $ data1 ;}} * ********* ---------------------- another method for executing the corresponding SQL statement $ res = mysql_query ($ SQL); If (mysql_num_rows ($ res) <1) // determine whether the retrieved data exists. Return false; while ($ ROW = mysql_fetch_array ($ res) {$ time = $ row ['day']; $ return [$ time] ['type'] = $ row ['type']; $ return [$ time] ['day'] = $ row ['day']; $ soldierinfo = ''; $ soldierinfo = unserialize ($ row [" soldierinfo "]); If (is_array ($ soldierinfo )) {foreach ($ soldierinfo as $ k => $ v) {$ return [$ time] ['soldierinfo'] [$ K] = $ soldierinfo [$ K]; $ return [$ time] ['all'] + = $ V ;}} return $ return ;} =======================================$ soldierinfo print the result: (object) #0 1 = 392276 2 = 128176 3 = 921 4 = 271652 5 = 33948 6 = 0 7 = 0

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.