Forum Moderators: coopster
$AGE_BRACKET = 5;
$START_AGE = 15;
$TOP_AGE = 100;
$sql = "SELECT age, count(age) as agenumber FROM people GROUP BY age";
$result = mysql_query($sql);
$bracketCount = array();
while ($row = mysql_fetch_assoc($result)) {
if ($row['age'] >= $START_AGE) {
$division = ceil($row['age'] / $AGE_BRACKET)-1;
$bracketCount[$division] += $row['agenumber'];
}
}
for ($lowerBracket = $START_AGE; $lowerBracket < $TOP_AGE; $lowerBracket=$lowerBracket+$AGE_BRACKET) {
$upperBracket = $lowerBracket+$AGE_BRACKET-1;
$bracketTotal = $bracketCount[$lowerBracket/($AGE_BRACKET)];
echo $lowerBracket . " - " . $upperBracket . " ";
for ($i = 0; $i < $bracketTotal; $i++) {
echo "*";
}
echo "<br>";
}
What I like to do is put it in a table so the histogram lines up nicely and then instead of asterisks use a small gif maybe 5x10 and echo out the image file instead. They'll all nicely stack up to look like a bar chart.
The age bracket thingy is what made it tougher. Any other variable would be easier.
$sql = "SELECT region, count(region) as regioncount FROM people GROUP BY region";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row['region'] . ' ';
for ($i = 0; $i < $row['regioncount']; $i++) {
echo "*";
}
echo "<br>";
}