Guest User

Untitled

a guest
Jul 20th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <?php
  2.  
  3. include("database.php");
  4.  
  5.  
  6. //select two random images from fs
  7. $dir = 'images';
  8. $files = glob($dir . '/*.*');
  9.  
  10. $file1 = array_rand($files);
  11. $file2 = array_rand($files);
  12. while($file2 == $file1) {
  13. $file2 = array_rand($files);
  14. }
  15.  
  16. $imageLink1 = $files[$file1];
  17. $imageLink2 = $files[$file2];
  18.  
  19. $imageLink1 = substr($imageLink1, strlen($dir) + 1);
  20. $imageLink2 = substr($imageLink2, strlen($dir) + 1);
  21.  
  22. $firstImage = $db->query("SELECT * FROM images WHERE image_name LIKE '$imageLink1'");
  23. if($firstImage->num_rows == 0) {
  24. //new image. set new row
  25. $db->query("INSERT INTO images (image_name) VALUES ('$imageLink1')");
  26. $db->query("UPDATE images SET image_show = image_show + 1 WHERE image_name LIKE '$imageLink1'");
  27.  
  28. $click1 = 0;
  29. $show1 = 1;
  30.  
  31. $percentage1 = 0;
  32. } else {
  33. $db->query("UPDATE images SET image_show = image_show + 1 WHERE image_name LIKE '$imageLink1'");
  34. $row = $firstImage->fetch_assoc();
  35.  
  36. $click1 = $row["image_clicked"];
  37. $show1 = $row["image_show"] + 1;
  38.  
  39. if($row["image_show"] > 0) {
  40. $percentage1 = round($click1 / $show1 * 100, 2);
  41. } else {
  42. $percentage1 = 0;
  43. }
  44. }
  45. $secondImage = $db->query("SELECT * FROM images WHERE image_name LIKE '$imageLink2'");
  46. if($secondImage->num_rows == 0) {
  47. //new image. set new row
  48. $db->query("INSERT INTO images (image_name) VALUES ('$imageLink2')");
  49. $db->query("UPDATE images SET image_show = image_show + 1 WHERE image_name LIKE '$imageLink2'");
  50.  
  51. $click2 = 0;
  52. $show2 = 1;
  53.  
  54. $percentage2 = 0;
  55. } else {
  56. $db->query("UPDATE images SET image_show = image_show + 1 WHERE image_name LIKE '$imageLink2'");
  57. $row = $secondImage->fetch_assoc();
  58.  
  59. $click2 = $row["image_clicked"];
  60. $show2 = $row["image_show"] + 1;
  61.  
  62. if($row["image_show"] > 0) {
  63. $percentage2 = round($click2 / $show2 * 100, 2);
  64. } else {
  65. $percentage2 = 0;
  66. }
  67. }
  68.  
  69. ?>
  70. <html>
  71. <head>
  72. <title>Facemash</title>
  73. <link href="homepage.css" rel="stylesheet" type="text/css">
  74. </head>
  75.  
  76. <script type="text/javascript">
  77. //NProgress.start();
  78. function clickImage(target) {
  79. NProgress.start();
  80. target.parent().addClass("clicked");
  81. var data = {
  82. image: target.attr("data-title")
  83. };
  84. $.post("save.php", data, function(response) {
  85. NProgress.done();
  86. $(".image img").off();
  87. //$(".ricarica").show();
  88. window.location.reload();
  89. });
  90. }
  91. $(document).ready( function() {
  92. $(".image img").click( function(e) {
  93. clickImage($(e.currentTarget));
  94. });
  95. $(".ricarica button").click( function() {
  96. window.location.reload();
  97. });
  98. $(document).keydown(function(e) {
  99. switch(e.which) {
  100. case 37: clickImage($(".left-arrow"));
  101. break;
  102. case 39: clickImage($(".right-arrow"));
  103. break;
  104. default: return;
  105. }
  106. e.preventDefault(); // prevent the default action (scroll / move caret)
  107. });
  108. //NProgress.done();
  109. });
  110. </script>
  111. </head>
  112. <body>
  113. <nav class="menuwrap">
  114. <div class="header">FACEMASH</div>
  115. </nav>
  116. <div class="container">
  117. <div class="image-container">
  118. <div class="image">
  119. <h2 class="image-title"><?php echo $imageLink1; ?></h2>
  120. <img class="left-arrow" src="images/<?php echo $imageLink1; ?>" data-title="<?php echo $imageLink1; ?>"/>
  121. <div class="info">
  122. <p>Won : <?php echo $click1; ?> Lost : <?php echo $show1; ?></p>
  123. </div>
  124. </div>
  125. <div class="image">
  126. <h2 class="image-title"><?php echo $imageLink2; ?></h2>
  127. <img class="right-arrow" src="images/<?php echo $imageLink2; ?>" data-title="<?php echo $imageLink2; ?>"/>
  128. <div class="info">
  129. <p>Won : <?php echo $click2; ?> Lost : <?php echo $show2; ?></p>
  130. </div>
  131. </div>
  132. <div class="clearfix"></div>
  133. </div>
  134. <div class="ricarica">
  135. <button class="btn btn-primary">Mostrami altre immagini!</button>
  136. </div>
  137.  
  138. </div>
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment