PHP + Ajax achieve a mood voting feature articles instance, you can learn to understand the basic processes of vote: Get mood icons and histogram data through ajax, when a user clicks on an icon of a mood, send a request to Ajax.php, PHP validation the user cookie to prevent duplicate submissions, then the data field contents mood mysql corresponding plus 1, after a successful return to the front page, and update the histogram and statistics.
Published mood:
. 1 $ ID = (int) $ _POST [ 'ID']; // article or post ID 2 $ MID = (int) $ _POST [ 'moodid']; // mood id (mood provide 8 profiles) 3 IF (! $ MID ||! $ ID ) { . 4 echo "this link is not present"; Exit ; . 5 } . 6 . 7 $ havemood = chk_mood ( $ ID ); // verify Cookie . 8 IF ( $ havemood ==. 1 ) { 9 echo "You have expressed feelings, and remain unbiased wholesome!"; Exit ; 10 } . 11 $ Field = 'Mood'. $ MID ; // data table mood field, respectively mood0, mood1, mood2 ... represent different mood field 12 is $ Query = the mysql_query ( "SET Mood Update." $ field .. "=" $ field "WHERE ID = +. 1.". $ ID ); // corresponding to the field value mood + 1'd 13 is IF ( $ Query ) { 14 the setcookie . ( "mood" $ ID , $ MID . the above mentioned id $ , time () +300); // set a cookie, we set a cookie in order to test the expiration time for the 300S 15 $ query2 =the mysql_query ( "SELECT * from Mood WHERE ID = $ ID " ); 16 $ RS = the mysql_fetch_array ( $ of query2 ); // Get the article data mood . 17 $ Total = $ RS [ 'mood0'] + $ RS [ 'mood1 '] + $ RS [' mood2 '] + $ RS [' mood3 '] + $ RS [' mood4 '] + $ RS [' mood5 '] + 18 is $ RS [' mood6 '] + $ RS [' mood7 ' ]; 19 $height = round(($rs[$field]/$total)*moodpicheight $ ); // get the total amount, and calculates the histogram corresponding to the mood of the current altitude 20 is echo $ height ; // return the current mood of the columnar height 21 is } the else { 22 is echo -1; // data error 23 }
Gets mood:
. 1 $ MNAME = the explode ( ',', $ moodname ); // mood Description 2 $ NUM = COUNT ( $ MNAME ); . 3 $ MPIC = the explode ( ',', $ moodpic ); // moods . 4 . 5 $ ID = (int) $ _GET [ 'the above mentioned id']; // article or post the above mentioned id 6 $ query = mysql_query ( "mood the WHERE from the SELECT * the above mentioned id = $ the above mentioned id "); // query data corresponding mood 7 $ rs = mysql_fetch_array (Query $ ); . 8 IF ( $ RS ) { . 9 // get total mood published 10 $ Total = $ RS [ 'mood0'] + $ RS [ 'mood1'] + $ RS [ 'mood2'] + $ RS [ 'mood3'] + $ RS [ 'mood4'] + . 11 $ RS [ 'mood5'] + $ RS [ 'mood6'] + $ RS [ 'mood7' ]; 12 is for ( $ I = 0; $ I < NUM $ ; $ I ++ ) { 13 is $ Field = 'Mood'. $ I ;//Field name 14 $ m_val = the intval ( $ RS [ $ Field ]); // mood corresponding to the value (number) 15 $ height = 0; // column height in FIG. 16 IF ( $ Total && $ m_val ) { . 17 $ height = round (( $ m_val / $ Total ) * $ moodpicheight ); // calculate the height 18 is } . 19 20 is $ ARR [] = Array ( 21 is 'MID' => $ I ,// corresponds mood ID 22 is 'mood_name' => $ MNAME [ $ I ], // the mood name 23 is 'mood_pic' => $ MPIC [ $ I ], // icon 24 'mood_val' => $ m_val , // number of times 25 'height' => $ height // histogram height 26 is ); 27 } 28 echo json_encode ( $ ARR ); // return JSON data 29 }
Mood get a list of information, and show the page:
. 1 $ ( function () { 2 $ .ajax ({ . 3 type: 'the GET', // a get request to send . 4 URL: 'the ajax.php', // destination address . 5 Cache: to false , // do not cache data , pay attention to the mood of the civilized published data in real time, the need to cache set to false, the default is to true 6 the data: 'the above mentioned id = 1', // parameter corresponding article or post of id, in this case fixed at 1, practical application in the current article is to obtain or post ID . 7 dataType: 'JSON', // data type JSON . 8 error: function () { . 9 Alert ( '! wrong' ); 10 }, 11 success: function(json){ //请求成功后 12 if(json){ 13 $.each(json,function(index,array){ //遍历json数据列 14 var str = "<li><span>"+array['mood_val']+"</span><div class=\"pillar\" 15 style=\"height:"+array['height']+"px;\"></div><div class=\"face\" 16 rel=\""+array['mid']+"\"><img src=\"images/"+array['mood_pic']+"\"> 17 <br/>"+array['mood_name']+"</div></li>"; 18 $("#mood ul").append(str); //The data added to the list #mood ul 19 }); 20 } 21 } 22 }); 23 ... 24 });
Database table to establish a direct run the following code:
1 CREATE TABLE IF NOT EXISTS `mood` ( 2 `id` int(11) NOT NULL, 3 `mood0` int(11) NOT NULL DEFAULT '0', 4 `mood1` int(11) NOT NULL DEFAULT '0', 5 `mood2` int(11) NOT NULL DEFAULT '0', 6 `mood3` int(11) NOT NULL DEFAULT '0', 7 `mood4` int(11) NOT NULL DEFAULT '0', 8 `mood5` int(11) NOT NULL DEFAULT '0', 9 `mood6` int(11) NOT NULL DEFAULT '0', 10 `mood7` int(11) NOT NULL DEFAULT '0', 11 PRIMARY KEY (`id`) 12 ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 13 14 15 INSERT INTO `mood` (`id`, `mood0`, `mood1`, `mood2`, `mood3`, `mood4`, `mood5`, `mood6`, `mood7`) 16 VALUES(1, 8, 6, 20, 16, 6, 9, 15, 21);
This switched: https://www.sucaihuo.com/php/155.html please indicate the source!