如何优化这段PHP代码?

时间:2015-09-06 09:26:54

标签: php mysql

我有一些PHP代码可以多次查询MySQL数据库以获取不同的值并将它们放入变量中。我认为它必须能够通过以某种方式循环和/或使用数组或组合来使这段代码更短,更智能,更高效???

这就是我所拥有的:

$datetoday  = date('Y-m-d');
$dateyesterday  = date('Y-m-d',strtotime("-1 days"));
$date2daysago   = date('Y-m-d',strtotime("-2 days"));
$date3daysago   = date('Y-m-d',strtotime("-3 days"));
$date4daysago   = date('Y-m-d',strtotime("-4 days"));
$date5daysago   = date('Y-m-d',strtotime("-5 days"));
$date6daysago   = date('Y-m-d',strtotime("-6 days"));
$date7daysago   = date('Y-m-d',strtotime("-7 days"));
$date8daysago   = date('Y-m-d',strtotime("-8 days"));
$date9daysago   = date('Y-m-d',strtotime("-9 days"));
$date10daysago  = date('Y-m-d',strtotime("-10 days"));
$date11daysago  = date('Y-m-d',strtotime("-11 days"));
$date12daysago  = date('Y-m-d',strtotime("-12 days"));
$date13daysago  = date('Y-m-d',strtotime("-13 days"));

$eventtotrack = 'Confirmation';

// Show # of Opt-Ins Today
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$datetoday'";
$r = $db->query($q);
$totalconfirmationstoday = $r->num_rows;

// Show # of Confirmations Yesterday
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$dateyesterday'";
$r = $db->query($q);
$totalconfirmationsyesterday = $r->num_rows;

// Show # of Confirmations 2 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date2daysago'";
$r = $db->query($q);
$totalconfirmations2daysago = $r->num_rows;

// Show # of Confirmations 3 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date3daysago'";
$r = $db->query($q);
$totalconfirmations3daysago = $r->num_rows;

// Show # of Confirmations 4 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date4daysago'";
$r = $db->query($q);
$totalconfirmations4daysago = $r->num_rows;

// Show # of Confirmations 5 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date5daysago'";
$r = $db->query($q);
$totalconfirmations5daysago = $r->num_rows;

// Show # of Confirmations 6 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date6daysago'";
$r = $db->query($q);
$totalconfirmations6daysago = $r->num_rows;

// Show # of Confirmations 7 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date7daysago'";
$r = $db->query($q);
$totalconfirmations7daysago = $r->num_rows;

// Show # of Confirmations 8 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date8daysago'";
$r = $db->query($q);
$totalconfirmations8daysago = $r->num_rows;

// Show # of Confirmations 9 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date9daysago'";
$r = $db->query($q);
$totalconfirmations9daysago = $r->num_rows;

// Show # of Confirmations 10 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date10daysago'";
$r = $db->query($q);
$totalconfirmations10daysago = $r->num_rows;

// Show # of Confirmations 11 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date11daysago'";
$r = $db->query($q);
$totalconfirmations11daysago = $r->num_rows;

// Show # of Confirmations 12 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date12daysago'";
$r = $db->query($q);
$totalconfirmations12daysago = $r->num_rows;

// Show # of Confirmations 13 days ago
$q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date13daysago'";
$r = $db->query($q);
$totalconfirmations13daysago = $r->num_rows;

有人可以帮助我,因为我不知道如何以一种所有变量都填充正确值的方式缩短和优化它。

感谢。

2 个答案:

答案 0 :(得分:2)

您可以使用关联数组,而不是为每个值创建新变量。

$dates = array();
for ($daysago = 0; $daysago > -14; $daysago --) {
    $dates[$daysago] = date('Y-m-d', strototime("-".$daysago." days"));
}
$totalconfirmations = array();
foreach($dates as $key => $date){
   $q = "SELECT * FROM customers WHERE trackingEvent='$eventtotrack' AND trackingDate='$date'";
   $r = $db->query($q);
   $totalconfirmations[$key] = $r->num_rows;
}

答案 1 :(得分:2)

您应该尽可能使用最少的查询

$datetoday  = date('Y-m-d');
$date13daysago  = date('Y-m-d',strtotime("-13 days"));
$eventtotrack = 'Confirmation';

$q = "SELECT DATEDIFF(`trackingDate`, '$datetoday') AS `Days_Ago`, `COUNT(`trackingEvent`) AS `num_rows`
    FROM `customers`
    WHERE `trackingEvent` = '$eventtotrack'
        AND `trackingDate` BETWEEN '$date13daysago'
        AND trackingDate = '$datetoday'
GROUP BY `Days_Ago`
ORDER BY `Days_Ago` ASC";

$r = $db->query($q);