PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/random.php

https://bitbucket.org/wlynch92/cs336-dbproject
PHP | 165 lines | 146 code | 14 blank | 5 comment | 41 complexity | fb9c272ac5d23ce28066f4564fd12053 MD5 | raw file
  1. <html>
  2. <body>
  3. <a href="/profile.php"><h1>Music Box</h1></a>
  4. A social music site for everyone!<p>
  5. <?php
  6. session_start();
  7. if($_SESSION['username']){
  8. #echo "<br>";
  9. echo "<div align = \"left\"> <a href=\"/logout.php\">Logout</a><br></div>";
  10. }
  11. if ($_SESSION['username']){
  12. echo "Logged in as: ".$_SESSION['username']."\n<p>\n";
  13. }
  14. ?>
  15. <div align=right>
  16. | <a href="/music.php">Top Songs & Artists</a> |
  17. <a href="/artistfind.php">Search for Artist</a> |
  18. <a href="/random.php">Site Info and Facts</a> |
  19. </div>
  20. <p><center><hr width=100% noshade=noshde></center><p>
  21. <form method="get">
  22. Search for artist: <input type="text" name = "artist">
  23. <input type="submit" value="Search">
  24. </form>
  25. <h2><center> Sample Facts about our users </center></h2>
  26. <?php
  27. session_start();
  28. $connection = mysql_connect("cs336-64.rutgers.edu","csuser","cs277315");
  29. if (!$connection)
  30. {
  31. die('Cannot connect to server'); /*Prints to the browser*/
  32. }
  33. mysql_select_db("cs336",$connection);
  34. echo "<h3>Number of:</h3>";
  35. echo "<form action=\"random.php\" method=\"post\">";
  36. echo "<select name=\"topic\">";
  37. echo "<option value=\"school\">schools attended by users</option>";
  38. echo "<option value=\"company\">companies users are employed by</option>";
  39. echo "<option value=\"message\">messages users have sent</option>";
  40. echo "<option value=\"Male\">Male users</option>";
  41. echo "<option value=\"Female\">Female users</option>";
  42. echo "<option value=\"AvSongLike\">Average songs liked</option>";
  43. echo "<option value=\"AvAgeSchool\">Average age of users by school</option>";
  44. echo "<option value=\"GreaterThanAvMesSentBySchool\">Users that have sent more messages than the average number of messages sent by thier school</option>";
  45. echo "<option value=\"relationship\">Users that are in a relationship and are in the same school</option>";
  46. echo "</select>\n<input type=\"submit\"></input>\n</form>";
  47. if ($_POST['topic']){
  48. if ($_POST['topic'] == 'Male')
  49. {
  50. $query = "select count(*) as count from user where gender = 'Male'";
  51. $res = mysql_query("$query");
  52. $num = mysql_fetch_array($res);
  53. echo "<b>Male users: </b>";
  54. echo $num['count'];
  55. }
  56. else if ($_POST['topic'] == 'Female')
  57. {
  58. $query = "select count(*) as count from user where gender = 'Female'";
  59. $res = mysql_query("$query");
  60. $num = mysql_fetch_array($res);
  61. echo "<b>Female users: </b>";
  62. echo $num['count'];
  63. }
  64. else if ($_POST['topic'] == 'AvSongLike')
  65. {
  66. $query = "select count(DISTINCT l.uid)/count(DISTINCT u.uid) as count from likesSong l , user u";
  67. $res = mysql_query("$query");
  68. $num = mysql_fetch_array($res);
  69. echo "<b>Average songs liked: </b>";
  70. echo $num['count']."<br/>";
  71. }
  72. else if ($_POST['topic'] == 'AvAgeSchool')
  73. {
  74. $query = "select s.sname as name, AVG(YEAR(CURDATE())-YEAR(birth)) as avg from user u, attended a, school s where a.sid=s.sid and u.uid=a.uid group by a.sid;";
  75. $res = mysql_query("$query");
  76. echo "<b>Average age of users by school: </b><br>";
  77. while ($num = mysql_fetch_array($res)){
  78. echo $num['name'].": ";
  79. echo $num['avg']."<br/>";
  80. }
  81. }
  82. else if ($_POST['topic'] == 'GreaterThanAvMesSentBySchool')
  83. {
  84. $query = "select distinct username,sname,mcount from (select u.username, a.uid,count(m.mid) as mcount from user u, attended a, message m where u.uid=a.uid and m.senderid=a.uid group by a.uid) as c, (select a.sname,a.sid,a.mcount/b.scount avg from (select s.sname, s.sid,count(m.mid) as mcount from school s, attended a, message m where s.sid=a.sid and m.senderid=a.uid group by s.sid) as a, (select sid,count(distinct uid) as scount from attended group by sid) as b where a.sid=b.sid) as d, attended at where c.mcount>=avg and at.uid=c.uid and at.sid=d.sid";
  85. $res = mysql_query("$query");
  86. if ($res){
  87. echo mysql_error();
  88. }
  89. echo "<b>Users that have sent more messages than the average number of messages sent by thier school:</b><br>";
  90. while ($num = mysql_fetch_array($res)){
  91. echo $num['sname'].": \t";
  92. echo $num['username']."<br/>";
  93. }
  94. }
  95. else if ($_POST['topic'] == 'relationship')
  96. {
  97. $query = "select c.username as name1, b.username as name2 from (select u.uid,sid,username from attended a, in_relationship_with r,user u where r.user1=a.uid and a.uid=u.uid) as c, (select u.uid,sid,username from attended a, in_relationship_with r, user u where r.user2=a.uid and a.uid=u.uid) as b, in_relationship_with r where c.sid=b.sid and (c.uid=user1 and b.uid=user2) or (c.uid=user2 and b.uid=user1)";
  98. $res = mysql_query("$query");
  99. echo "<b>Users that are in a relationship and are in the same school: </b><br>";
  100. while ($num = mysql_fetch_array($res))
  101. {
  102. echo $num['name1']." with ";
  103. echo $num['name2']."<br/>";
  104. }
  105. }
  106. else {
  107. $query="select DISTINCT count(*) as count from ".$_POST['topic'];
  108. $res = mysql_query("$query");
  109. $num = mysql_fetch_array($res);
  110. if ($_POST['topic'] == 'school') {
  111. echo "<b>school attended by users: </b>";
  112. }
  113. else if ($_POST['topic'] == 'company') {
  114. echo "<b>companies users are employed by: </b>";
  115. }
  116. else if ($_POST['topic'] == 'message') {
  117. echo "<b>messages users have sent: </b>";
  118. }
  119. echo $num['count']."<br/>";
  120. }
  121. }
  122. ?>
  123. <hr width=50% noshade=noshade>
  124. <h3>Likes a certain artist and goes to a certain school</h3>
  125. <form action="/random.php" method="post">
  126. Artist: <input name="artist" type="text">
  127. School: <input name="school" type="text">
  128. <input type="submit">
  129. </form>
  130. <?php
  131. if ($_POST['artist'] && $_POST['school']){
  132. $connection = mysql_connect("cs336-64.rutgers.edu","csuser","cs277315");
  133. if (!$connection)
  134. {
  135. die('Cannot connect to server'); /*Prints to the browser*/
  136. }
  137. mysql_select_db("cs336",$connection);
  138. $query="select u.username as name from user u, likesArtist l, artist a, attended at, school s where s.sid=at.sid and u.uid=at.uid and u.uid=l.uid and a.aid=l.aid and a.name=\"".$_POST['artist']."\" and s.sname=\"".$_POST['school']."\"";
  139. $res=mysql_query($query);
  140. echo "<ul>";
  141. while ($row=mysql_fetch_array($res)){
  142. echo "<li>".$row['name']."</li>\n";
  143. }
  144. echo "</ul>";
  145. mysql_close($connection);
  146. }
  147. #echo "List users in a relationship from the same school<br>";
  148. #select c.username as name1, b.username as name2 from (select u.uid,sid,username from attended a, in_relationship_with r,user u where r.user1=a.uid and a.uid=u.uid) as c, (select u.uid,sid,username from attended a, in_relationship_with r, user u where r.user2=a.uid and a.uid=u.uid) as b, in_relationship_with r where c.sid=b.sid and (c.uid=user1 and b.uid=user2) or (c.uid=user2 and b.uid=user1);
  149. #
  150. # BILLY DO THIS ONE ----->>>>>>> echo "List all single users that like a certain genre<br>";
  151. ?>