<?php
function getBar($value, $ext = false) {
    $width = $value * 20;
    if ($width == 0)
        $width = 1;
    $bar = 'redbar.gif';
    if ($width > 182)
        $bar = 'purbar.gif';
    else if ($width > 164)
        $bar = 'blupurbar.gif';
    else if ($width > 146)
        $bar = 'blubar.gif';
    else if ($width > 128)
        $bar = 'greblubar.gif';
    else if ($width > 110)
        $bar = 'grebar.gif';
    else if ($width > 92)
        $bar = 'yelgrebar.gif';
    else if ($width > 74)
        $bar = 'yelbar.gif';
    else if ($width > 56)
        $bar = 'oryelbar.gif';
    else if ($width > 38)
        $bar = 'orbar.gif';
    else if ($width > 20)
        $bar = 'redorbar.gif';

    return '<img src="'. ($ext ? 'http://www.monkeyquiz.com' : '') .'/img/'. $bar .'" height="12" width="'. $width .'" '. ($ext ? 'style="border: 1px solid #000000; border-left: none; vertical-align: middle; padding: 0px; margin: 0px;">' : 'class="bar">');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rate My Life Quiz - Final Results</title>
<link rel="stylesheet" type="text/css" href="life.css">
</head>
<body>

<h1>Rate My Life Quiz Stats</h1>

<?php
$life = 0;
$mind = 0;
$body = 0;
$spirit = 0;
$friends = 0;
$love = 0;
$finance = 0;
$count = 0;
$db = mysql_connect("localhost", 'monkey_dbuser', 'xrWill0');
mysql_select_db("monkey_monkeyquiz",$db);
$result = mysql_query("SELECT AVG(life), AVG(mind), AVG(body), AVG(spirit), AVG(friends), AVG(love), AVG(finance), COUNT(*) FROM life_results WHERE life != -1 AND mind != -1 AND body != -1 AND spirit != -1 AND friends != -1 AND love != -1 AND finance != -1");
if ($row = mysql_fetch_row($result)) {
    $life = round($row[0], 1);
    $mind = round($row[1], 1);
    $body = round($row[2], 1);
    $spirit = round($row[3], 1);
    $friends = round($row[4], 1);
    $love = round($row[5], 1);
    $finance = round($row[6], 1);
    $count = $row[7];
}
?>

<p>The following results box shows the average scores for each rating, based on 
<?= number_format($count) ?> quiz scores.</p><br>

<table cellspacing="0" class="fin">
  <tr>
    <td class="finc life"><b>Life: </b></td>
<?php
if ($life != -1)
    echo '<td class="fin life">'. getBar($life) .' <b>'. $life .'</b></td>';
else
    echo '<td class="fin life"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc"><b>Mind: </b></td>
<?php
if ($mind != -1)
    echo '<td class="fin">'. getBar($mind) .' <b>'. $mind .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc"><b>Body: </b></td>
<?php
if ($body != -1)
    echo '<td class="fin">'. getBar($body) .' <b>'. $body .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc"><b>Spirit: </b></td>
<?php
if ($spirit != -1)
    echo '<td class="fin">'. getBar($spirit) .' <b>'. $spirit .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc"><b>Friends/Family: </b></td>
<?php
if ($friends != -1)
    echo '<td class="fin">'. getBar($friends) .' <b>'. $friends .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc"><b>Love: </b></td>
<?php
if ($love != -1)
    echo '<td class="fin">'. getBar($love) .' <b>'. $love .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc"><b>Finance: </b></td>
<?php
if ($finance != -1)
    echo '<td class="fin">'. getBar($finance) .' <b>'. $finance .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
</table>

<h2>Gender Averages</h2>

<table cellspacing="0" class="fin">
  <tr>
    <td class="finc life" style="border-bottom: none;"><b>Life: </b></td>
    <td class="fin life" style="border-bottom: none;">&nbsp;</td>
  </tr>

<?php
$gender = 'm';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.life) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.life != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $life = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($life) .' <b>'. $life .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Mind: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.mind) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.mind != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $mind = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($mind) .' <b>'. $mind .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Body: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.body) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.body != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $body = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($body) .' <b>'. $body .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Spirit: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.spirit) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.spirit != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $spirit = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($spirit) .' <b>'. $spirit .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Frnds/Fmly: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.friends) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.friends != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $friends = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($friends) .' <b>'. $friends .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Love: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.love) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.love != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $love = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($love) .' <b>'. $love .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Finance: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.finance) FROM life_results b, gender a WHERE a.gender_id=b.gender_id AND b.finance != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $finance = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($finance) .' <b>'. $finance .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Counts: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, COUNT(*) FROM life_results b, gender a WHERE a.gender_id=b.gender_id GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result))
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin"><b>'. number_format($row[2]) .'</b></td></tr>';
?>
</table>

<h2>Age Group Averages</h2>

<table cellspacing="0" class="fin">
  <tr>
    <td class="finc life" style="border-bottom: none;"><b>Life: </b></td>
    <td class="fin life" style="border-bottom: none;">&nbsp;</td>
  </tr>

<?php
$gender = 'm';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.life) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.life != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $life = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($life) .' <b>'. $life .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Mind: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.mind) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.mind != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $mind = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($mind) .' <b>'. $mind .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Body: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.body) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.body != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $body = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($body) .' <b>'. $body .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Spirit: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.spirit) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.spirit != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $spirit = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($spirit) .' <b>'. $spirit .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Frnds/Fmly: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.friends) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.friends != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $friends = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($friends) .' <b>'. $friends .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Love: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.love) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.love != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $love = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($love) .' <b>'. $love .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Finance: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, AVG(b.finance) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id AND b.finance != -1 GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result)) {
    $finance = round($row[2], 1);
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin">'. getBar($finance) .' <b>'. $finance .'</b></td></tr>';
}

echo '<tr><td class="finc" style="border-bottom: none;"><b>Counts: </b></td><td class="fin" style="border-bottom: none;">&nbsp;</td></tr>';
$result = mysql_query("SELECT a.name, a.disp_order, COUNT(*) FROM life_results b, age_group a WHERE a.age_group_id=b.age_group_id GROUP BY a.name, a.disp_order ORDER BY a.disp_order");
while ($row = mysql_fetch_row($result))
    echo '<tr><td class="finc" style="text-align: right;">'. $row[0] .'</td><td class="fin"><b>'. number_format($row[2]) .'</b></td></tr>';
?>
</table>

<br><br>
</body></html>
