Last active
September 9, 2017 10:37
-
-
Save Lysak/f9b5f71b0758ddfc9b1112bfc314665f to your computer and use it in GitHub Desktop.
PHP likes. Complete code using PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Complete code using PDO | |
| db_params.php: | |
| <?php | |
| return array( | |
| 'host' => 'localhost', | |
| 'dbname' => 'dbname', | |
| 'user' => 'root', | |
| 'password' => 'password' | |
| ); | |
| index.php: | |
| <?php | |
| //connect | |
| ini_set('display_errors', 1); | |
| error_reporting(E_ALL); | |
| session_start(); | |
| define('ROOT', dirname(__FILE__)); | |
| // connect to the DB | |
| $charset = "utf8"; | |
| $paramsPath = ROOT . '/config/db_params.php'; | |
| $params = include($paramsPath); | |
| $dsn = "mysql:host={$params['host']};dbname={$params['dbname']};charset=$charset"; | |
| $db = new PDO($dsn, $params['user'], $params['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); | |
| ////////////////////// | |
| if (isset($_POST['liked'])) { | |
| $postid = $_POST['postid']; | |
| $result = $db->query("SELECT * FROM articles WHERE id=$postid"); | |
| $row = $result->fetch(PDO::FETCH_ASSOC); | |
| $n = $row['likes']; | |
| $db->query("UPDATE articles SET likes=$n+1 WHERE id=$postid"); | |
| $db->query("INSERT INTO likes(userid, postid) VALUE(1, $postid)"); | |
| exit(); | |
| } | |
| if (isset($_POST['unliked'])) { | |
| $postid = $_POST['postid']; | |
| $result = $db->query("SELECT * FROM articles WHERE id=$postid"); | |
| $row = $result->fetch(PDO::FETCH_ASSOC); | |
| $n = $row['likes']; | |
| //delete from the likes before updation posts | |
| $db->query("DELETE FROM likes WHERE postid=$postid AND userid=1"); | |
| $db->query("UPDATE articles SET likes=$n-1 WHERE id=$postid"); | |
| exit(); | |
| } | |
| ?> | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Document</title> | |
| <style type="text/css"> | |
| .content { | |
| width: 50%; | |
| margin: 100px auto; | |
| border: 1px solid #cbcbcb; | |
| } | |
| .post { | |
| width: 80%; | |
| margin: 10px auto; | |
| border: 1px solid #cbcbcb; | |
| padding: 10px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="content"> | |
| <!-- Get data from the DB and display on the page --> | |
| <?php | |
| // $query= mysql_query("SELECT * FROM articles"); | |
| $query= $db->query("SELECT * FROM articles"); | |
| //while ($row = mysql_fetch_array($query)) { ?/> | |
| while ($row = $query->fetch(PDO::FETCH_ASSOC)) { ?> | |
| <div class="post"> | |
| <?php echo $row['title']; ?><br> | |
| <?php | |
| // determine if user has already like this post | |
| $result = $db->query("SELECT * FROM likes WHERE userid=1 AND postid=".$row['id'].""); | |
| $result->execute(); | |
| // if (mysql_num_rows($result) == 1) { ?/> | |
| print_r($row['likes']); | |
| if ($result->rowCount() == 1) { ?> | |
| <!-- user already likes post --> | |
| <span><a href="" class="unlike" id="<?php echo $row['id']; ?>">unlike</a></span> | |
| <?php } else { ?> | |
| <!-- user has not yet liked post --> | |
| <span><a href="" class="like" id="<?php echo $row['id']; ?>">like</a></span> | |
| <?php } ?> | |
| </div> | |
| <?php } ?> | |
| </div> | |
| <!-- Add JQuery --> | |
| <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| $(document).ready(function() { | |
| // when the user clicks on like | |
| $('.like').click(function() { | |
| var postid = $(this).attr('id'); | |
| // alert('You clicked on ' + postid); | |
| $.ajax({ | |
| url: 'index.php', | |
| type: 'post', | |
| async: false, | |
| data: { | |
| 'liked': 1, | |
| 'postid': postid | |
| }, | |
| success: function() { | |
| } | |
| }); | |
| }); | |
| // when the user clicks on unlike | |
| $('.unlike').click(function() { | |
| var postid = $(this).attr('id'); | |
| // alert('You clicked on ' + postid); | |
| $.ajax({ | |
| url: 'index.php', | |
| type: 'post', | |
| async: false, | |
| data: { | |
| 'unliked': 1, | |
| 'postid': postid | |
| }, | |
| success: function() { | |
| } | |
| }); | |
| }); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment