PHP post mood voting function example sharing

小云云
Release: 2023-03-19 13:10:02
Original
1312 people have browsed it

This article mainly shares with you examples of the PHP mood voting function, hoping to help everyone.

When you browse news pages or other pages, you will have feelings after reading them, such as awesome, calm, soy sauce, come on, cheating, etc. Let readers rate it to see if they feel the same as other readers. Very nice interaction!

This article requires familiarity with jquery, mysql, and ajax-related knowledge, but it is not used much. This article has three files: index.html, mood.php, sql.php

index.html. The page displays and requests ajax data mood.php. The background file processes the data from the get request and returns the data sql. php, database file, save the database information and enter the code directly.

index.html First import jquery

//cdn.bootcss.com/jquery/1.7.2/jquery.min.js
当文档载入完毕就请求(ajax-get)投票人数数据
$.ajax({
  type: 'GET',
  url: 'mood.php',
  cache: false,
  data: 'id=1',
  dataType: 'json',
  error: function(){
    alert('出错了!');
  },
  success: function(json){
    if(json){
      $.each(json,function(index,array){
        var str = "<li><span>"+array['mood_val']+"</span><div class=\"pillar\" style=\"height:"+array[&#39;height&#39;]+"px;\"></div><div class=\"face\" rel=\""+array[&#39;mid&#39;]+"\"><img src=\"images/"+array[&#39;mood_pic&#39;]+"\"><br/>"+array['mood_name']+"</div></li>";
        $("#mood ul").append(str);
      });
    }
  }
});
Copy after login

and return to add it to the web page, then click on the expression logic and ajax to the background

$(".face").live('click',function(){
  var face = $(this);
  var mid = face.attr("rel");
  var value = face.parent().find("span").html();
  var val = parseInt(value)+1;
  $.post("mood.php?action=send",{moodid:mid,id:1},function(data){
    if(data>0){
      face.prev().css("height",data+"px");
      face.parent().find("span").html(val);
      face.find("img").addClass("selected");
    }else{
      alert(data);
    }
  });
});
Copy after login

In this way, the entire front desk completes the work

mood.php First, import the sql.php database file

include_once("sql.php"); This file handles the core of the entire function, processing databases, cookies...

1. Processing the code for obtaining the number of voters

$mname = explode(',',$moodname);//心情说明
$num = count($mname);
$mpic = explode(',',$moodpic);//心情图标
$id = (int)$_GET['id'];
$query = mysql_query("select * from mood where id=$id");
$rs = mysql_fetch_array($query);
if($rs){
  $total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4'];
  for($i=0;$i<$num;$i++){
    $field = &#39;mood&#39;.$i;
    $m_val = intval($rs[$field]);
    $height = 0; //柱图高度
    if($total && $m_val){
      $height=round(($m_val/$total)*$moodpicheight); //计算高度
    }
    $arr[] = array(
      &#39;mid&#39; => $i,
      'mood_name' => $mname[$i],
      'mood_pic' => $mpic[$i],
      'mood_val' => $m_val,
      'height' => $height
    );
  }
  echo json_encode($arr);
} else {
  for($i=0;$i<$num;$i++){
    $arr[] = array(
      &#39;mid&#39; => $i,
      'mood_name' => $mname[$i],
      'mood_pic' => $mpic[$i],
      'mood_val' => 0,
      'height' => 0
    );
  }
  echo json_encode($arr);
}
Copy after login

2. Processing the voting function

$id = (int)$_POST['id'];
$mid = (int)$_POST['moodid'];
if($mid<0 || !$id){
  echo "错误";
  exit;
}
$havemood = chk_mood($id);
if($havemood==1){
  echo "您已表达过了";exit;
}
$field = 'mood'.$mid;
//查询是否有这个id
$result = mysql_query("select 1 from mood where id='{$id}'");
$row = mysql_fetch_array($result);
if(is_array($row)){
  $query = mysql_query("update mood set ".$field."=".$field."+1 where id=".$id);
  if($query){
    setcookie("mood".$id, $mid.$id, time()+3600);
    $query2 = mysql_query("select * from mood where id=$id");
    $rs = mysql_fetch_array($query2);
    $total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4'];
    $height = round(($rs[$field]/$total)*$moodpicheight);
    echo $height;
  }else{
    echo -1;
  }
} else {
  mysql_query("INSERT INTO mood(id,mood0,mood1,mood2,mood3,mood4)VALUES ('{$id}','0','0','0','0','0')");
  $query = mysql_query("update mood set ".$field."=".$field."+1 where id=".$id);
  setcookie("mood".$id, $mid.$id, time()+3600);
  echo $moodpicheight;
}
Copy after login

This file is very simple, basically processing the database, and the logic is not very complicated. You can come down and take a closer look yourself.

sql.php is a general database information storage file, database IP, account, password, database name, etc.

$host="localhost";
$db_user="root";
$db_pass="";
$db_name="demo";
$timezone="Asia/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");
header("Content-Type: text/html; charset=utf-8");
Copy after login

So far, all the core codes have been posted, and the master will jump However, if necessary, download it and take a look. There is also a database. OK, the DDL is also posted.

CREATE TABLE `mood` (
 `id` tinyint(5) NOT NULL,
 `mood0` int(9) unsigned NOT NULL,
 `mood1` int(9) unsigned NOT NULL,
 `mood2` int(9) unsigned NOT NULL,
 `mood3` int(9) unsigned NOT NULL,
 `mood4` int(9) unsigned NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Copy after login

Have you learned it? Hurry up and try it out.

Related recommendations:

Sample code analysis of php implementation of voting system

How to use PHP+MySql to implement WeChat voting function

php example code sharing to implement the mood voting function

The above is the detailed content of PHP post mood voting function example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template