Based on jquery, PHP and MySQL achieve a rating function

Source: Internet
Author: User
Tags php and mysql
This article mainly introduces based on jquery through PHP and MySQL to achieve a rating function, interested in a friend's reference, I hope to be helpful to everyone.

Database design

First prepare two tables, the PIC table holds the picture information, including the image corresponding to the name, path and the total number of pictures "like", Pic_ip records the user click on the IP data.

CREATE TABLE IF not EXISTS ' pic ' (  ' id ' int (one) not null auto_increment,  ' pic_name ' varchar) ' is not NULL  ' pic _url ' varchar ' NOT null, ' Love  ' int (one) not null default ' 0 ',  PRIMARY KEY (' id ')) engine=myisam default Charse T=utf8;
CREATE TABLE IF not EXISTS ' pic_ip ' (  ' id ' int (one-by-one) not NULL auto_increment,  ' pic_id ' int (one-by-one) not NULL,  ' IP ' VA Rchar (+) not NULL,  PRIMARY KEY (' id ')) engine=myisam DEFAULT Charset=utf8

index.php in index.php, we read the picture information in the PIC table through PHP and display it, combining CSS to enhance the effect of the page display.
The code is as follows

<! DOCTYPE html>

CSS, we'll define the dynamic effect of the mouse slide and leave the Hearts button, and position the button. the code is as follows

. list{width:760px; margin:20px Auto}. list li{float:left; width:360px; height:280px; margin:10px; position:relative}. List Li P{position:absolute; top:0; left:0; width:360px; height:24px; line-height:24px; background: #000; opacity:.8; Filter:alpha (opacity=80);} . list Li P a{padding-left:30px; height:24px; Background:url (images/heart.png) no-repeat 4px-1px;color: #fff; Font-weight:bold; FONT-SIZE:14PX}. List Li P A:hover{background-position:4px-25px;text-decoration:none}

jquery Code when a user clicks on the Hearts button on their favorite image, sends an AJAX request to the background love.php, and then updates the original value after the request has been successfully answered.
The code is as follows

$ (function () {   $ ("p a"). Click (function () {     var love = $ (this);     var id = love.attr ("rel"); Corresponding ID     love.fadeout (300);//Fade effect     $.ajax ({       type: "POST",       URL: "love.php",       data: "id=" +id,       Cache:false,//Do not cache this page       success:function (data) {         love.html (data);         Love.fadein (300); Fade effect       }     );     return false;   }); });

love.php
Background love.php receive the AJAX request of the front-end, according to the submitted image ID value, find whether the IP table has a click record of the user's IP, if there is a tell the user has "liked", conversely, then do a bit:
1. Update the value of the image Love field in the Picture table, and add the value 1.
2, the User IP information is written to the Pic_ip table, to prevent users from repeating the click.
3. Get the updated love value, that is, the total number of users who like the picture, and output that total to the front page.
The code is as follows

<?php$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");? > <?phpinclude_once ("connect.php"); $ip = Get_client_ip (); $id = $_post[' id '];if (!isset ($id) | | empty ($id)) exit;$ Ip_sql=mysql_query ("Select IP from pic_ip where pic_id= ' $id ' and ip= ' $ip '"); $count =mysql_num_rows ($ip _sql); if ($count =  =0) {$sql = "update pic set love=love+1 where id= ' $id '"; mysql_query ($sql); $sql _in = "INSERT into PIC_IP (PIC_ID,IP) values (' $id ', ' $ip '); mysql_query ($sql _in); $result = mysql_query ("Select Love from pic where id= ' $id '"); $row = Mysql_fetch_arr Ay ($result); $love = $row [' Love '];echo $love;} Else{echo "loved it.";} Get the user real ipfunction get_client_ip () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), " Unknown ")) $ip = getenv (" Http_client_ip "), ElseIf (getenv (" Http_x_forwarded_for ") && strcasecmp (getenv (" Http_ ") X_forwarded_for ")," UNKNown ")) $ip = getenv (" Http_x_forwarded_for "), ElseIf (getenv (" REMOTE_ADDR ") && strcasecmp (getenv (" REMOTE_ADDR ") ")," Unknown ")) $ip = getenv (" REMOTE_ADDR ") ElseIf (Isset ($_server[' remote_addr ')) && $_server[' REMOTE_ADDR '] && strcasecmp ($_server[' remote_addr '), "unknown") $ip = $_server[' remote_addr '];else$ip = "Unknown"; return ($ IP);}? >

The GET_CLIENT_IP () function in the code is used to get the user's real IP.

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP prevents websites from being attacked by prohibiting frequent IP access

Php+mysql+jquery implementation of Query and list box selection

PHP implementation of digital format, the number of every three comma-plus function function

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.