Skip to content

Instantly share code, notes, and snippets.

@lmatteis
Created November 10, 2011 10:48
Show Gist options
  • Save lmatteis/1354589 to your computer and use it in GitHub Desktop.
Save lmatteis/1354589 to your computer and use it in GitHub Desktop.
I can actually still write PHP code!
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("singer_dec_21") or die(mysql_error());
$origcodes = array(
'AFG',
'DZA',
'BHR',
'CYP',
'DJI',
'EGY',
'IRN',
'IRQ',
'JOR',
'KWT',
'LBN',
'LBY',
'MLT',
'MRT',
'MAR',
'OMN',
'PAK',
'PSE',
'QAT',
'SAU',
'SOM',
'SDN',
'SYR',
'TUN',
'TUR',
'ARE',
'YEM'
);
$institutes = array();
$ret = array();
foreach($origcodes as $code) {
$temp = array();
// get total
$total = mysql_query("select count(*) as tot from accdata where `origcode_` = '".$code."'")
or die(mysql_error());
$totals = mysql_fetch_array($total);
$tot = $totals[0];
$temp['origcode'] = $code;
$temp['tot'] = $tot;
//echo $code. ', ';
//echo $tot. ', ';
// get the total for each institute
$result = mysql_query("select instname, count(*) as tot from accdata where `origcode_` = '".$code."' group by `instname`")
or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$inst = $row['instname'];
$institutes[$inst] = true;
//echo $inst. '('.$row['tot'].'), ';
$temp[$inst] = $row['tot'];
}
//echo "\n";
$ret[] = $temp;
}
// build header
echo ',';
echo 'Total, ';
foreach($institutes as $in=>$v) {
echo $in.', ';
}
echo "\n";
foreach($ret as $arow) {
echo $arow['origcode'].',';
echo $arow['tot'].',';
foreach($institutes as $in=>$v) {
echo $arow[$in].', ';
}
echo "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment