<html>
<head>
    <title></title>
    <style type="text/css">
        a:visited {
            text-decoration: none;
            color: white;
        }

        a {
            text-decoration: none;
            color: white;
        }

        a:hover {
            text-decoration: none;
            color: white;
        }

        body {
            padding: 20px;
            color: white;
            background-color: #b00;
        }
    </style>
</head>
<body>
<?php

$couponArray = array();

if(time() < strtotime("2012-07-05 09:00")) {

    $db = new mysqli(
        "192.168.130.211",
        "pingdom_read",
        "eQI43tqD",
        "bill2db"
    );

    if ($db->errno)
    {
        throw new Exception('Couldn\'t connect to database');
    }

    $query = "SELECT
            i.code,
            i.description,
            i.used
        FROM
            bill2coupon AS i
        WHERE
            i.description = '4july'
        AND
            i.used = 0";

    if($ps = $db->prepare($query))
    {
        //$ps->bind_param('ii', $qcfDate, $qctDate);

        $ps->execute();

        $ps->bind_result(
            $code,
            $description,
            $used
        );

        //LOOP RESULTS
        while($ps->fetch())
        {
            $couponArray[$code] = true;
        }

        $ps->close();

    }  else {
        die('prepare() failed: ' . htmlspecialchars($db->error));
    }


    $randomCoupon = array_rand($couponArray);

    if(count($randomCoupon) > 0) {

        echo '<div style="margin-left: auto; margin-right: auto; font-size: 30px; font-family: Helvetica, Arial, sans-serif; width: 400px; text-align: center;">';
        echo 'Click code to claim<br><br>';
        echo '<b><a href="https://www.pingdom.com/signup/discountcode/basic/' . $randomCoupon . '" target="_blank">' . $randomCoupon . '</a></b></div>';

    } else {
        echo '<div style="margin-left: auto; margin-right: auto; font-size: 30px; font-family: Helvetica, Arial, sans-serif; width: 400px; text-align: center;">';
        echo 'Click code to claim<br><br>';
        echo '<b>Sorry, all coupons claimed!</b></div>';
    }

} else {
    echo '<div style="margin-left: auto; margin-right: auto; font-size: 30px; font-family: Helvetica, Arial, sans-serif; width: 400px; text-align: center;">
            Campaign closed.<br><br></div>';
}

?>
</body>
</html>