Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <!--
  2. AUTHOR: A. Student
  3. CREATE DATE: 3/14/16
  4. FILE: Homework8C.php
  5. PURPOSE: Read in and calculate average percentages of Super Tuesday results
  6. CALLS: VotingPatterns.txt
  7. -->
  8. <html>
  9. <head><title>Super Tuesday Votes</title></head>
  10. <body>
  11.  
  12. <?php
  13.  
  14. $host = "studentwebmysql.ewu.edu"
  15. $user = "astudentname";
  16. $password = "password";
  17. $dbName = "votesDB"; //same as user
  18. $cxn = mysqli_connect($host, $user, $password, $dbName);
  19.  
  20. $votingFile = fopen("VotingPatterns.txt","r");
  21.  
  22. while(!feof($votingFile))
  23. {
  24.     $votingRecord = fgets($votingFile);
  25.     $voteArray[] = explode(";", $votingRecord);
  26. }
  27.  
  28. fclose($votingRecord);
  29.  
  30. //we initialize the averages to zero
  31. $totlAFC = 0;
  32. $totlAFS = 0;
  33. $totlYVC = 0;
  34. $totlYVS = 0;
  35.  
  36. //SIZEOF means we don't have to worry about how big the array is
  37. for($i = 0; $i <= sizeof($voteArray); $i++)
  38. {
  39.     /*start by loading up the variables with the array values.
  40.     make sure you are consistent in counter use. this is important if
  41.     you copy/pasted code from different places. here, it's $i in the FOR
  42.     statement and $i in the array counter. */
  43.     $state = $voteArray[$i][0];
  44.     $afSOE = $voteArray[$i][1];
  45.     $afVFC = $voteArray[$i][2];
  46.     $afVFS = $voteArray[$i][3];
  47.     $yvSOE = $voteArray[$i][4];
  48.     $yvVFC = $voteArray[$i][5];
  49.     $yvVFS = $voteArray[$i][6];
  50.    
  51.     //we total while we are preparing the SQL
  52.     $totAFC = $totAFC + $afVFC;
  53.     $totAFS = $totAFS + $afVFS;
  54.     $totYVC = $totYVC + $yvVFC;
  55.     $totYVS = $totYVS + $yvVFS;
  56.    
  57.     /* notice that we are running the INSERT query every time we look at a new record
  58.     also notice the different ` and ' marks */
  59.     $sqlString = "INSERT INTO `votes` (`State`,`AFSOE`,`AFVFC`,`AFVFS`,`YVSOE`,`YVVFC`,`YVVFS`) VALUES('$state','$afSOE','$afVFC','$afVFS','$yvSOE','$yvVFC','$yvVFS')";
  60.    
  61.     $result = mysqli_query($cxn, $voteString);
  62. }
  63.  
  64. //we calculate our averages
  65. $avgPercentAFC = $totlAFC/9;
  66. $avgPercentAFS = $totlAFS/9;
  67. $avgPercentYVC = $totlYVC/9;
  68. $avgPercentYVS = $totlYVS/9;
  69.  
  70.  
  71. $query = "SELECT * FROM votes";
  72.  
  73. $result = mysqli_query($cxn, $sqlString);
  74.  
  75. if($result == false)
  76. {
  77.     print("Error: ".myspli_error($cxn). "</p>");
  78. }
  79. else
  80. {
  81.     echo"<table border='1'>
  82.     <tr><th>State</th><th>AF Share of Electorate</th>
  83.     <th>AF Vote for Clinton</th><th>AF Vote for Sanders</th>
  84.     <th>YV Share of Electorate</th><th>YV Vote for Clinton</th>
  85.     <th>YV Vote for Sanders</th></tr>";
  86.    
  87.     for($i=0;$i<mysqli_num_rows($result);$i++)
  88.     {
  89.         echo"<tr>";
  90.         $row_array = mysqli_fetch_row($result);
  91.         for($j=0;$j<mysqli_num_fields($result);$j++)
  92.         {
  93.             echo"<td>".$row_array[$row]."</td>\n";
  94.         }
  95.     }
  96.     echo"</table>";
  97. }
  98.  
  99. /*
  100. print("<table border ='1'><tr><th></th><th>Sanders</th><th>Clinton</th></tr>;");
  101. print("<tr><th>AF</th><td>$avgPercentAFS%</td><td>$avgPercentAFC%</td></tr>");
  102. print("<tr><th>YV</th><td>$avgPercentYVS%</td><td>$avgPercentYVC%</td></tr></tr>");
  103. print("</table>");
  104. $query = "SELECT * FROM votes";
  105.  
  106. ?>
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement