Php+ajax implementation of micro-letter on the wall

Source: Internet
Author: User
Tags array json key connect mysql query return

For a long time to realize this function, but has not been in the mood, fortunately received netease internship offer, so the mood is good, realize this function to share with you. We can see the effect first: As long as the focus on micro-credit public account Say_magic,


Then open the URL http://www.saymagic.cn/weixin/wall.php, in the public number reply: on the wall + you want to say, you will find that your words will be synchronized to the above Web site.


The whole process is probably like this:

Public number backstage received the message and put the message into the database, and the front desk, then use the settimeout function of JS cycle using Ajax back to get data to obtain the latest data of the database, when understand the whole principle, it seems very simple, then look at the main code:

wall.php (micro-letter Wall page)

 -->




 
  
 
<title>Micro-Letter Wall</title>



    
 


  -->Query ($wxQuery); while ($wxRow =mysql_fetch_row ($wxResult)) {$lastID or $lastID = $wxRow [0];//0 represents the ID in the database, which corresponds to your own database $content = $
WXROW[4];//4 is also the same echo "", $content, "\ n"; $lastID = (int) $lastID;?> <script src= "Http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js" ></ Script> <script> var LastID =
  -->;
        function Getmessages () {$.ajax ({url: "message.php?lastid=" + LastID + "&v=" + (new Date ()/1),  
        DataType: "JSON", Error:function () {alert ("Error loading JSON document"); }, Success:function (data) {//If PHP succeeds $.each (data,function (i,n) {Messag
                        E = "" + N + "";
                       	$ (message). Prependto ("#msgBox"). Hide (). Slidedown ("slow");
				LastID = i;


        });	
	}
    });
Window.settimeout (Getmessages, 5000);
} getmessages (); </script>
 


MySQL Operation file sql.php:
 -->Db_host = $db _host;
        $this->db_user = $db _user;
        $this->db_pwd = $db _pwd;
        $this->db_database = $db _database;
        $this->conn = $conn;
        $this->coding = $coding;
    $this->connect ();
            /* Database Connection/* Public function Connect () {if ($this->conn = = "Pconn") {//Permanent link
        $this->conn = mysql_pconnect ($this->db_host, $this->db_user, $this->db_pwd); else {//even if the link $this->conn = mysql_connect ($this->db_host, $this->db_user, $this->db_
        PWD);
                } if (!mysql_select_db ($this->db_database, $this->conn)) {if ($this->show_error) {
            $this->show_error ("Database unavailable:", $this->db_database);
    }//mysql_query ("SET NAMES $this->coding"); }/* Database execution statement, executable query add modify delete, and so on any SQL statement */Public Function query ($sql) {if ($sql = = ") {$this ; Show_error ("SQL statement error:", "SQL query statement is empty");
        } $this->sql = $sql;


        $result = mysql_query ($this->sql, $this->conn); if (! $result) {//is used in debugging, the SQL statement automatically prints if an error occurs if ($this->show_error) {$this->sh
            Ow_error ("Error SQL statement:", $this->sql);
        } else {$this->result = $result;
    return $this->result;
 }}?>


A file message.php that constantly gets the latest data from the database.


 --> 1000) {die ("possible deep recursion attack"); foreach ($array as $key => $value) {if (Is_array ($value)) {arrayrecursive ($array [$key], $fu
        Nction, $apply _to_keys_also);
        else {$array [$key] = $function ($value);
            } if ($apply _to_keys_also && is_string ($key)) {$new _key = $function ($key);
                if ($new _key!= $key) {$array [$new _key] = $array [$key];
            Unset ($array [$key]);
}}} $recursive _counter--;
	function JSON ($array) {arrayrecursive ($array, "UrlEncode", true);
	$json = Json_encode ($array);
Return UrlDecode ($json);
} $lastID = (int) $_get["LastID"];
Include_once ("sql.php");


$backValue =array (); $wxQuery = "SELECT * from Wx_note WHERE ID >". $lastID. "
Order BY ID LIMIT 3 ";
$wxResult = $mysql->query ($wxQuery);
	while ($wxRow =mysql_fetch_row ($wxResult)) {$recordID = $wxRow [0];
    $content = $wxRow [4]; //$Xml= $content;


$backValue [$recordID] = $content;


echo JSON ($backValue);

 ?>


Finally, if necessary, there is also a background picture:/program/uploadpic/2014-3/2014317135337634.jpg.


Of course, this is just the back end of the part, in the processing of micro-letter messages, you also need to send the message to the database, which should be no difficulty, do not put the code. It's important to remember that the data format corresponds to the method you just manipulated, which is the most error-prone. OK, if you have any questions, please leave a message.


Reprint Please specify: My original blog link http://blog.saymagic.cn/blog.php?id=58




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.