Mã php cho nút thích và không thích
Trong hướng dẫn này, chúng ta sẽ học Cách tạo mã Thích và Không thích bằng cách sử dụng PHP, MySQL và jQuery Show Hệ thống đánh giá rất hữu ích cho mọi dự án web. Thông qua hệ thống này, quản trị viên web có thể theo dõi lượt thích và không thích của khách truy cập. Ngoài ra, nó sẽ giúp quản trị viên web hiểu các lựa chọn của khách truy cập và làm cho trang web của họ tương tác nhiều hơn với khách truy cập Thử nghiệm Tải xuống Chúng tôi đã thực hiện đánh giá đơn giản hoặc thích hoặc không thích hệ thống mã bằng cách sử dụng PHP, MySQL, jQuery và Ajax. Hệ thống xếp hạng này đưa khách truy cập thích hoặc không thích với jQuery và Ajax Bước 1. Tạo bảng 'thích' cơ sở dữ liệu bằng truy vấn MySQL CREATE TABLE `likes` ( `id` int(5) NOT NULL AUTO_INCREMENT, `pid` int(10) NOT NULL, `like` int(10) NOT NULL, `unlike` int(10) NOT NULL, `uid` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1; Bước 2. Tạo bảng 'sản phẩm' cơ sở dữ liệu bằng truy vấn MySQL CREATE TABLE `products` ( `id` int(11) NOT NULL AUTO_INCREMENT, `product_name` varchar(255) NOT NULL, `price` double(10,2) NOT NULL DEFAULT '0.00', `status` int(2) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4; -- -- Dumping data for table `products` -- INSERT INTO `products` VALUES (1, 'developer desks design book', 20.00, 1); INSERT INTO `products` VALUES (2, 'Developer desks login tutorials', 10.00, , 1); INSERT INTO `products` VALUES (3, 'developer desks coding book', 100.00, 1); Bước 3. Kết nối chức năng DB với mã hóa db Bước 4. Mục lục. tệp php có Tệp chính hiển thị danh sách sản phẩm và các nút thích 0) // check if alredy liked or not condition { $likeORunlike = mysqli_fetch_array($query); if($likeORunlike['like'] == 1) // if alredy liked set unlike for alredy liked product { mysqli_query($connection,"update `likes` set `unlike`=1,`like`=0 where id='".$likeORunlike['id']."' and uid='".$uid."'"); echo 2; } elseif($likeORunlike['unlike'] == 1) // if alredy unliked set like for alredy unliked product { mysqli_query($connection,"update `likes` set `like`=1,`unlike`=0 where id='".$likeORunlike['id']."' and uid='".$uid."'"); echo 2; } } else // New Like { mysqli_query($connection,"INSERT INTO `likes` (`pid`,`uid`, `$gofor`) VALUES ('$pid','$uid','1')"); echo 1; } exit; } $query = "SELECT * FROM `products`"; // products list $res = mysqli_query($connection,$query); $HTML = ""; while($row=mysqli_fetch_array($res)) { // get likes and dislikes of a product $query = mysqli_query($connection,"select sum(`like`) as `like`,sum(`unlike`) as `unlike` from `likes` where pid = ".$row['id']); $rowCount = mysqli_fetch_array($query); if($rowCount['like'] == "") $rowCount['like'] = 0; if($rowCount['unlike'] == "") $rowCount['unlike'] = 0; if($uid == "") // if user not loggedin then show login link on like button click { $like = ' '; } else { $query = mysqli_query($connection,"SELECT * from `likes` WHERE pid='".$row['id']."' and uid='".$uid."'"); if(mysqli_num_rows($query)>0){ //if already liked od disliked a product $likeORunlike = mysqli_fetch_array($query); // clear values of variables $liked = ''; $unliked = ''; $disable_like = ''; $disable_unlike = ''; if($likeORunlike['like'] == 1) // if alredy liked then disable like button { $liked = 'disabled="disabled"'; $disable_unlike = "button_disable"; } elseif($likeORunlike['unlike'] == 1) // if alredy dislike the disable unlike button { $unliked = 'disabled="disabled"'; $disable_like = "button_disable"; } $like = ' '; } else{ //not liked and disliked product $like = ' '; } } $HTML.=' '.$row['product_name'].'
$'.$row['price'].'
Buy Now
'.$like.'
PHPGang Shopping store |