What’s this?
This is my JogLog. It’s where I’m keeping data for my weekly runs. The graphs on the right side of the main page are powered by PHP/SWF Charts, which is a pretty sweet little piece of software.
I’ve been running, off and on, for about four years. The first two of those years were the best–I didn’t run hard, but I ran regularly and weight came off. I’m hoping that this little blog will help me track my progress as I recommit to a regular running schedule.
It took a while to figure out how to connect the charts to the custom data in WordPress, but my friend Jeff got me going. In case it helps anyone else, here’s the code that formats the chart and pulls the data from the custom “Weight” field into the chart:
< ?php
include "charts.php";
//change the chart to a bar chart
$chart [ 'chart_type' ] = "line";
$chart [ 'chart_pref' ] = array ( 'line_thickness' => 3,
'point_shape' => "circle",
'fill_shape' => true
);
$chart[ 'chart_value' ] = array ( 'prefix'=>"",
'suffix'=>" lbs",
'decimals'=>1,
'separator'=>"",
'position'=>"cursor",
'size'=>12 );
//the chart's background
$chart [ 'chart_bg' ] = array ( 'positive_alpha' => 0 );
//connect to the database
$dbh=mysql_connect ("yourhost", "your_db_name", "your_table_name") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ( "your_db_name" );
// initialize (construct) a new array
$chart["chart_data"] = array();
// Setting up the php array
$chart [ 'chart_data' ][ 0 ][ 0 ] = "";
$chart [ 'chart_data' ][ 1 ][ 0 ] = "Weight";
// Selecting the columns
$sql = "
SELECT a.post_date AS day,
b.meta_value AS weight
FROM wp_posts a,
wp_postmeta b
WHERE a.id = b.post_id AND
b.meta_key = 'Weight'
ORDER BY a.post_date";
$result = mysql_query($sql);
for ( $i=0; $i < mysql_num_rows($result); $i++ ) {
$col = mysql_result ( $result, $i, "day");
$date = mysql_result ( $result, $i, "day"); // assign the variable "date" the "day" value from the query
$date = strtotime($date); // the "date()" method we will use next expects a unix timestamp
$date = date ( "n/j", $date); // this will convert the timestamp to the format of your choice
// $date = date ( "n/j", strtotime($row["day"]));
$row = $i+1;
$chart ['chart_data'][0][$row] = $date;
$chart ['chart_data'][1][$row] = mysql_result ( $result, $i, "weight");
}
//draw some text
$chart [ 'draw_text' ] = array ( array ( 'x' => 210,
'y' => 10,
'width' => 100,
'height' => 100,
'h_align' => "center",
'v_align' => "top",
'rotation' => 90,
'text' => "weight",
'font' => "Arial",
'bold' => true,
'size' => 30,
'color' => "000000",
'alpha' => 20
));
$chart [ 'legend_bg' ] = array ( 'bg_alpha' => 0,
'border_alpha' => 0
);
$chart [ 'legend_label' ] = array ( 'alpha' => 0,
'size' => 0 );
//send the new data to charts.swf
SendChartData ( $chart );
?>