PDA

View Full Version : How to combine 2 queries into one datastore


Quantnet
12-20-2009, 02:17 AM
I have this script in /cron that will query the database and update the datastore
<?php

if (!is_object($vbulletin->db)) {
exit;
} else {
$db = &$vbulletin->db;
}

$totalapps = 0;
$programstats = array();

$appresult = $db->query_read("
SELECT programid
, COUNT(*) AS apps
FROM " . TABLE_PREFIX . "application
GROUP
BY programid
");

while ($program = $db->fetch_array($appresult)) {
$totalapps += $program['apps'];
$programstats[$program['programid']] = $program['apps'];
}

build_datastore(
'appstats',
serialize(array('total' => $totalapps, 'programs' => $programstats)),
true
);


I'd like to extend the query and datastore a bit by adding another query
$avgtime = $db->query_read("
SELECT programid,
ceil(avg((resultdate-datesubmitted)/86400)) as average
FROM application
where finalresult ="Admit" OR
finalresult ="Reject" AND
resultdate > 0
group by programid
");


Would anyone kindly advise me on how to put this extra query in the existing script and update the datastore so I can call the extra variables?

Thanks a lot

Quantnet
12-21-2009, 03:59 AM
Can anyone help?
Thanks

Adrian Schneider
12-21-2009, 06:17 AM
Hey, still playing catch up on the emails. :(

This should do it: <?php

if (!is_object($vbulletin->db)) {
exit;
} else {
$db = &$vbulletin->db;
}

$totalapps = 0;
$averages = array();
$programstats = array();

$appresult = $db->query_read("
SELECT programid
, COUNT(*) AS apps
FROM " . TABLE_PREFIX . "application
GROUP
BY programid
");

while ($program = $db->fetch_array($appresult)) {
$totalapps += $program['apps'];
$programstats[$program['programid']] = $program['apps'];
}

$avgtime = $db->query_read("
SELECT programid,
ceil(avg((resultdate-datesubmitted)/86400)) as average
FROM application
where finalresult ="Admit" OR
finalresult ="Reject" AND
resultdate > 0
group by programid
");

while ($program = $db->fetch_array($avgtime)) {
$averages[$program['programid']] = $program['average'];
}

build_datastore(
'appstats',
serialize(array('total' => $totalapps, 'programs' => $programstats, 'averages' => $averages)),
true
);Cheers

Quantnet
12-22-2009, 03:40 AM
Thanks for your help, Adrian. There is no better person to answer this than you ;)