Example of PHP mood voting function (with source code), sample source code

Source: Internet
Author: User
Tags php database

Example of PHP mood voting function (with source code), sample source code

When you browse news pages or other pages, you will feel the feelings after reading them, such as the expressions of awesome, calm, soy sauce, fuel, and potholes. Let readers rate and see if they feel the same as other readers. Great interaction!

Download Now: mood_jb51.rar

This article requires familiarity with jquery, mysql, and ajax, but it is rarely used. This document contains three files: index.html, mood. php, and SQL. php.

  • Index.html: displays and requests ajax data on pages
  • Mood. php: the background file processes the data from the get request and returns the data.
  • SQL. php, database files, and database information

Go directly to the code.

Index.html

First import jquery

//cdn.bootcss.com/jquery/1.7.2/jquery.min.js

Request (ajax-get) Voting count data when the file is loaded

$. Ajax ({type: 'get', url: 'mood. php ', cache: false, data: 'id = 1', ype: 'json', error: function () {alert (' error! ');}, Success: function (json) {if (json) {$. each (json, function (index, array) {var str = "<li> <span>" + array ['mood _ val'] + "</span> <div class = \" pillar \ "style = \" height: "+ array ['height'] +" px; \ "> </div> <div class = \" face \ "rel = \" "+ array ['mid '] +" \ ">  <br/>" + array ['mood _ name'] + "</div> </li> "; $ ("# mood ul "). append (str );});}}});

Add the response to the webpage, click the emoticons logic, and add 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);    }  });});

In this way, the entire front-end is finished.

Mood. php

First, import the SQL. php database file.

include_once("sql.php");

This file processes the core of the entire function, processing databases, cookies...

1. process the code for obtaining the number of votes

$ Mname = explode (',', $ moodname); // mood description $ num = count ($ mname); $ mpic = explode (',', $ moodpic ); // mood icon $ id = (int) $ _ GET ['id']; $ query = mysql_query ("select * from mood where id = $ id "); $ rs = mysql_fetch_array ($ query); if ($ rs) {$ total = $ rs ['mood0'] + $ rs ['moo1'] + $ rs ['mood2'] + $ rs ['mood3'] + $ rs [' mood4']; for ($ I = 0; $ I <$ num; $ I ++) {$ field = 'mood '. $ I; $ m_val = intval ($ rs [$ field]); $ height = 0; // bar chart height if ($ total & $ m_val) {$ height = round ($ m_val/$ total) * $ moodpicheight); // calculate the height} $ arr [] = array ('mid '=> $ 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 ('mid '=> $ I, 'mood _ name' => $ mname [$ I], 'mood _ pic '=> $ mpic [$ I], 'mood _ val' => 0, 'height' => 0);} echo json_encode ($ arr );}

2. Voting Processing

$ Id = (int) $ _ POST ['id']; $ mid = (int) $ _ POST ['moodid']; if ($ mid <0 |! $ Id) {echo "error"; exit;} $ havemood = chk_mood ($ id); if ($ havemood = 1) {echo "you have already expressed "; exit;} $ field = 'mood '. $ mid; // check whether this 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 ['moo1'] + $ 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 ;}

This file is very simple, basically processing databases, and the logic is not very complex. You can watch it carefully.

SQL. php

A common database information storage file, database ip address, 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");

All the core code has been posted so far, so you can skip it. If you need it, download it and have a look.

By the way, there is also a database. You can also paste the DDL.

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;

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.