Forum Moderators: coopster
Loop through products to find the ids to use (typical format is: x,y,z (comma separated values)
Open Loop through years/months
Execute one sql query for each affected table/database to get the number of affected rows.
Populate my arrays of data
Close year loop
Send data to graph script (jquery jqPlot to be exact) to print on screen
while($row = ...) {
$query = "SELECT .. FROM ... ";
$rst = ...
while($row = ...) {
... more code...
}
}
for ($m=1; $m<=12; $m++) // loop months
{
$startDate = mktime(0, 0, 1, $m, 1, date("Y"));
$daysOfMonth = date("t", mktime(10, 10, 10, $m, 10, date("Y")));
$endDate = mktime(23, 59, 59, $m, $daysOfMonth, date("Y"));
$query_chk1 = "SELECT COUNT(*) AS monthlyTotal FROM db1.table1 WHERE dateMonitor>='$startDate' AND dateMonitor<='$endDate' AND pageName='$myPage' ";
$result_chk1 = mysql_query($query_chk1) or die(mysql_error().'<p>'.$query_chk1.'</p>');
$row1 = mysql_fetch_array($result_chk1);
$num_results1 = $row1['monthlyTotal'];
$monthlyValues[] = $num_results1;
}
SELECT COUNT(*) AS monthlyTotal FROM db1.table1 WHERE dateMonitor>='1167606001' AND dateMonitor<='1170284399' AND dateMonitor='test'
UNION ALL
SELECT COUNT(*) AS monthlyTotal FROM db1.table2 WHERE dateMonitor>='1170284401' AND dateMonitor<='1172703599' AND dateMonitor='test'
...
SELECT COUNT(id) AS monthlyTotal, MONTH(FROM_UNIXTIME(dateMonitor)) as month FROM db1.table1
GROUP BY MONTH(FROM_UNIXTIME(dateMonitor))
WHERE dateMonitor='test'
SELECT COUNT(id) AS monthlyTotal, year, month FROM db1.table1
GROUP BY year, month
WHERE dateMonitor='test'
Loop through products to find the ids to use (typical format is: x,y,z (comma separated values)
Open Loop through years/months
Execute one sql query for each affected table/database to get the number of affected rows.
$query = "select field1,field2,field3,field4 from $table";
// These should be SELECT lists, not user input
$start=$end=$compare=$where=null;
if (
isset($_POST['startmonth']) and ($_POST['startmonth'] > 0) and
isset($_POST['startday']) and ($_POST['startday'] > 0) and
isset($_POST['startyear']) and ($_POST['startyear'] > 0)
) { $start = "$_POST['startday']-$_POST['startmonth']-$_POST['startyear']; }
if (
isset($_POST['endmonth']) and ($_POST['endmonth'] > 0) and
isset($_POST['endday']) and ($_POST['endday'] > 0) and
isset($_POST['endyear']) and ($_POST['endyear'] > 0)
) { $end = "$_POST['endday']-$_POST['endmonth']-$_POST['endyear']; }
if ($start and $end) { $compare = " (dateMonitor >='$start' and dateMonitor <= '$end'"; }
else if ($start) { $compare = dateMonitor >= '$start'; }
else if ($end) { $compare = dateMonitor <= '$end'; }
if ($compare) { $where = " $comparator"; }
if (isset($_POST['pageName']) and ! empty($_POST['pageName'])) {
if ($where) { $where .= ' and'; }
$where .= " pageName='$_POST['pageName']'";
}
if (isset($_POST['productID']) and ! empty($_POST['productID'])) {
if ($where) { $where .= ' and'; }
// If single value, don't use IN - faster
if (preg_match('/,/',$_POST['productID'])) {
$where .= " productID in ('$_POST['productID']')";
}
else { $where .= " productID = '$_POST['productID']'";
}
//
if ($where) { $query .= " where $where"; }
Since most systems I've designed will SELECT data more often then INSERT data, I allways code by the rule: SELECT should be faster then INSERT.
Loop through products to find the ids to use (typical format is: x,y,z (comma separated values)
Open Loop through years/months
Execute one sql query for each affected table/database to get the number of affected rows.
Use the mySQL date commands instead of mktime.
INSERT INTO aggr_table (year, month, page, product, views) VALUES (
SELECT
YEAR(FROM_UNIXTIME(dateMonitor)) as year,
MONTH(FROM_UNIXTIME(dateMonitor)) as month,
pageName,
productId,
COUNT(primaryID)
FROM stats2006
GROUP BY year, month
)