Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. CREATE TABLE IF NOT EXISTS `our_productions` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `youtube_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `like_num` bigint(10) NOT NULL,
  6. `dislike_num` bigint(10) NOT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
  9.  
  10. <?php
  11. class Tutorial{
  12.  
  13. function __construct(){
  14. //db details
  15. $db_host = 'localhost';
  16. $db_user = 'root';
  17. $db_pass = '123456';
  18. $db_name = 'productions';
  19.  
  20. //connect db
  21. $con = mysql_connect($db_host, $db_user, $db_pass);
  22. mysql_set_charset('utf8');
  23.  
  24. //select db
  25. mysql_select_db($db_name, $con);
  26. }
  27.  
  28. function get_rows($id = ''){
  29. if($id != ''){
  30. //fetch single row
  31. $query = mysql_query("SELECT * FROM our_productions WHERE id = $id");
  32. $data = mysql_fetch_assoc($query);
  33. }else{
  34. //fetch all rows
  35. $query = mysql_query("SELECT * FROM our_productions");
  36. while($row = mysql_fetch_assoc($query)){
  37. $data[] = $row;
  38. }
  39. }
  40. return $data;
  41. }
  42.  
  43. function insert($data = array()){
  44. $data_array_num = count($data);
  45. $columns = "";
  46. $values = "";
  47. $i=0;
  48. foreach($data as $key=>$val){
  49. $i++;
  50. $sep = ($i == $data_array_num)?"":", ";
  51. $columns .= $key.$sep;
  52. $values .= $val.$sep;
  53. }
  54. $insert = mysql_query("INSERT INTO our_productions ($columns) VALUES ($values)");
  55. return $insert?TRUE:FALSE;
  56. }
  57.  
  58. function update($data = array(), $conditions = array()){
  59. $data_array_num = count($data);
  60. $cols_vals = "";
  61. $condition_str = "";
  62. $i=0;
  63. foreach($data as $key=>$val){
  64. $i++;
  65. $sep = ($i == $data_array_num)?'':', ';
  66. $cols_vals .= $key."='".$val."'".$sep;
  67. }
  68. foreach($conditions as $key=>$val){
  69. $i++;
  70. $sep = ($i == $data_array_num)?"":" AND ";
  71. $condition_str .= $key."='".$val."'";
  72. }
  73.  
  74. $update = mysql_query("UPDATE our_productions SET $cols_vals WHERE $condition_str");
  75. return $update?TRUE:FALSE;
  76. }
  77. }
  78. ?>
  79.  
  80. <?php
  81. include_once("db.php");
  82. $tutorial = new Tutorial();
  83.  
  84. if(isset($_POST['id']) != '' && isset($_POST['type']) != ''){
  85. //previous tutorial data
  86. $prev_record = $tutorial->get_rows($_POST['id']);
  87. //previous total likes
  88. $prev_like = $prev_record['like_num'];
  89. //previous total dislikes
  90. $prev_dislike = $prev_record['dislike_num'];
  91.  
  92. //calculates the numbers of like or dislike
  93. if($_POST['type'] == 1){
  94. $like = ($prev_like + 1);
  95. $dislike = $prev_dislike;
  96. $return_count = $like;
  97. }else{
  98. $like = $prev_like;
  99. $dislike = ($prev_dislike + 1);
  100. $return_count = $dislike;
  101. }
  102.  
  103. //store update data
  104. $data = array('like_num'=>$like,'dislike_num'=>$dislike,'modified'=>date("Y-m-d H:i:s"));
  105. //update condition
  106. $condition = array('id'=>$_POST['id']);
  107. //update tutorial like dislike
  108. $update = $tutorial->update($data,$condition);
  109.  
  110. //return like or dislike number if update is successful, otherwise return error
  111. echo $update?$return_count:'err';
  112. }
  113. ?>
  114.  
  115. <?php
  116. include_once("db.php");
  117. $tutorial = new Tutorial();
  118. $trows = $tutorial->get_rows();
  119. ?>
  120.  
  121. <script src="js/jquery-1.11.1.min.js"></script>
  122. <script type="text/javascript">
  123.  
  124. function voting(id,type,target){
  125. $.ajax({
  126. type:'POST',
  127. url:'votes.php',
  128. data:'id='+id+'&type='+type,
  129. success:function(msg){
  130. if(msg == 'err'){
  131. alert('Some problem occured, please try again.');
  132. }else{
  133. $('#'+target).html(msg);
  134. }
  135. }
  136. });
  137. }
  138. </script>
  139.  
  140. <?php foreach($trows as $trow){ ?>
  141. <div id="content" class="full-width flush-top flush-bottom">
  142. <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">
  143.  
  144. <div class="full-width red timestamp">
  145. <h2 class="month">Jan</h2>
  146. <h2 class="year">2016</h2>
  147. </div>
  148.  
  149. <div class="full-width">
  150. <div class="container">
  151. <header>
  152. <time class="day"><?php echo $trow['id']; ?></time>
  153. <h2 class="post-title"><a><?php echo $trow['title']; ?></a></h2>
  154. </header>
  155. <div class="entry">
  156. <div class="fve-video-wrapper vimeo" style="padding-bottom:56.25%;">
  157. <iframe width="560" height="315" src="<?php echo $trow['youtube_url']; ?>" frameborder="0" allowfullscreen></iframe>
  158. </div>
  159. <p style="text-align: left;">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  160. tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  161. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  162. consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  163. cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  164. proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  165. </div>
  166.  
  167. <footer class="social-blog">
  168. <!-- Like Icon HTML -->
  169. <span class="glyphicon glyphicon-thumbs-up" onClick="voting(<?php echo $trow['id']; ?>,1,'like_count<?php echo $trow['id']; ?>')"></span>&nbsp;
  170. <!-- Like Counter -->
  171. <span class="counter" id="like_count<?php echo $trow['id']; ?>"><?php echo $trow['like_num']; ?></span>&nbsp;&nbsp;&nbsp;
  172.  
  173. <!-- Dislike Icon HTML -->
  174. <span class="glyphicon glyphicon-thumbs-down" onClick="voting(<?php echo $trow['id']; ?>,0,'dislike_count<?php echo $trow['id']; ?>')"></span>&nbsp;
  175. <!-- Dislike Counter -->
  176. <span class="counter" id="dislike_count<?php echo $trow['id']; ?>"><?php echo $trow['dislike_num']; ?></span>
  177. </footer>
  178. </div>
  179. </div>
  180. </article>
  181. </div>
  182. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement