Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE IF NOT EXISTS `our_productions` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
- `youtube_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
- `like_num` bigint(10) NOT NULL,
- `dislike_num` bigint(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
- <?php
- class Tutorial{
- function __construct(){
- //db details
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = '123456';
- $db_name = 'productions';
- //connect db
- $con = mysql_connect($db_host, $db_user, $db_pass);
- mysql_set_charset('utf8');
- //select db
- mysql_select_db($db_name, $con);
- }
- function get_rows($id = ''){
- if($id != ''){
- //fetch single row
- $query = mysql_query("SELECT * FROM our_productions WHERE id = $id");
- $data = mysql_fetch_assoc($query);
- }else{
- //fetch all rows
- $query = mysql_query("SELECT * FROM our_productions");
- while($row = mysql_fetch_assoc($query)){
- $data[] = $row;
- }
- }
- return $data;
- }
- function insert($data = array()){
- $data_array_num = count($data);
- $columns = "";
- $values = "";
- $i=0;
- foreach($data as $key=>$val){
- $i++;
- $sep = ($i == $data_array_num)?"":", ";
- $columns .= $key.$sep;
- $values .= $val.$sep;
- }
- $insert = mysql_query("INSERT INTO our_productions ($columns) VALUES ($values)");
- return $insert?TRUE:FALSE;
- }
- function update($data = array(), $conditions = array()){
- $data_array_num = count($data);
- $cols_vals = "";
- $condition_str = "";
- $i=0;
- foreach($data as $key=>$val){
- $i++;
- $sep = ($i == $data_array_num)?'':', ';
- $cols_vals .= $key."='".$val."'".$sep;
- }
- foreach($conditions as $key=>$val){
- $i++;
- $sep = ($i == $data_array_num)?"":" AND ";
- $condition_str .= $key."='".$val."'";
- }
- $update = mysql_query("UPDATE our_productions SET $cols_vals WHERE $condition_str");
- return $update?TRUE:FALSE;
- }
- }
- ?>
- <?php
- include_once("db.php");
- $tutorial = new Tutorial();
- if(isset($_POST['id']) != '' && isset($_POST['type']) != ''){
- //previous tutorial data
- $prev_record = $tutorial->get_rows($_POST['id']);
- //previous total likes
- $prev_like = $prev_record['like_num'];
- //previous total dislikes
- $prev_dislike = $prev_record['dislike_num'];
- //calculates the numbers of like or dislike
- if($_POST['type'] == 1){
- $like = ($prev_like + 1);
- $dislike = $prev_dislike;
- $return_count = $like;
- }else{
- $like = $prev_like;
- $dislike = ($prev_dislike + 1);
- $return_count = $dislike;
- }
- //store update data
- $data = array('like_num'=>$like,'dislike_num'=>$dislike,'modified'=>date("Y-m-d H:i:s"));
- //update condition
- $condition = array('id'=>$_POST['id']);
- //update tutorial like dislike
- $update = $tutorial->update($data,$condition);
- //return like or dislike number if update is successful, otherwise return error
- echo $update?$return_count:'err';
- }
- ?>
- <?php
- include_once("db.php");
- $tutorial = new Tutorial();
- $trows = $tutorial->get_rows();
- ?>
- <script src="js/jquery-1.11.1.min.js"></script>
- <script type="text/javascript">
- function voting(id,type,target){
- $.ajax({
- type:'POST',
- url:'votes.php',
- data:'id='+id+'&type='+type,
- success:function(msg){
- if(msg == 'err'){
- alert('Some problem occured, please try again.');
- }else{
- $('#'+target).html(msg);
- }
- }
- });
- }
- </script>
- <?php foreach($trows as $trow){ ?>
- <div id="content" class="full-width flush-top flush-bottom">
- <article class="post-2037 post type-post status-publish format-standard hentry category-aerial category-blog tag-4120 tag-aerial tag-dslr tag-film tag-fortyonetwenty tag-highlight tag-production tag-san-diego tag-video tag-videography tag-vulcan-uav">
- <div class="full-width red timestamp">
- <h2 class="month">Jan</h2>
- <h2 class="year">2016</h2>
- </div>
- <div class="full-width">
- <div class="container">
- <header>
- <time class="day"><?php echo $trow['id']; ?></time>
- <h2 class="post-title"><a><?php echo $trow['title']; ?></a></h2>
- </header>
- <div class="entry">
- <div class="fve-video-wrapper vimeo" style="padding-bottom:56.25%;">
- <iframe width="560" height="315" src="<?php echo $trow['youtube_url']; ?>" frameborder="0" allowfullscreen></iframe>
- </div>
- <p style="text-align: left;">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
- tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
- quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
- consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
- cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
- proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
- </div>
- <footer class="social-blog">
- <!-- Like Icon HTML -->
- <span class="glyphicon glyphicon-thumbs-up" onClick="voting(<?php echo $trow['id']; ?>,1,'like_count<?php echo $trow['id']; ?>')"></span>
- <!-- Like Counter -->
- <span class="counter" id="like_count<?php echo $trow['id']; ?>"><?php echo $trow['like_num']; ?></span>
- <!-- Dislike Icon HTML -->
- <span class="glyphicon glyphicon-thumbs-down" onClick="voting(<?php echo $trow['id']; ?>,0,'dislike_count<?php echo $trow['id']; ?>')"></span>
- <!-- Dislike Counter -->
- <span class="counter" id="dislike_count<?php echo $trow['id']; ?>"><?php echo $trow['dislike_num']; ?></span>
- </footer>
- </div>
- </div>
- </article>
- </div>
- <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement