PHP: insert multiple lines of records (including real-time progress display) in the second development of the SMS mobile phone interface)

Source: Internet
Author: User
Originally, this CP is a single SMS interface. For various reasons, you need to send a message to all the members of the website (tens of thousands of member records) each time ), Program It is difficult to insert and update records in ten thousand rows between different database servers from MySQL to MSSQL, display the sending progress in real time, and feedback the post-sending results. There are several requirements in the text message sending process: 1) program timeout is not allowed; 2) the validity of the mobile phone number is automatically detected; 3) the proportion of the sent progress is calculated; 4) from MySQL to MSSQL, there are tens of thousands of lines of records inserted and updated between different database servers. SQL statements are not overloaded. Solution: use an Ajax script in JQ to submit an application for sending SMS records, the program reads the application record and uses the session to record the progress data. Based on the number of messages sent each time, it returns the phone number in the record that has not sent the text message, and adds the mobile phone legality verification before sending the program. Ajax receives the record data returned for the first time, submits the mobile phone number to a recursive function, and then submits the remaining mobile phone number again in the function until the progress is 100%. I don't know, but I have not explained it. For more information, see Code Function send_onesms (S, P, sendid) {// This is the first ajax to send if (sendid = '') {$. prompt ('you have not selected the text message to send? ', {Buttons: {OK: true}); Return false;} var totel = ''; $ (' # loading '). show (); $. ajax ({Timeout: 36000, type: "Get", URL: "/admincp/page. PHP ", data:" showtype = 2 & page = "+ P + '& sendid =' + sendid, beforesend: function () {$. ajax ({async: True, URL: "/admincp/jindu. PHP? Act = clear & RND = "+ new date (). gettime (), success: function (response) {$ ("# jindu" ).html (response) ;}, datatype: "html"}); Response ('a.sendclass').html ('locked status '). removeattr ("onclick"); ('inclusid'{sendid}.html ('sending... '). removeattr ("onclick");}, success: function (MSG) {$ ('# i2page '). val = P; // ('{smslist'{.html (Unescape (MSG); // $ ('# loading '). hide (); If (MSG! = 'Over') {totel = MSG; // setinterval (function () {$. Ajax ({async: True, URL: "/admincp/jindu. php? RND = "+ new date (). gettime (), success: function (response) {$ ("# jindu" ).html (response) ;}, datatype: "html "})}, 1000) ;}}); send_sms (S, P, sendid, totel); // window. clearinterval (thread);} function send_sms (S, P, sendid, totel) {// a recursive function $. ajax ({Timeout: 36000, datatype: "html", type: "Post", URL: "/admincp/page. PHP? RND = "+ new date (). gettime (), data: "showtype = 2 & page =" + P + '& sendid =' + sendid + '& totel =' + totel, success: function (response) {If (response! = 'Over') {$. Ajax ({async: True, URL: "/admincp/jindu. php? RND = "+ new date (). gettime (), success: function (response) {$ ("# jindu" ).html (response) ;}, datatype: "html"}); setTimeout (send_sms (S, P, sendid, response), 1000);} else {$. ajax ({async: True, URL: "/admincp/jindu. PHP? Act = over & RND = "+ new date (). gettime (), success: function (response) {alert (response) ;}, datatype: "html"}); $ ('# loading '). hide (); menu_sec2_move (p );}}});} ============ below is the code in PHP ============ function send_sms ($ SID = '', & $ MSG, & $ totel) {if ($ totel = ''& $ _ session ['sendjsondu'] = 0) {// if ($ this-> get_tel ($ Sid, $ MSG, $ totel) {; $ telarr = explode (",", $ totel ); $ _ session ['sendcount'] = count ($ telarr);} else {$ _ Session ['sendjsondu'] = 100;} $ I = 0; $ J = 0; $ _ session ['send _ I '] = 0; $ _ session ['send _ J'] = 0;} else {// progress refresh execution if ($ _ session ['sendjsondu'] = 100) {$ MSG = 'success :'. $ _ session ['send _ I ']. ', error :'. $ _ session ['send _ J']; return false;} $ I =$ _ session ['send _ I ']; $ J = $ _ session ['send _ J'];} if ($ SID = '') {$ MSG = 'unknown SMS record '; return false ;} $ db = new db_ SQL; // initialize the database $ db-> connect (db_database, db_host, db_user, DB _ Password); // database connection $ db-> db_ SQL ("set names GBK "); $ SQL = "select * From sz_sms where sd_state = 'reviewed' and sd_id = ". $ Sid; $ db-> query ($ SQL); if ($ db-> next_record () {$ sdid = $ db-> record ['sd _ id']; $ sdfrom = $ db-> record ['sd _ from']; $ sdto = $ db-> record ['sd _ to']; $ sdcontent = $ db-> record ['sd _ content']; $ sdtime = $ db-> record ['sd _ time']; $ sdstate = $ db-> record ['sd _ state']; $ sdtowho = $ db-> record ['sd _ towh']; $ sdediter = $ db-> Record ['sd _ editer ']; $ sdpasser = $ db-> record ['sd _ passer']; $ sdpasstime = $ db-> record ['sd _ passtime '];} else {$ MSG = 'sms record status is not reviewed'; return false ;} $ alljindu = $ _ session ['sendcount']; // total $ telarr = explode (",", $ totel); $ cpdb = mssql_connect (cp_host, cp_user, cp_password ); mssql_select_db (cp_database, $ cpdb); $ sendtime = date ("Y-m-d h: I: s"); $ T = 10; // send several messages each time, the larger the value, the longer the wait time (Note: The value is too large and the client is prone to crashes) $ C = 0; $ nexttel = ''; $ SQL = ""; Foreach ($ telarr as & $ value) {$ sender = substr ($ value, 0, 11); if ($ C <$ T) {If (preg_match ("/(^ (13) [0-9] [0-9] {8} $) | (^ (15) [0-9] [0-9] {8} $)/", $ sender) {$ SQL =" insert into sms_table (from_telephone, to_telephone, msg_content, msg_time) values "; $ SQL. = "('". $ sdfrom. "','". $ sender. "','". $ sdcontent. "','". $ sendtime. "')"; mssql_query ($ SQL, $ cpdb); $ I ++;} else {$ J ++ ;}} else {if ($ nexttel = '') {$ nexttel = $ Sender;} else {if ($ sender! = '') $ Nexttel. = ','. $ sender ;}}$ C ++ ;}$ _ session ['sendjsondu'] = intval ($ I + $ J)/$ alljindu) * 100 ); $ _ session ['send _ I '] = $ I; $ _ session ['send _ J'] = $ J; if ($ nexttel = '') {$ _ session ['sendjsondu'] = 100;} $ totel = $ nexttel; $ MSG = 'success :'. $ I. ', error :'. $ J; return true ;}

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.