The red-blue (top-Step) voting code and jquerymysql are implemented based on PHP + jQuery + MySql. The red and blue (top step) voting code is implemented based on PHP + jQuery + MySql. jquerymysql will show you how to view and download the source code. this is a very practical example of voting, the application is based on PHP + jQuery + MySql to implement the red-blue (top-Step) voting code, jquerymysql
First, we will show you:
View demo download source code
This is a very practical example of voting, which is applied in the scenario where both parties disagree with the voting. Users can select one party to vote on behalf of their own views. This article uses the red and blue sides to vote as an example. through the frontend and backend interaction, the voting numbers and proportions of the red and blue sides are intuitively displayed, which is widely used.
This is a comprehensive knowledge application article that requires you to have basic knowledge about PHP, jQuery, MySQL, html, and css.
HTML
We need to show the views of both parties on the red and blue pages, the number and proportion of votes, and the hand images used for voting interaction, in this example, # red and # blue represent the red and blue sides respectively ..Redhand and. bluehandThis button is used for manual voting. the. redbar and. bluebar display the proportional adjustment of red and blue sides. # red_num and # blue_num display the voting numbers of both sides.
What do you think of the articles provided by Helloweba?
Very practical and hard to understand
CSS
Use CSS to beautify the page, load the background image, and determine the relative location. you can directly copy the following code and make some modifications in your project.
.vote{width:288px; height:220px; margin:60px auto 20px auto;position:relative} .votetitle{width:100%;height:62px; background:url(icon.png) no-repeat 0 30px; font-size:15px} .red{position:absolute; left:0; top:90px; height:80px;} .blue{position:absolute; right:0; top:90px; height:80px;} .votetxt{line-height:24px} .votetxt span{float:right} .redhand{position:absolute; left:0;width:36px; height:36px; background:url(icon.png) no-repeat -1px -38px;cursor:pointer} .bluehand{position:absolute; right:0;width:36px; height:36px; background:url(icon.png) no-repeat -41px -38px;cursor:pointer} .grayhand{width:34px; height:34px; background:url(icon.png) no-repeat -83px -38px;cursor:pointer} .redbar{position:absolute; left:42px; margin-top:8px;} .bluebar{position:absolute; right:42px; margin-top:8px; } .redbar span{display:block; height:6px; background:red; width:100%;border-radius:4px;} .bluebar span{display:block; height:6px; background:#09f; width:100%;border-radius:4px; position:absolute; right:0} .redbar p{line-height:20px; color:red;} .bluebar p{line-height:20px; color:#09f; text-align:right; margin-top:6px}
JQuery
When you click the hand button, use jQuery's $. getJSON () sends an Ajax request to php in the background. if the request succeeds, the json data returned by the background is obtained, and jQuery then processes the json data. The following function: getdata (url, sid) is passed with two parameters. the url is the php address in the request background. sid indicates the ID of the current voting topic. in this function, the returned json data includes the number of votes on both sides of red and blue, and the ratio of both sides. the width of the proportion bar is calculated based on the ratio, and the voting effect is displayed asynchronously.
Function getdata (url, sid) {$. getJSON (url, {id: sid}, function (data) {if (data. success = 1) {var w = 208; // defines the total width of the proportional bar // The number of RedSquare Votes $ ("# red_num" ).html (data. red); $ ("# red" ).css ("width", data. red_percent * 100 + "%"); var red_bar_w = w * data. red_percent-10; // RedSquare proportional bar width $ ("# red_bar" ).css ("width", red_bar_w); // number of blue-side Votes $ ("# blue_num" ).html (data. blue); $ ("# blue" ).css ("width", data. blue_percent * 100 + "%"); var blue_bar_w = w * data. blue_percent; // The width of the blue proportional bar $ ("# blue_bar" ).css ("width", blue_bar_w);} else {alert (data. msg );}});}
When the page is loaded for the first time, getdata () is called, and getdata () is also called by clicking vote for the Red Party or vote for the Blue Party, but the parameters passed are different. Note that the sid parameter in this example is set to 1 based on the id in the data table. developers can read the exact id based on the actual project.
$ (Function () {// get the initial data getdata ("vote. php ", 1); // Red Party vote $ (". redhand "). click (function () {getdata ("vote. php? Action = red ", 1) ;}); // blue party vote $ (". bluehand "). click (function () {getdata (" vote. php? Action = blue ", 1 );});});
PHP
The frontend requests vote. php in the background. vote. php connects to the database based on the received parameters and calls related functions.
Include_once ("connect. php "); $ action = $ _ GET ['action']; $ id = intval ($ _ GET ['id']); $ ip = get_client_ip (); // obtain the ip if ($ action = 'red') {// The red party vote for vote (1, $ id, $ ip );} elseif ($ action = 'blue') {// The blue-party voting vote (0, $ id, $ ip );} else {// Default returned initial data echo jsons ($ id );}
The vote ($ type, $ id, $ ip) function is used to perform a voting action. $ type indicates the voting party, $ id indicates the voting topic id, and $ ip indicates the user's current ip address. First, query whether the current IP address record exists in the votes_ip table based on the user's current ip address. If yes, the user has voted. Otherwise, the red or blue voting count is updated, write the voting records of the current user to the votes_ip table to prevent repeated voting.
Function vote ($ type, $ id, $ ip) {$ ip_ SQL = mysql_query ("select ip from votes_ip where vid = '$ ID' and ip =' $ IP '"); $ count = mysql_num_rows ($ ip_ SQL); if ($ count = 0) {// No vote yet if ($ type = 1) {// RedSquare $ SQL = "update votes set likes = likes + 1 where id = ". $ id;} else {// Blue side $ SQL = "update votes set unlikes = unlikes + 1 where id = ". $ id;} mysql_query ($ SQL); $ SQL _in = "insert into votes_ip (vid, ip) values ('$ ID',' $ IP ')"; mysql_query ($ SQL _in); if (mysql_insert_id ()> 0) {echo jsons ($ id);} else {$ arr ['success'] = 0; $ arr ['MSG '] = 'Operation Failed. please try again'; echo json_encode ($ arr) ;}} else {$ arr ['success'] = 0; $ arr ['MSG '] = 'vote passed'; echo json_encode ($ arr );}}
The jsons ($ id) function is used to query the number of votes of the current id, calculate the proportion, and return the json data format for the front-end call.
function jsons($id){ $query = mysql_query("select * from votes where id=".$id); $row = mysql_fetch_array($query); $red = $row['likes']; $blue = $row['unlikes']; $arr['success']=1; $arr['red'] = $red; $arr['blue'] = $blue; $red_percent = round($red/($red+$blue),3); $arr['red_percent'] = $red_percent; $arr['blue_percent'] = 1-$red_percent; return json_encode($arr); }
The function for obtaining the real IP address of a user is also involved:Get_client_ip (),Click here to see the relevant code: http://www.bkjia.com/article/58610.htm
MySQL
Finally, the Mysql data table is attached. the votes table is used to record the total number of votes on both sides of red and blue, and the votes_ip table is used to store the user's voting IP address records.
CREATE TABLE IF NOT EXISTS `votes` ( `id` int(10) NOT NULL AUTO_INCREMENT, `likes` int(10) NOT NULL DEFAULT '0', `unlikes` int(10) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES (1, 30, 10); CREATE TABLE IF NOT EXISTS `votes_ip` ( `id` int(10) NOT NULL, `vid` int(10) NOT NULL, `ip` varchar(40) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
PHP + MySql + jQuery implement the top and top vote function
This article uses examples to explain the "top" and "step-on" voting functions implemented using PHP + MySql + jQuery, and record the user IP address to determine whether the user's voting behavior is effective, this instance can also be extended to the voting system.
First, place the buttons "top" and "step" on the page, that is, # dig_up and # dig_down. the buttons respectively record the votes and percentage of votes.
Very good, very powerful!
Too bad!
$ (Function () {// when the mouse is suspended or left, switch the button background style $ ("# dig_up "). hover (function () {$ (this ). addClass ("digup_on") ;}, function () {$ (this ). removeClass ("digup_on") ;}); $ ("# dig_down "). hover (function () {$ (this ). addClass ("digdown_on") ;}, function () {$ (this ). removeClass ("digdown_on") ;}); // initialize the data getdata ("ajax. php ", 1); // when you click" top "$ (" # dig_up "). click (function () {getdata ("ajax. php? Action = like ", 1) ;}); // click $ (" # dig_down "). click (function () {getdata (" ajax. php? Action = unlike ", 1 );});});
The above content implements the red-blue (top-Step) voting code based on PHP + jQuery + MySql. I hope you will like it.
Vote code (Top step). jquerymysql will show you the demo download source code. this is a very practical voting instance applied to both parties...