PHP+Ajax implementation of article mood voting function example

A PHP+Ajax implementation of article mood voting function example, you can learn to understand the basic process of voting: get mood icon and histogram related data through Ajax, when the user clicks one of the mood icons, send a request to Ajax.php, PHP verifies that the user cookie prevents repeated submission, then adds 1 to the corresponding data mood field in mysql, and returns before success And update the histogram and statistics.

Publishing mood:

 1 $id = (int)$_POST['id']; //Article or post id  
 2 $mid = (int)$_POST['moodid']; //mood id(8 moods in profile)  
 3 if(!$mid || !$id){  
 4     echo "This link does not exist";exit;  
 5 }  
 6   
 7 $havemood = chk_mood($id); //Verification cookie  
 8 if($havemood==1){  
 9     echo "You have expressed your mood. It's good for your physical and mental health to keep your normal mind!";exit;  
10 }  
11 $field = 'mood'.$mid; //The mood fields in the data table are mood0,mood1,mood2...Show different mood fields  
12 $query = mysql_query("update mood set ".$field."=".$field."+1 where id=".$id); //Corresponding mood field value+1  
13 if($query){  
14     setcookie("mood".$id, $mid.$id, time()+300); //Set up cookie,To test our settings cookie Expiration time is 300 s  
15     $query2 = mysql_query("select * from mood where id=$id");  
16     $rs = mysql_fetch_array($query2);//Get mood data of the article  
17     $total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4']+$rs['mood5']+  
18 $rs['mood6']+$rs['mood7'];  
19     $height = round(($rs[$field]/$total)*$moodpicheight); //Get the total amount and calculate the height of the histogram corresponding to the current mood  
20     echo $height; //Return to the height of the current mood column  
21 }else{  
22     echo -1; //Data error  
23 }


Get mood:

 1 $mname = explode(',',$moodname);//Mood description  
 2 $num = count($mname);  
 3 $mpic = explode(',',$moodpic);//Mood Icon  
 4   
 5 $id = (int)$_GET['id']; //Article or post id  
 6 $query = mysql_query("select * from mood where id=$id"); //Query the corresponding mood data  
 7 $rs = mysql_fetch_array($query);  
 8 if($rs){  
 9     //Get the total amount of published mood  
10     $total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4']+  
11 $rs['mood5']+$rs['mood6']+$rs['mood7'];  
12     for($i=0;$i<$num;$i++){  
13         $field = 'mood'.$i; //Field name  
14         $m_val = intval($rs[$field]); //Corresponding value of mood (Times)  
15         $height = 0; //Column height  
16         if($total && $m_val){  
17             $height=round(($m_val/$total)*$moodpicheight); //Calculated altitude  
18         }  
19               
20         $arr[] = array(  
21             'mid' => $i, //Corresponding mood id  
22             'mood_name' => $mname[$i], //Mood name  
23             'mood_pic' => $mpic[$i], //Icon  
24             'mood_val' => $m_val, //frequency  
25             'height' => $height //Height of histogram  
26         );  
27     }  
28     echo json_encode($arr); //Return JSON data  
29 }


Get mood list information and display it on the page:

 1 $(function(){  
 2     $.ajax({  
 3         type: 'GET', //adopt get Send request by  
 4         url: 'ajax.php', //Destination address  
 5         cache: false, //Do not cache the data. Note that the data of civilized publishing mood is real-time. You need to cache Set to false,The default is true  
 6         data: 'id=1', //Parameter, corresponding to the id,In this case, it is fixed to 1, which is used to get the current article or post id  
 7         dataType: 'json', //The data type is json  
 8         error: function(){  
 9             alert('Wrong!');  
10         },  
11         success: function(json){ //After successful request  
12             if(json){  
13                 $.each(json,function(index,array){ //ergodic json Data column  
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); //Add data to#In the mood ul list  
19                    });   
20             }  
21         }  
22     });  
23     ...  
24 });


Database table creation runs the following code directly:

 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);


From: https://www.sucaihuo.com/php/155.html Reprint please indicate the source!

Keywords: PHP JSON MySQL Database

Added by bluebyyou on Thu, 05 Dec 2019 16:40:49 +0200