Advertisement
str8mjed

Ver Code

Mar 16th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 1);
  4. //==========================================
  5. //DB config
  6. //==========================================
  7. $servername = "localhost";
  8. $username = "root";
  9. $password = "KRe9bUn9U8eGKnWB";
  10. $dbname = "missu2016";
  11. //==========================================
  12. // Create connection
  13. $conn = new mysqli($servername, $username, $password, $dbname);
  14.  
  15. // Check connection
  16. if ($conn->connect_error) {
  17. die("Connection failed: " . $conn->connect_error);
  18. }
  19.  
  20. $sql = "SELECT cv.candidate_id,c.name,c.country,COUNT(cv.candidate_id) as number_votes FROM count_votes as cv INNER JOIN candidate as c ON c.id = cv.candidate_id GROUP BY candidate_id ORDER BY COUNT(candidate_id) DESC";
  21.  
  22. $votes = $conn->query($sql);
  23.  
  24. $count_votes = array();
  25. if ($votes->num_rows > 0) {
  26. // output data of each row
  27. while($row = $votes->fetch_assoc()) {
  28. echo"
  29. <tr>
  30. <td>".$row["candidate_id"]."</td>
  31. <td>".$row["candidate_id"]."</td>
  32. <td>".$row["candidate_id"]."</td>
  33. <td>".$row["candidate_id"]."</td>
  34. <td>".$row["candidate_id"]."</td>
  35. </tr>
  36. ";
  37. $count_votes[] = array("id"=>$row['candidate_id'],"name"=>$row['name'],"country"=>$row['country'],"votes"=>$row['number_votes']);
  38. }
  39. }
  40.  
  41. $count_votes = array("lists"=>$count_votes);
  42. echo json_encode($count_votes);
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement