<?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>This is My Life, Compared</title>
<link rel="stylesheet" type="text/css" href="life.css">
</head>
<body>

<h1>Your Life As It Compares to the Averages</h1>

<?php
if (!$_POST['li'] || !$_POST['xyz']) {
    echo '<p>There are no scores to compare!</p>';
    exit;
}
$life = $_POST['li'];
$mind = $_POST['m'];
$body = $_POST['b'];
$spirit = $_POST['s'];
$friends = $_POST['fr'];
$love = $_POST['lo'];
$finance = $_POST['fi'];
$gender = $_POST['g'];
$age = $_POST['a'];
$alife = 0;
$amind = 0;
$abody = 0;
$aspirit = 0;
$afriends = 0;
$alove = 0;
$afinance = 0;
$count = 0;
$pdo = new PDO('mysql:host=localhost;dbname=monkey_monkeyquiz', 'monkey_dbuser', 'xrWill0');

$stmt = $pdo->prepare("SELECT AVG(life), AVG(mind), AVG(body), AVG(spirit), AVG(friends), AVG(love), AVG(finance), COUNT(*) FROM life_results WHERE age_group_id = ? AND gender_id = ? AND life != -1 AND mind != -1 AND body != -1 AND spirit != -1 AND friends != -1 AND love != -1 AND finance != -1");
$stmt->bindValue(1, $age);
$stmt->bindValue(2, $gender);
$stmt->execute();

if ($row = $stmt->fetch(PDO::FETCH_NUM)) {
    $alife = round($row[0], 1);
    $amind = round($row[1], 1);
    $abody = round($row[2], 1);
    $aspirit = round($row[3], 1);
    $afriends = round($row[4], 1);
    $alove = round($row[5], 1);
    $afinance = round($row[6], 1);
    $count = $row[7];
?>

<p>We thought it might be interesting to let quiz-takers compare their scores to the averages we generate. 
So here are your scores compared to the average results from your age group and gender.  
This average is generated from <?= number_format($count) ?> quiz scores.</p><br>

<div style="width: 637px; padding: 0px; margin: 0px;">
<div style="float: right; width: 265px; vertical-align: middle;">
<?php
if (strstr($xyz, 'gmd')) {
    echo '<a href="http://www.kqzyfj.com/79115hz74z6MOURRWSVMWWQQOOV" target="_top" onmouseover="window.status=\'http://www.gay.com\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.lduhtrp.net/6f102wquiom79FCCHDG7HHBB99G" alt="Gay.com Personals" border="0"></a>';
}
else if (strstr($xyz, 'j') && strstr($xyz, 'd')) {
    echo '<a href="http://www.tkqlhce.com/8a66r09608OQWTTYUXOQPSXRTUQ" target="_top" onmouseover="window.status=\'http://www.jdate.com/default.asp?prm=41330\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.lduhtrp.net/bq98p59y31NPVSSXTWNPORWQSTP" alt="JDate.com" border="0" style="margin-left: 20px;"></a>';
}
else if (strstr($xyz, 'md')) {
    echo '<a href="http://www.tkqlhce.com/20110ft1zt0GIOLLQMPGIHKNOJIQ" target="_top" onmouseover="window.status=\'http://www.americansingles.com/default.asp?p=7090&prm=44816\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.afcyhf.com/sq112kpthnl68EBBGCF687ADE98G" alt="Free Registration" border="0" style="margin-left: 20px;"></a>';
}
else if (strstr($xyz, 'gfd')) {
    echo '<a href="http://www.tkqlhce.com/hn65ox52x4KMSPPUQTKTLLOQQQ" target="_top" onmouseover="window.status=\'http://www.date.com\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.awltovhc.com/13111qmqeki35B88D9C3C447999" alt="" border="0"></a>';
}
else if (strstr($xyz, 'fd')) {
    echo '<a href="http://www.kqzyfj.com/al102nmvsmu9BHEEJFI9BADGHCBC" target="_top" onmouseover="window.status=\'http://www.americansingles.com/default.asp?p=7090&prm=44816\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.afcyhf.com/gg77vvzntrCEKHHMILCEDGJKFEF" alt="Free Registration" border="0" style="margin-left: 25px;"></a>';
}
else if (strstr($xyz, 'm')) {
    echo '<a href="http://www.anrdoezrs.net/ob117biroiq57DAAFBE576969ED6" target="_top" onmouseover="window.status=\'http://www.real.com\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.tqlkg.com/om75o26v0zKMSPPUQTKMLOLOTSL" alt="Real Networks" border="0" style="margin-left: 25px;"></a>';
}
else {
    echo '<a href="http://www.jdoqocy.com/e0111y1A719PRXUUZVYPRQTQTXWS" target="_top" onmouseover="window.status=\'http://www.real.com\';return true;" onmouseout="window.status=\' \';return true;">';
    echo '<img src="http://www.lduhtrp.net/pi105ax0pvtEGMJJOKNEGFIFIMLH" alt="Real Networks" border="0" style="margin-left: 25px;"></a>';
}
?>
</div>

<table cellspacing="0" class="fin">
  <tr>
    <td class="finc life" style="border-bottom: none;"><b>Life: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </b></td>
<?php
if ($life != -1)
    echo '<td class="fin">'. getBar($life) .' <b>'. $life .'</b></td>';
else
    echo '<td class="fin"><i>Not enough information</i></td>';
?>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($alife) ?> <b><?= $alife ?></b></td>
  </tr>
  <tr>
    <td class="finc" style="border-bottom: none;"><b>Mind: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </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" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($amind) ?> <b><?= $amind ?></b></td>
  </tr>
  <tr>
    <td class="finc" style="border-bottom: none;"><b>Body: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </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" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($abody) ?> <b><?= $abody ?></b></td>
  </tr>
  <tr>
    <td class="finc" style="border-bottom: none;"><b>Spirit: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </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" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($aspirit) ?> <b><?= $aspirit ?></b></td>
  </tr>
  <tr>
    <td class="finc" style="border-bottom: none;"><b>Friends/Family: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </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" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($afriends) ?> <b><?= $afriends ?></b></td>
  </tr>
  <tr>
    <td class="finc" style="border-bottom: none;"><b>Love: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </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" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($alove) ?> <b><?= $alove ?></b></td>
  </tr>
  <tr>
    <td class="finc" style="border-bottom: none;"><b>Finance: </b></td><td class="fin">&nbsp;</td>
  </tr>
  <tr>
    <td class="finc" style="text-align: right;"><b>You: </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>
  <tr>
    <td class="finc" style="text-align: right;"><b>Average: </b></td>
    <td class="fin"><?= getBar($afinance) ?> <b><?= $afinance ?></b></td>
  </tr>
</table>
</div>
<?php
}
else
    echo 'An error occurred.';
?>

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