Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Lila 0.2 - Word Count Analysis of WordPress Posts - PHP & Google Charts
<html>
<head>
<title>Lila Prototype 0.2 - johnmiedema.com</title>
</head>
<body>
<?php
//References
//https://gist.github.com/chasewoodford/51e185ed1d49862bf988
//https://developers.google.com/chart/interactive/docs/gallery/linechart
define('DB_NAME', 'dbname');
define('DB_USER', 'dbuser');
define('DB_PASSWORD', 'dbpassword');
define('DB_HOST', 'dbhost');
// connect to database
$dbhandle = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die (mysql_error());
mysql_select_db(DB_NAME) or die (mysql_error());
// query database
$sql = "
SELECT * FROM `wp_fuix_posts`
WHERE post_type = 'post'
AND post_status IN ('publish')
ORDER BY post_date ASC;
";
$result = mysql_query($sql) or die (mysql_error());
//calculate average word count
$total_count = 0;
$post_total = 0;
$average_count = 0;
while ($row = mysql_fetch_array($result)) {
$post_count = str_word_count($row['post_content']);
$total_count += $post_count;
$post_total +=1;
}
$average_count = $total_count/$post_total;
//populate array for chart
$entry = null;
$post_number = 0;
$post_count = 0;
mysql_data_seek($result, 0); //reset top use again
while ($row = mysql_fetch_array($result)) {
$post_count = str_word_count($row['post_content']);
$post_number +=1;
$entry .= "[".$post_number.",".$post_count.",".$average_count."],";
}
//close the connection
mysql_close($dbhandle);
?>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 50%; height: 50%;"></div>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawBackgroundColor);
function drawBackgroundColor() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Post Word Count');
data.addColumn('number', 'Corpus Average');
data.addRows([
<?php echo $entry ?>
]);
var options = {
title: 'Lila 0.2 - Word Count',
hAxis: {
title: 'Post Sequence'
},
vAxis: {
title: 'Word Count'
},
backgroundColor: '#f1f8e9',
trendlines: {
0: {type: 'linear', color: '#111', opacity: .3, labelInLegend: 'Trend', visibleInLegend: true}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment