-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll_results.php
executable file
·149 lines (123 loc) · 5.38 KB
/
poll_results.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// Things to notice:
// The script queries the DB to get the contents of the poll table
// it then uses Google Charts scripts to plot the data
// The script produces two charts from the same set of data
// The process is simple, since we just tell the script to render another type of chart
// execute the header script:
require_once "header.php";
echo "<h2>Favourite Games (Poll Table)</h2>";
// The following code simply gets the contents of the polldata table
// RETRIEVE FILM AND VOTE DETAILS FROM THE DB
// connect directly to our database (notice 4th argument):
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// if the connection fails, we need to know, so allow this exit:
if (!$connection)
{
die("Connection failed: " . $mysqli_connect_error);
}
//reset query and result variables as we used them earlier
$query = $result = "";
// get all favourite films - exclude any 'empty' favourites
$query = "SELECT game, votes FROM poll WHERE comments IS NOT NULL";
// this query can return data ($result is an identifier):
$result = mysqli_query($connection, $query);
// how many rows of data come back?
$n = mysqli_num_rows($result);
// if we got some results then use them to plot a graph
if ($n > 0) {
// create a HEREDOC to hold the Google Charts script
echo <<<_END
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load the Visualization API and the corechart package - notice the 'controls' portion added
google.charts.load('current', {'packages':['corechart', 'controls']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Games');
data.addColumn('number', 'Votes');
data.addRows([
_END;
// loop over all rows, to fill the DataTable
for ($i = 0; $i < $n; $i++) {
// fetch one row as an associative array (elements named after columns):
$row = mysqli_fetch_assoc($result);
// set the size of the bar to plot based upon number of votes versus total votes
echo "['{$row['game']}', {$row['votes']}],";
}
echo <<<_END
]);
// Set chart options
var options = {'title':'Games',
'width':600,
'height':400,
legend: {position: "left"},
};
// Instantiate and draw our chart, passing in some options.
// this creates the normal bar chart
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
//////////////////////////////
// CREATION OF THE PIE CHART
//////////////////////////////
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(
document.getElementById('dashboard_div'));
// Create a range slider, passing some options
var slider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'filter_div',
'options':
{
'filterColumnLabel': 'Votes'
}
});
// set pie chart options
var pieChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'pie_div',
'options':
{
'title':'Games',
'width': 600,
'height': 400,
'pieSliceText': 'value',
'legend': 'right'
}
});
// Establish dependencies, declaring that 'filter' drives 'pieChart',
// so that the pie chart will only display entries that are let through
// given the chosen slider range.
dashboard.bind(slider, pieChart);
dashboard.draw(data);
}
</script>
</head>
<body>
<table bgcolor='#g807g29' cellspacing='5' cellpadding='5'><tr>
<!--Divs that will hold the bar charts-->
<td>
<div id="chart_div"></div>
</td>
<!-- divs to hodl the pie chart -->
<td><div id="dashboard_div">
<div id="filter_div"></div>
<div id="pie_div"></div>
</div></td>
</tr></table>
_END;
}
// if anything else happens indicate a problem
else {
echo "No data available to plot<br>";
}
// finish off the HTML for this page:
require_once "footer.php";
?>