Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- AUTHOR: A. Student
- CREATE DATE: 3/14/16
- FILE: Homework8C.php
- PURPOSE: Read in and calculate average percentages of Super Tuesday results
- CALLS: VotingPatterns.txt
- -->
- <html>
- <head><title>Super Tuesday Votes</title></head>
- <body>
- <?php
- $host = "studentwebmysql.ewu.edu"
- $user = "astudentname";
- $password = "password";
- $dbName = "votesDB"; //same as user
- $cxn = mysqli_connect($host, $user, $password, $dbName);
- $votingFile = fopen("VotingPatterns.txt","r");
- while(!feof($votingFile))
- {
- $votingRecord = fgets($votingFile);
- $voteArray[] = explode(";", $votingRecord);
- }
- fclose($votingRecord);
- //we initialize the averages to zero
- $totlAFC = 0;
- $totlAFS = 0;
- $totlYVC = 0;
- $totlYVS = 0;
- //SIZEOF means we don't have to worry about how big the array is
- for($i = 0; $i <= sizeof($voteArray); $i++)
- {
- /*start by loading up the variables with the array values.
- make sure you are consistent in counter use. this is important if
- you copy/pasted code from different places. here, it's $i in the FOR
- statement and $i in the array counter. */
- $state = $voteArray[$i][0];
- $afSOE = $voteArray[$i][1];
- $afVFC = $voteArray[$i][2];
- $afVFS = $voteArray[$i][3];
- $yvSOE = $voteArray[$i][4];
- $yvVFC = $voteArray[$i][5];
- $yvVFS = $voteArray[$i][6];
- //we total while we are preparing the SQL
- $totAFC = $totAFC + $afVFC;
- $totAFS = $totAFS + $afVFS;
- $totYVC = $totYVC + $yvVFC;
- $totYVS = $totYVS + $yvVFS;
- /* notice that we are running the INSERT query every time we look at a new record
- also notice the different ` and ' marks */
- $sqlString = "INSERT INTO `votes` (`State`,`AFSOE`,`AFVFC`,`AFVFS`,`YVSOE`,`YVVFC`,`YVVFS`) VALUES('$state','$afSOE','$afVFC','$afVFS','$yvSOE','$yvVFC','$yvVFS')";
- $result = mysqli_query($cxn, $voteString);
- }
- //we calculate our averages
- $avgPercentAFC = $totlAFC/9;
- $avgPercentAFS = $totlAFS/9;
- $avgPercentYVC = $totlYVC/9;
- $avgPercentYVS = $totlYVS/9;
- $query = "SELECT * FROM votes";
- $result = mysqli_query($cxn, $sqlString);
- if($result == false)
- {
- print("Error: ".myspli_error($cxn). "</p>");
- }
- else
- {
- echo"<table border='1'>
- <tr><th>State</th><th>AF Share of Electorate</th>
- <th>AF Vote for Clinton</th><th>AF Vote for Sanders</th>
- <th>YV Share of Electorate</th><th>YV Vote for Clinton</th>
- <th>YV Vote for Sanders</th></tr>";
- for($i=0;$i<mysqli_num_rows($result);$i++)
- {
- echo"<tr>";
- $row_array = mysqli_fetch_row($result);
- for($j=0;$j<mysqli_num_fields($result);$j++)
- {
- echo"<td>".$row_array[$row]."</td>\n";
- }
- }
- echo"</table>";
- }
- /*
- print("<table border ='1'><tr><th></th><th>Sanders</th><th>Clinton</th></tr>;");
- print("<tr><th>AF</th><td>$avgPercentAFS%</td><td>$avgPercentAFC%</td></tr>");
- print("<tr><th>YV</th><td>$avgPercentYVS%</td><td>$avgPercentYVC%</td></tr></tr>");
- print("</table>");
- $query = "SELECT * FROM votes";
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement